3 * Zend Framework (http://framework.zend.com/)
5 * @see http://github.com/zendframework/zend-diactoros for the canonical source repository
6 * @copyright Copyright (c) 2015-2016 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license https://github.com/zendframework/zend-diactoros/blob/master/LICENSE.md New BSD License
10 namespace Zend\Diactoros;
12 use InvalidArgumentException;
13 use Psr\Http\Message\StreamInterface;
14 use Psr\Http\Message\UploadedFileInterface;
17 class UploadedFile implements UploadedFileInterface
22 private $clientFilename;
27 private $clientMediaType;
42 private $moved = false;
50 * @var null|StreamInterface
55 * @param string|resource $streamOrFile
57 * @param int $errorStatus
58 * @param string|null $clientFilename
59 * @param string|null $clientMediaType
60 * @throws InvalidArgumentException
62 public function __construct($streamOrFile, $size, $errorStatus, $clientFilename = null, $clientMediaType = null)
64 if ($errorStatus === UPLOAD_ERR_OK) {
65 if (is_string($streamOrFile)) {
66 $this->file = $streamOrFile;
68 if (is_resource($streamOrFile)) {
69 $this->stream = new Stream($streamOrFile);
72 if (! $this->file && ! $this->stream) {
73 if (! $streamOrFile instanceof StreamInterface) {
74 throw new InvalidArgumentException('Invalid stream or file provided for UploadedFile');
76 $this->stream = $streamOrFile;
80 if (! is_int($size)) {
81 throw new InvalidArgumentException('Invalid size provided for UploadedFile; must be an int');
85 if (! is_int($errorStatus)
89 throw new InvalidArgumentException(
90 'Invalid error status for UploadedFile; must be an UPLOAD_ERR_* constant'
93 $this->error = $errorStatus;
95 if (null !== $clientFilename && ! is_string($clientFilename)) {
96 throw new InvalidArgumentException(
97 'Invalid client filename provided for UploadedFile; must be null or a string'
100 $this->clientFilename = $clientFilename;
102 if (null !== $clientMediaType && ! is_string($clientMediaType)) {
103 throw new InvalidArgumentException(
104 'Invalid client media type provided for UploadedFile; must be null or a string'
107 $this->clientMediaType = $clientMediaType;
112 * @throws \RuntimeException if the upload was not successful.
114 public function getStream()
116 if ($this->error !== UPLOAD_ERR_OK) {
117 throw new RuntimeException('Cannot retrieve stream due to upload error');
121 throw new RuntimeException('Cannot retrieve stream after it has already been moved');
124 if ($this->stream instanceof StreamInterface) {
125 return $this->stream;
128 $this->stream = new Stream($this->file);
129 return $this->stream;
135 * @see http://php.net/is_uploaded_file
136 * @see http://php.net/move_uploaded_file
137 * @param string $targetPath Path to which to move the uploaded file.
138 * @throws \RuntimeException if the upload was not successful.
139 * @throws \InvalidArgumentException if the $path specified is invalid.
140 * @throws \RuntimeException on any error during the move operation, or on
141 * the second or subsequent call to the method.
143 public function moveTo($targetPath)
146 throw new RuntimeException('Cannot move file; already moved!');
149 if ($this->error !== UPLOAD_ERR_OK) {
150 throw new RuntimeException('Cannot retrieve stream due to upload error');
153 if (! is_string($targetPath) || empty($targetPath)) {
154 throw new InvalidArgumentException(
155 'Invalid path provided for move operation; must be a non-empty string'
159 $targetDirectory = dirname($targetPath);
160 if (! is_dir($targetDirectory) || ! is_writable($targetDirectory)) {
161 throw new RuntimeException(sprintf(
162 'The target directory `%s` does not exists or is not writable',
169 case (empty($sapi) || 0 === strpos($sapi, 'cli') || ! $this->file):
170 // Non-SAPI environment, or no filename present
171 $this->writeFile($targetPath);
174 // SAPI environment, with file present
175 if (false === move_uploaded_file($this->file, $targetPath)) {
176 throw new RuntimeException('Error occurred while moving uploaded file');
187 * @return int|null The file size in bytes or null if unknown.
189 public function getSize()
197 * @see http://php.net/manual/en/features.file-upload.errors.php
198 * @return int One of PHP's UPLOAD_ERR_XXX constants.
200 public function getError()
208 * @return string|null The filename sent by the client or null if none
211 public function getClientFilename()
213 return $this->clientFilename;
219 public function getClientMediaType()
221 return $this->clientMediaType;
225 * Write internal stream to given path
227 * @param string $path
229 private function writeFile($path)
231 $handle = fopen($path, 'wb+');
232 if (false === $handle) {
233 throw new RuntimeException('Unable to write to designated path');
236 $stream = $this->getStream();
238 while (! $stream->eof()) {
239 fwrite($handle, $stream->read(4096));