3 * @see https://github.com/zendframework/zend-diactoros for the canonical source repository
4 * @copyright Copyright (c) 2015-2017 Zend Technologies USA Inc. (http://www.zend.com)
5 * @license https://github.com/zendframework/zend-diactoros/blob/master/LICENSE.md New BSD License
8 namespace Zend\Diactoros;
10 use InvalidArgumentException;
11 use Psr\Http\Message\StreamInterface;
12 use Psr\Http\Message\UploadedFileInterface;
15 class UploadedFile implements UploadedFileInterface
17 const ERROR_MESSAGES = [
18 UPLOAD_ERR_OK => 'There is no error, the file uploaded with success',
19 UPLOAD_ERR_INI_SIZE => 'The uploaded file exceeds the upload_max_filesize directive in php.ini',
20 UPLOAD_ERR_FORM_SIZE => 'The uploaded file exceeds the MAX_FILE_SIZE directive that was '
21 . 'specified in the HTML form',
22 UPLOAD_ERR_PARTIAL => 'The uploaded file was only partially uploaded',
23 UPLOAD_ERR_NO_FILE => 'No file was uploaded',
24 UPLOAD_ERR_NO_TMP_DIR => 'Missing a temporary folder',
25 UPLOAD_ERR_CANT_WRITE => 'Failed to write file to disk',
26 UPLOAD_ERR_EXTENSION => 'A PHP extension stopped the file upload.',
32 private $clientFilename;
37 private $clientMediaType;
52 private $moved = false;
60 * @var null|StreamInterface
65 * @param string|resource $streamOrFile
67 * @param int $errorStatus
68 * @param string|null $clientFilename
69 * @param string|null $clientMediaType
70 * @throws InvalidArgumentException
72 public function __construct($streamOrFile, $size, $errorStatus, $clientFilename = null, $clientMediaType = null)
74 if ($errorStatus === UPLOAD_ERR_OK) {
75 if (is_string($streamOrFile)) {
76 $this->file = $streamOrFile;
78 if (is_resource($streamOrFile)) {
79 $this->stream = new Stream($streamOrFile);
82 if (! $this->file && ! $this->stream) {
83 if (! $streamOrFile instanceof StreamInterface) {
84 throw new InvalidArgumentException('Invalid stream or file provided for UploadedFile');
86 $this->stream = $streamOrFile;
90 if (! is_int($size)) {
91 throw new InvalidArgumentException('Invalid size provided for UploadedFile; must be an int');
95 if (! is_int($errorStatus)
99 throw new InvalidArgumentException(
100 'Invalid error status for UploadedFile; must be an UPLOAD_ERR_* constant'
103 $this->error = $errorStatus;
105 if (null !== $clientFilename && ! is_string($clientFilename)) {
106 throw new InvalidArgumentException(
107 'Invalid client filename provided for UploadedFile; must be null or a string'
110 $this->clientFilename = $clientFilename;
112 if (null !== $clientMediaType && ! is_string($clientMediaType)) {
113 throw new InvalidArgumentException(
114 'Invalid client media type provided for UploadedFile; must be null or a string'
117 $this->clientMediaType = $clientMediaType;
122 * @throws \RuntimeException if the upload was not successful.
124 public function getStream()
126 if ($this->error !== UPLOAD_ERR_OK) {
127 throw new RuntimeException(sprintf(
128 'Cannot retrieve stream due to upload error: %s',
129 self::ERROR_MESSAGES[$this->error]
134 throw new RuntimeException('Cannot retrieve stream after it has already been moved');
137 if ($this->stream instanceof StreamInterface) {
138 return $this->stream;
141 $this->stream = new Stream($this->file);
142 return $this->stream;
148 * @see http://php.net/is_uploaded_file
149 * @see http://php.net/move_uploaded_file
150 * @param string $targetPath Path to which to move the uploaded file.
151 * @throws \RuntimeException if the upload was not successful.
152 * @throws \InvalidArgumentException if the $path specified is invalid.
153 * @throws \RuntimeException on any error during the move operation, or on
154 * the second or subsequent call to the method.
156 public function moveTo($targetPath)
159 throw new RuntimeException('Cannot move file; already moved!');
162 if ($this->error !== UPLOAD_ERR_OK) {
163 throw new RuntimeException(sprintf(
164 'Cannot retrieve stream due to upload error: %s',
165 self::ERROR_MESSAGES[$this->error]
169 if (! is_string($targetPath) || empty($targetPath)) {
170 throw new InvalidArgumentException(
171 'Invalid path provided for move operation; must be a non-empty string'
175 $targetDirectory = dirname($targetPath);
176 if (! is_dir($targetDirectory) || ! is_writable($targetDirectory)) {
177 throw new RuntimeException(sprintf(
178 'The target directory `%s` does not exists or is not writable',
185 case (empty($sapi) || 0 === strpos($sapi, 'cli') || ! $this->file):
186 // Non-SAPI environment, or no filename present
187 $this->writeFile($targetPath);
190 // SAPI environment, with file present
191 if (false === move_uploaded_file($this->file, $targetPath)) {
192 throw new RuntimeException('Error occurred while moving uploaded file');
203 * @return int|null The file size in bytes or null if unknown.
205 public function getSize()
213 * @see http://php.net/manual/en/features.file-upload.errors.php
214 * @return int One of PHP's UPLOAD_ERR_XXX constants.
216 public function getError()
224 * @return string|null The filename sent by the client or null if none
227 public function getClientFilename()
229 return $this->clientFilename;
235 public function getClientMediaType()
237 return $this->clientMediaType;
241 * Write internal stream to given path
243 * @param string $path
245 private function writeFile($path)
247 $handle = fopen($path, 'wb+');
248 if (false === $handle) {
249 throw new RuntimeException('Unable to write to designated path');
252 $stream = $this->getStream();
254 while (! $stream->eof()) {
255 fwrite($handle, $stream->read(4096));