| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 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\Mime\MimeTypes; |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
class File extends \SplFileInfo |
| 24 |
{ |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
public function __construct(string $path, bool $checkPath = true) |
| 34 |
{ |
| 35 |
if ($checkPath && !is_file($path)) { |
| 36 |
throw new FileNotFoundException($path); |
| 37 |
} |
| 38 |
|
| 39 |
parent::__construct($path); |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
public function guessExtension(): ?string |
| 54 |
{ |
| 55 |
if (!class_exists(MimeTypes::class)) { |
| 56 |
throw new \LogicException('You cannot guess the extension as the Mime component is not installed. Try running "composer require symfony/mime".'); |
| 57 |
} |
| 58 |
|
| 59 |
return MimeTypes::getDefault()->getExtensions($this->getMimeType())[0] ?? null; |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
public function getMimeType(): ?string |
| 72 |
{ |
| 73 |
if (!class_exists(MimeTypes::class)) { |
| 74 |
throw new \LogicException('You cannot guess the mime type as the Mime component is not installed. Try running "composer require symfony/mime".'); |
| 75 |
} |
| 76 |
|
| 77 |
return MimeTypes::getDefault()->guessMimeType($this->getPathname()); |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
public function move(string $directory, ?string $name = null): self |
| 86 |
{ |
| 87 |
$target = $this->getTargetFile($directory, $name); |
| 88 |
|
| 89 |
set_error_handler(static function ($type, $msg) use (&$error) { $error = $msg; }); |
| 90 |
try { |
| 91 |
$renamed = rename($this->getPathname(), $target); |
| 92 |
} finally { |
| 93 |
restore_error_handler(); |
| 94 |
} |
| 95 |
if (!$renamed) { |
| 96 |
throw new FileException(\sprintf('Could not move the file "%s" to "%s" (%s).', $this->getPathname(), $target, strip_tags($error))); |
| 97 |
} |
| 98 |
|
| 99 |
@chmod($target, 0o666 & ~umask()); |
| 100 |
|
| 101 |
return $target; |
| 102 |
} |
| 103 |
|
| 104 |
public function getContent(): string |
| 105 |
{ |
| 106 |
$content = file_get_contents($this->getPathname()); |
| 107 |
|
| 108 |
if (false === $content) { |
| 109 |
throw new FileException(\sprintf('Could not get the content of the file "%s".', $this->getPathname())); |
| 110 |
} |
| 111 |
|
| 112 |
return $content; |
| 113 |
} |
| 114 |
|
| 115 |
protected function getTargetFile(string $directory, ?string $name = null): self |
| 116 |
{ |
| 117 |
if (!is_dir($directory) && !@mkdir($directory, 0o777, true) && !is_dir($directory)) { |
| 118 |
if (is_file($directory)) { |
| 119 |
throw new FileException(\sprintf('Unable to create the "%s" directory: a similarly-named file exists.', $directory)); |
| 120 |
} |
| 121 |
throw new FileException(\sprintf('Unable to create the "%s" directory.', $directory)); |
| 122 |
} elseif (!is_writable($directory)) { |
| 123 |
throw new FileException(\sprintf('Unable to write in the "%s" directory.', $directory)); |
| 124 |
} |
| 125 |
|
| 126 |
$target = rtrim($directory, '/\\').\DIRECTORY_SEPARATOR.(null === $name ? $this->getBasename() : $this->getName($name)); |
| 127 |
|
| 128 |
return new self($target, false); |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
protected function getName(string $name): string |
| 135 |
{ |
| 136 |
$originalName = str_replace('\\', '/', $name); |
| 137 |
$pos = strrpos($originalName, '/'); |
| 138 |
|
| 139 |
return false === $pos ? $originalName : substr($originalName, $pos + 1); |
| 140 |
} |
| 141 |
} |