Subversion Repositories php-qbpwcf

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
3 liveuser 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 The guessed extension or null if it cannot be guessed
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 The guessed mime type (e.g. "application/pdf")
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 A File object representing the new file
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
        $renamed = rename($this->getPathname(), $target);
97
        restore_error_handler();
98
        if (!$renamed) {
99
            throw new FileException(sprintf('Could not move the file "%s" to "%s" (%s).', $this->getPathname(), $target, strip_tags($error)));
100
        }
101
 
102
        @chmod($target, 0666 & ~umask());
103
 
104
        return $target;
105
    }
106
 
107
    /**
108
     * @return self
109
     */
110
    protected function getTargetFile(string $directory, string $name = null)
111
    {
112
        if (!is_dir($directory)) {
113
            if (false === @mkdir($directory, 0777, true) && !is_dir($directory)) {
114
                throw new FileException(sprintf('Unable to create the "%s" directory.', $directory));
115
            }
116
        } elseif (!is_writable($directory)) {
117
            throw new FileException(sprintf('Unable to write in the "%s" directory.', $directory));
118
        }
119
 
120
        $target = rtrim($directory, '/\\').\DIRECTORY_SEPARATOR.(null === $name ? $this->getBasename() : $this->getName($name));
121
 
122
        return new self($target, false);
123
    }
124
 
125
    /**
126
     * Returns locale independent base name of the given path.
127
     *
128
     * @return string
129
     */
130
    protected function getName(string $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
}