~ubuntu-branches/debian/experimental/php-nette/experimental

« back to all changes in this revision

Viewing changes to Nette-2.1.0RC/Nette/Http/FileUpload.php

  • Committer: Package Import Robot
  • Author(s): David Prévot
  • Date: 2013-11-30 08:47:54 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20131130084754-4udf1xsu9085tnfc
Tags: 2.1.0~rc-1
* New upstream branch
* Update copyright

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
 
 
3
/**
 
4
 * This file is part of the Nette Framework (http://nette.org)
 
5
 *
 
6
 * Copyright (c) 2004 David Grudl (http://davidgrudl.com)
 
7
 *
 
8
 * For the full copyright and license information, please view
 
9
 * the file license.txt that was distributed with this source code.
 
10
 */
 
11
 
 
12
namespace Nette\Http;
 
13
 
 
14
use Nette;
 
15
 
 
16
 
 
17
/**
 
18
 * Provides access to individual files that have been uploaded by a client.
 
19
 *
 
20
 * @author     David Grudl
 
21
 *
 
22
 * @property-read string $name
 
23
 * @property-read string $sanitizedName
 
24
 * @property-read string $contentType
 
25
 * @property-read int $size
 
26
 * @property-read string $temporaryFile
 
27
 * @property-read int $error
 
28
 * @property-read bool $ok
 
29
 * @property-read bool $image
 
30
 * @property-read array $imageSize
 
31
 * @property-read string $contents
 
32
 */
 
33
class FileUpload extends Nette\Object
 
34
{
 
35
        /** @var string */
 
36
        private $name;
 
37
 
 
38
        /** @var string */
 
39
        private $type;
 
40
 
 
41
        /** @var string */
 
42
        private $size;
 
43
 
 
44
        /** @var string */
 
45
        private $tmpName;
 
46
 
 
47
        /** @var int */
 
48
        private $error;
 
49
 
 
50
 
 
51
        public function __construct($value)
 
52
        {
 
53
                foreach (array('name', 'type', 'size', 'tmp_name', 'error') as $key) {
 
54
                        if (!isset($value[$key]) || !is_scalar($value[$key])) {
 
55
                                $this->error = UPLOAD_ERR_NO_FILE;
 
56
                                return; // or throw exception?
 
57
                        }
 
58
                }
 
59
                $this->name = $value['name'];
 
60
                $this->size = $value['size'];
 
61
                $this->tmpName = $value['tmp_name'];
 
62
                $this->error = $value['error'];
 
63
        }
 
64
 
 
65
 
 
66
        /**
 
67
         * Returns the file name.
 
68
         * @return string
 
69
         */
 
70
        public function getName()
 
71
        {
 
72
                return $this->name;
 
73
        }
 
74
 
 
75
 
 
76
        /**
 
77
         * Returns the sanitized file name.
 
78
         * @return string
 
79
         */
 
80
        public function getSanitizedName()
 
81
        {
 
82
                return trim(Nette\Utils\Strings::webalize($this->name, '.', FALSE), '.-');
 
83
        }
 
84
 
 
85
 
 
86
        /**
 
87
         * Returns the MIME content type of an uploaded file.
 
88
         * @return string
 
89
         */
 
90
        public function getContentType()
 
91
        {
 
92
                if ($this->isOk() && $this->type === NULL) {
 
93
                        $this->type = Nette\Utils\MimeTypeDetector::fromFile($this->tmpName);
 
94
                }
 
95
                return $this->type;
 
96
        }
 
97
 
 
98
 
 
99
        /**
 
100
         * Returns the size of an uploaded file.
 
101
         * @return int
 
102
         */
 
103
        public function getSize()
 
104
        {
 
105
                return $this->size;
 
106
        }
 
107
 
 
108
 
 
109
        /**
 
110
         * Returns the path to an uploaded file.
 
111
         * @return string
 
112
         */
 
113
        public function getTemporaryFile()
 
114
        {
 
115
                return $this->tmpName;
 
116
        }
 
117
 
 
118
 
 
119
        /**
 
120
         * Returns the path to an uploaded file.
 
121
         * @return string
 
122
         */
 
123
        public function __toString()
 
124
        {
 
125
                return $this->tmpName;
 
126
        }
 
127
 
 
128
 
 
129
        /**
 
130
         * Returns the error code. {@link http://php.net/manual/en/features.file-upload.errors.php}
 
131
         * @return int
 
132
         */
 
133
        public function getError()
 
134
        {
 
135
                return $this->error;
 
136
        }
 
137
 
 
138
 
 
139
        /**
 
140
         * Is there any error?
 
141
         * @return bool
 
142
         */
 
143
        public function isOk()
 
144
        {
 
145
                return $this->error === UPLOAD_ERR_OK;
 
146
        }
 
147
 
 
148
 
 
149
        /**
 
150
         * Move uploaded file to new location.
 
151
         * @param  string
 
152
         * @return self
 
153
         */
 
154
        public function move($dest)
 
155
        {
 
156
                @mkdir(dirname($dest), 0777, TRUE); // @ - dir may already exist
 
157
                @unlink($dest); // @ - file may not exists
 
158
                if (!call_user_func(is_uploaded_file($this->tmpName) ? 'move_uploaded_file' : 'rename', $this->tmpName, $dest)) {
 
159
                        throw new Nette\InvalidStateException("Unable to move uploaded file '$this->tmpName' to '$dest'.");
 
160
                }
 
161
                chmod($dest, 0666);
 
162
                $this->tmpName = $dest;
 
163
                return $this;
 
164
        }
 
165
 
 
166
 
 
167
        /**
 
168
         * Is uploaded file GIF, PNG or JPEG?
 
169
         * @return bool
 
170
         */
 
171
        public function isImage()
 
172
        {
 
173
                return in_array($this->getContentType(), array('image/gif', 'image/png', 'image/jpeg'), TRUE);
 
174
        }
 
175
 
 
176
 
 
177
        /**
 
178
         * Returns the image.
 
179
         * @return Nette\Image
 
180
         */
 
181
        public function toImage()
 
182
        {
 
183
                return Nette\Image::fromFile($this->tmpName);
 
184
        }
 
185
 
 
186
 
 
187
        /**
 
188
         * Returns the dimensions of an uploaded image as array.
 
189
         * @return array
 
190
         */
 
191
        public function getImageSize()
 
192
        {
 
193
                return $this->isOk() ? @getimagesize($this->tmpName) : NULL; // @ - files smaller than 12 bytes causes read error
 
194
        }
 
195
 
 
196
 
 
197
        /**
 
198
         * Get file contents.
 
199
         * @return string
 
200
         */
 
201
        public function getContents()
 
202
        {
 
203
                // future implementation can try to work around safe_mode and open_basedir limitations
 
204
                return $this->isOk() ? file_get_contents($this->tmpName) : NULL;
 
205
        }
 
206
 
 
207
}