comparison vendor/symfony/http-foundation/File/File.php @ 0:c75dbcec494b

Initial commit from drush-created site
author Chris Cannam
date Thu, 05 Jul 2018 14:24:15 +0000
parents
children a9cd425dd02b
comparison
equal deleted inserted replaced
-1:000000000000 0:c75dbcec494b
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Symfony\Component\HttpFoundation\File;
13
14 use Symfony\Component\HttpFoundation\File\Exception\FileException;
15 use Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException;
16 use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesser;
17 use Symfony\Component\HttpFoundation\File\MimeType\ExtensionGuesser;
18
19 /**
20 * A file in the file system.
21 *
22 * @author Bernhard Schussek <bschussek@gmail.com>
23 */
24 class File extends \SplFileInfo
25 {
26 /**
27 * Constructs a new file from the given path.
28 *
29 * @param string $path The path to the file
30 * @param bool $checkPath Whether to check the path or not
31 *
32 * @throws FileNotFoundException If the given path is not a file
33 */
34 public function __construct($path, $checkPath = true)
35 {
36 if ($checkPath && !is_file($path)) {
37 throw new FileNotFoundException($path);
38 }
39
40 parent::__construct($path);
41 }
42
43 /**
44 * Returns the extension based on the mime type.
45 *
46 * If the mime type is unknown, returns null.
47 *
48 * This method uses the mime type as guessed by getMimeType()
49 * to guess the file extension.
50 *
51 * @return string|null The guessed extension or null if it cannot be guessed
52 *
53 * @see ExtensionGuesser
54 * @see getMimeType()
55 */
56 public function guessExtension()
57 {
58 $type = $this->getMimeType();
59 $guesser = ExtensionGuesser::getInstance();
60
61 return $guesser->guess($type);
62 }
63
64 /**
65 * Returns the mime type of the file.
66 *
67 * The mime type is guessed using a MimeTypeGuesser instance, which uses finfo(),
68 * mime_content_type() and the system binary "file" (in this order), depending on
69 * which of those are available.
70 *
71 * @return string|null The guessed mime type (e.g. "application/pdf")
72 *
73 * @see MimeTypeGuesser
74 */
75 public function getMimeType()
76 {
77 $guesser = MimeTypeGuesser::getInstance();
78
79 return $guesser->guess($this->getPathname());
80 }
81
82 /**
83 * Moves the file to a new location.
84 *
85 * @param string $directory The destination folder
86 * @param string $name The new file name
87 *
88 * @return self A File object representing the new file
89 *
90 * @throws FileException if the target file could not be created
91 */
92 public function move($directory, $name = null)
93 {
94 $target = $this->getTargetFile($directory, $name);
95
96 set_error_handler(function ($type, $msg) use (&$error) { $error = $msg; });
97 $renamed = rename($this->getPathname(), $target);
98 restore_error_handler();
99 if (!$renamed) {
100 throw new FileException(sprintf('Could not move the file "%s" to "%s" (%s)', $this->getPathname(), $target, strip_tags($error)));
101 }
102
103 @chmod($target, 0666 & ~umask());
104
105 return $target;
106 }
107
108 protected function getTargetFile($directory, $name = null)
109 {
110 if (!is_dir($directory)) {
111 if (false === @mkdir($directory, 0777, true) && !is_dir($directory)) {
112 throw new FileException(sprintf('Unable to create the "%s" directory', $directory));
113 }
114 } elseif (!is_writable($directory)) {
115 throw new FileException(sprintf('Unable to write in the "%s" directory', $directory));
116 }
117
118 $target = rtrim($directory, '/\\').DIRECTORY_SEPARATOR.(null === $name ? $this->getBasename() : $this->getName($name));
119
120 return new self($target, false);
121 }
122
123 /**
124 * Returns locale independent base name of the given path.
125 *
126 * @param string $name The new file name
127 *
128 * @return string containing
129 */
130 protected function getName($name)
131 {
132 $originalName = str_replace('\\', '/', $name);
133 $pos = strrpos($originalName, '/');
134 $originalName = false === $pos ? $originalName : substr($originalName, $pos + 1);
135
136 return $originalName;
137 }
138 }