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

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children c2387f117808
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
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 if (!@rename($this->getPathname(), $target)) {
97 $error = error_get_last();
98 throw new FileException(sprintf('Could not move the file "%s" to "%s" (%s)', $this->getPathname(), $target, strip_tags($error['message'])));
99 }
100
101 @chmod($target, 0666 & ~umask());
102
103 return $target;
104 }
105
106 protected function getTargetFile($directory, $name = null)
107 {
108 if (!is_dir($directory)) {
109 if (false === @mkdir($directory, 0777, true) && !is_dir($directory)) {
110 throw new FileException(sprintf('Unable to create the "%s" directory', $directory));
111 }
112 } elseif (!is_writable($directory)) {
113 throw new FileException(sprintf('Unable to write in the "%s" directory', $directory));
114 }
115
116 $target = rtrim($directory, '/\\').DIRECTORY_SEPARATOR.(null === $name ? $this->getBasename() : $this->getName($name));
117
118 return new self($target, false);
119 }
120
121 /**
122 * Returns locale independent base name of the given path.
123 *
124 * @param string $name The new file name
125 *
126 * @return string containing
127 */
128 protected function getName($name)
129 {
130 $originalName = str_replace('\\', '/', $name);
131 $pos = strrpos($originalName, '/');
132 $originalName = false === $pos ? $originalName : substr($originalName, $pos + 1);
133
134 return $originalName;
135 }
136 }