PluginProbe
WPIDE – File Manager & Code Editor / 3.5.9
WPIDE – File Manager & Code Editor v3.5.9
3.5.9 3.5.8 3.5.7 2.0.14 2.0.15 2.0.16 2.0.2 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 2.1 2.2 2.3 2.3.1 2.3.2 2.4.0 2.5 2.6 3.0 3.1 3.2 3.3 All 55 releases
wpide / vendor / symfony / http-foundation / File / File.php

File.php in WPIDE – File Manager & Code Editor 3.5.9, at vendor/symfony/http-foundation/File/File.php

153 lines 4.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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\Mime\MimeTypes;
17
18 /**
19 * A file in the file system.
20 *
21 * @author Bernhard Schussek <bschussek@gmail.com>
22 */
23 class File extends \SplFileInfo
24 {
25 /**
26 * Constructs a new file from the given path.
27 *
28 * @param string $path The path to the file
29 * @param bool $checkPath Whether to check the path or not
30 *
31 * @throws FileNotFoundException If the given path is not a file
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 * Returns the extension based on the mime type.
44 *
45 * If the mime type is unknown, returns null.
46 *
47 * This method uses the mime type as guessed by getMimeType()
48 * to guess the file extension.
49 *
50 * @return string|null
51 *
52 * @see MimeTypes
53 * @see getMimeType()
54 */
55 public function guessExtension()
56 {
57 if (!class_exists(MimeTypes::class)) {
58 throw new \LogicException('You cannot guess the extension as the Mime component is not installed. Try running "composer require symfony/mime".');
59 }
60
61 return MimeTypes::getDefault()->getExtensions($this->getMimeType())[0] ?? null;
62 }
63
64 /**
65 * Returns the mime type of the file.
66 *
67 * The mime type is guessed using a MimeTypeGuesserInterface instance,
68 * which uses finfo_file() then the "file" system binary,
69 * depending on which of those are available.
70 *
71 * @return string|null
72 *
73 * @see MimeTypes
74 */
75 public function getMimeType()
76 {
77 if (!class_exists(MimeTypes::class)) {
78 throw new \LogicException('You cannot guess the mime type as the Mime component is not installed. Try running "composer require symfony/mime".');
79 }
80
81 return MimeTypes::getDefault()->guessMimeType($this->getPathname());
82 }
83
84 /**
85 * Moves the file to a new location.
86 *
87 * @return self
88 *
89 * @throws FileException if the target file could not be created
90 */
91 public function move(string $directory, ?string $name = null)
92 {
93 $target = $this->getTargetFile($directory, $name);
94
95 set_error_handler(function ($type, $msg) use (&$error) { $error = $msg; });
96 try {
97 $renamed = rename($this->getPathname(), $target);
98 } finally {
99 restore_error_handler();
100 }
101 if (!$renamed) {
102 throw new FileException(sprintf('Could not move the file "%s" to "%s" (%s).', $this->getPathname(), $target, strip_tags($error)));
103 }
104
105 @chmod($target, 0666 & ~umask());
106
107 return $target;
108 }
109
110 public function getContent(): string
111 {
112 $content = file_get_contents($this->getPathname());
113
114 if (false === $content) {
115 throw new FileException(sprintf('Could not get the content of the file "%s".', $this->getPathname()));
116 }
117
118 return $content;
119 }
120
121 /**
122 * @return self
123 */
124 protected function getTargetFile(string $directory, ?string $name = null)
125 {
126 if (!is_dir($directory)) {
127 if (false === @mkdir($directory, 0777, true) && !is_dir($directory)) {
128 throw new FileException(sprintf('Unable to create the "%s" directory.', $directory));
129 }
130 } elseif (!is_writable($directory)) {
131 throw new FileException(sprintf('Unable to write in the "%s" directory.', $directory));
132 }
133
134 $target = rtrim($directory, '/\\').\DIRECTORY_SEPARATOR.(null === $name ? $this->getBasename() : $this->getName($name));
135
136 return new self($target, false);
137 }
138
139 /**
140 * Returns locale independent base name of the given path.
141 *
142 * @return string
143 */
144 protected function getName(string $name)
145 {
146 $originalName = str_replace('\\', '/', $name);
147 $pos = strrpos($originalName, '/');
148 $originalName = false === $pos ? $originalName : substr($originalName, $pos + 1);
149
150 return $originalName;
151 }
152 }
153