Overview

Namespaces

  • Kotchasan
    • Cache
    • Database
    • Http
    • Log
    • Orm
  • None
  • PHP
  • Psr
    • Cache
    • Http
      • Message
    • Log

Classes

  • ArrayIterator
  • DateInterval
  • DOMNode
  • Kotchasan
  • Kotchasan\Accordion
  • Kotchasan\ApiController
  • Kotchasan\ArrayTool
  • Kotchasan\Cache\ApcCache
  • Kotchasan\Cache\Cache
  • Kotchasan\Cache\CacheItem
  • Kotchasan\Cache\FileCache
  • Kotchasan\CKEditor
  • Kotchasan\Collection
  • Kotchasan\Config
  • Kotchasan\Controller
  • Kotchasan\Country
  • Kotchasan\Csv
  • Kotchasan\Curl
  • Kotchasan\Currency
  • Kotchasan\Database
  • Kotchasan\Database\Db
  • Kotchasan\Database\DbCache
  • Kotchasan\Database\Driver
  • Kotchasan\Database\PdoMysqlDriver
  • Kotchasan\Database\Query
  • Kotchasan\Database\QueryBuilder
  • Kotchasan\Database\Schema
  • Kotchasan\Database\Sql
  • Kotchasan\DataTable
  • Kotchasan\Date
  • Kotchasan\DOMNode
  • Kotchasan\DOMParser
  • Kotchasan\Email
  • Kotchasan\File
  • Kotchasan\Files
  • Kotchasan\Form
  • Kotchasan\Grid
  • Kotchasan\Html
  • Kotchasan\Htmldoc
  • Kotchasan\HtmlTable
  • Kotchasan\Http\AbstractMessage
  • Kotchasan\Http\AbstractRequest
  • Kotchasan\Http\Message
  • Kotchasan\Http\NotFound
  • Kotchasan\Http\Request
  • Kotchasan\Http\Response
  • Kotchasan\Http\Stream
  • Kotchasan\Http\UploadedFile
  • Kotchasan\Http\Uri
  • Kotchasan\Image
  • Kotchasan\InputItem
  • Kotchasan\Inputs
  • Kotchasan\KBase
  • Kotchasan\Language
  • Kotchasan\ListItem
  • Kotchasan\Log\AbstractLogger
  • Kotchasan\Log\Logger
  • Kotchasan\Login
  • Kotchasan\Menu
  • Kotchasan\Mime
  • Kotchasan\Model
  • Kotchasan\Number
  • Kotchasan\ObjectTool
  • Kotchasan\Orm\Field
  • Kotchasan\Orm\Recordset
  • Kotchasan\Password
  • Kotchasan\Pdf
  • Kotchasan\Province
  • Kotchasan\Router
  • Kotchasan\Session
  • Kotchasan\Singleton
  • Kotchasan\Tab
  • Kotchasan\TableRow
  • Kotchasan\Template
  • Kotchasan\Text
  • Kotchasan\Validator
  • Kotchasan\View
  • PHPMailer
  • Psr\Log\AbstractLogger
  • Psr\Log\LogLevel
  • Psr\Log\NullLogger
  • SMTP

Interfaces

  • ArrayAccess
  • Countable
  • DateTimeInterface
  • Iterator
  • IteratorAggregate
  • Psr\Cache\CacheItemInterface
  • Psr\Cache\CacheItemPoolInterface
  • Psr\Http\Message\MessageInterface
  • Psr\Http\Message\RequestInterface
  • Psr\Http\Message\ResponseInterface
  • Psr\Http\Message\ServerRequestInterface
  • Psr\Http\Message\StreamInterface
  • Psr\Http\Message\UploadedFileInterface
  • Psr\Http\Message\UriInterface
  • Psr\Log\LoggerAwareInterface
  • Psr\Log\LoggerInterface
  • SeekableIterator
  • Serializable
  • Traversable

Traits

  • Psr\Log\LoggerTrait

Exceptions

  • Exception
  • InvalidArgumentException
  • Kotchasan\ApiException
  • Kotchasan\Cache\Exception
  • Kotchasan\Database\Exception
  • Kotchasan\InputItemException
  • LogicException
  • phpmailerException
  • RuntimeException

Functions

  • createClass
  • debug
  • getClassPath
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * @filesource Kotchasan/Http/UploadedFile.php
  4:  *
  5:  * @copyright 2016 Goragod.com
  6:  * @license http://www.kotchasan.com/license/
  7:  *
  8:  * @see http://www.kotchasan.com/
  9:  */
 10: 
 11: namespace Kotchasan\Http;
 12: 
 13: use Kotchasan\Image;
 14: use Kotchasan\Language;
 15: use Psr\Http\Message\UploadedFileInterface;
 16: 
 17: /**
 18:  * Class สำหรับจัดการไฟล์อัปโหลด.
 19:  *
 20:  * @author Goragod Wiriya <admin@goragod.com>
 21:  *
 22:  * @since 1.0
 23:  */
 24: class UploadedFile implements UploadedFileInterface
 25: {
 26:     /**
 27:      * ข้อผิดพลาดการอัปโหลด UPLOAD_ERR_XXX.
 28:      *
 29:      * @var int
 30:      */
 31:     private $error;
 32:     /**
 33:      * นามสกุลของไฟล์อัปโหลด.
 34:      *
 35:      * @var string
 36:      */
 37:     private $ext;
 38:     /**
 39:      * ใช้สำหรับบอกว่ามีการย้ายไฟล์ไปแล้ว.
 40:      *
 41:      * @var bool
 42:      */
 43:     private $isMoved = false;
 44:     /**
 45:      * MIME Type.
 46:      *
 47:      * @var string
 48:      */
 49:     private $mime;
 50:     /**
 51:      * ชื่อไฟล์ที่อัปโหลด.
 52:      *
 53:      * @var string
 54:      */
 55:     private $name;
 56:     /**
 57:      *  Indicates if the upload is from a SAPI environment.
 58:      *
 59:      * @var bool
 60:      */
 61:     private $sapi = false;
 62:     /**
 63:      * ขนาดไฟล์อัปโหลด.
 64:      *
 65:      * @var int
 66:      */
 67:     private $size;
 68:     /**
 69:      * file stream.
 70:      *
 71:      * @var Stream
 72:      */
 73:     private $stream;
 74:     /**
 75:      * ไฟล์อัปโหลด รวมพาธ.
 76:      *
 77:      * @var string
 78:      */
 79:     private $tmp_name;
 80: 
 81:     /**
 82:      * ไฟล์อัปโหลด.
 83:      *
 84:      * @param string $path         ไฟล์อัปโหลด รวมพาธ
 85:      * @param string $originalName ชื่อไฟล์ที่อัปโหลด
 86:      * @param string $mimeType     MIME Type
 87:      * @param int    $size         ขนาดไฟล์อัปโหลด
 88:      * @param int    $error        ข้อผิดพลาดการอัปโหลด UPLOAD_ERR_XXX
 89:      * @param bool   $sapi         indicates if the upload is in a SAPI environment
 90:      */
 91:     public function __construct($path, $originalName, $mimeType = null, $size = null, $error = null, $sapi = true)
 92:     {
 93:         $this->tmp_name = $path;
 94:         $this->name = $originalName;
 95:         $this->mime = $mimeType;
 96:         $this->size = $size;
 97:         $this->error = $error;
 98:         $this->sapi = $sapi;
 99:     }
100: 
101:     /**
102:      * สำเนาไฟล์อัปโหลดไปยังที่อยู่ใหม่
103:      * คืนค่า true ถ้าอัปโหลดเรียบร้อย.
104:      *
105:      * @param string $targetPath ที่อยู่ปลายทางที่ต้องการย้าย
106:      *
107:      * @throws \RuntimeException         ข้อผิดพลาดการอัปโหลด
108:      * @throws \InvalidArgumentException ไดเรคทอรี่ไม่สามารถเขียนได้
109:      *
110:      * @return bool
111:      */
112:     public function copyTo($targetPath)
113:     {
114:         if ($this->isMoved) {
115:             throw new \RuntimeException(sprintf(Language::get('Uploaded file %1s has already been moved'), $this->name));
116:         }
117:         if (!is_writable(dirname($targetPath))) {
118:             throw new \InvalidArgumentException(Language::get('Target directory is not writable'));
119:         }
120:         if (!copy($this->tmp_name, $targetPath)) {
121:             throw new \RuntimeException(sprintf(Language::get('Error copying file %1s to %2s'), $this->name, $targetPath));
122:         }
123: 
124:         return true;
125:     }
126: 
127:     /**
128:      * ฟังก์ชั่น ตัดรูปภาพ ตามขนาดที่กำหนด และย้ายไปยังปลายทาง
129:      * รูปภาพปลายทางจะมีขนาดเท่าที่กำหนด หากรูปภาพต้นฉบับมีขนาดหรืออัตราส่วนไม่พอดีกับขนาดของภาพปลายทาง
130:      * รูปภาพจะถูกตัดขอบออกหรือจะถูกขยาย เพื่อให้พอดีกับรูปภาพปลายทางที่ต้องการ
131:      * ผลลัพท์จะได้ไฟล์รูปภาพ jpg เท่านั้น
132:      * สำเร็จคืนค่า true ไม่สำเร็จคืนค่าข้อความผิดพลาด.
133:      *
134:      * @param array  $exts       นามสกุลของไฟล์รูปภาพที่ยอมรับ เช่น [jpg, gif, png]
135:      * @param string $targetPath path และชื่อไฟล์ของไฟล์รูปภาพปลายทาง
136:      * @param int    $width      ความกว้างของรูปภาพที่ต้องการ
137:      * @param int    $height     ความสูงของรูปภาพที่ต้องการ
138:      * @param string $watermark  (optional) ข้อความลายน้ำ
139:      *
140:      * @throws \InvalidArgumentException ข้อผิดพลาดหากที่อยู่ปลายทางไม่สามารถเขียนได้
141:      * @throws \RuntimeException         ข้อผิดพลาดไม่สามารถสร้างรูปภาพได้
142:      *
143:      * @return bool|string
144:      */
145:     public function cropImage($exts, $targetPath, $width, $height, $watermark = '')
146:     {
147:         $this->check($exts, dirname($targetPath));
148:         $ret = Image::crop($this->tmp_name, $targetPath, $width, $height, $watermark);
149:         if ($ret === false) {
150:             throw new \RuntimeException(Language::get('Unable to create image'));
151:         }
152: 
153:         return true;
154:     }
155: 
156:     /**
157:      * อ่านชื่อไฟล์จากไฟล์ที่อัปโหลดและตัดตัวอักษที่ไม่สามารถใช้เป็นชื่อไฟล์ได้ออก
158:      * ยอมรับ ภาษาอังกฤษ ตัวเลข ( ) _ - และ .(จุด) เท่านั้น
159:      * นอกเหนือจากนั้นจะถูกแทนที่ด้วย $replace ติดกันไม่เกิน 1 ตัวอักษร.
160:      *
161:      * @param string $replace ตัวอักษรที่จะแทนที่อักขระไที่ไม่ต้องการ ถ้าไม่ระบุจะใช้ _ (ขีดล่าง)
162:      *
163:      * @return string
164:      */
165:     public function getCleanFilename($replace = '_')
166:     {
167:         return preg_replace('/[^a-zA-Z0-9_\-\.\(\)]{1,}/', $replace, $this->name);
168:     }
169: 
170:     /**
171:      * คืนค่านามสกุลของไฟล์อัปโหลด ตัวพิมพ์เล็ก เช่น jpg.
172:      *
173:      * @return string
174:      */
175:     public function getClientFileExt()
176:     {
177:         if ($this->ext == null) {
178:             $exts = explode('.', $this->name);
179:             $this->ext = strtolower(end($exts));
180:         }
181: 
182:         return $this->ext;
183:     }
184: 
185:     /**
186:      * อ่านชื่อไฟล์ (ต้นฉบับ) ของไฟล์ที่อัปโหลด.
187:      *
188:      * @return string|null
189:      */
190:     public function getClientFilename()
191:     {
192:         return $this->name;
193:     }
194: 
195:     /**
196:      * อ่าน MIME Type ของไฟล์.
197:      *
198:      * @return string|null
199:      */
200:     public function getClientMediaType()
201:     {
202:         return $this->mime;
203:     }
204: 
205:     /**
206:      * อ่านข้อผิดพลาดของไฟล์อัปโหลด
207:      * คืนค่า UPLOAD_ERR_XXX.
208:      *
209:      * @return int
210:      */
211:     public function getError()
212:     {
213:         return $this->error;
214:     }
215: 
216:     /**
217:      * อ่านข้อผิดพลาดของไฟล์อัปโหลด เป็นข้อความ
218:      *
219:      * @staticvar array $errors
220:      *
221:      * @return string
222:      */
223:     public function getErrorMessage()
224:     {
225:         switch ($this->error) {
226:             case UPLOAD_ERR_INI_SIZE:
227:                 return sprintf('The file "%s" exceeds your upload_max_filesize ini directive (limit is %s).', $this->getClientFilename(), self::getUploadSize());
228:                 break;
229:             case UPLOAD_ERR_FORM_SIZE:
230:                 return sprintf('The file "%s" exceeds the upload limit defined in your form.', $this->getClientFilename());
231:                 break;
232:             case UPLOAD_ERR_PARTIAL:
233:                 return sprintf('The file "%s" was only partially uploaded.', $this->getClientFilename());
234:                 break;
235:             case UPLOAD_ERR_CANT_WRITE:
236:                 return sprintf('The file "%s" could not be written on disk.', $this->getClientFilename());
237:                 break;
238:             case UPLOAD_ERR_NO_TMP_DIR:
239:                 return 'File could not be uploaded: missing temporary directory.';
240:                 break;
241:             case UPLOAD_ERR_EXTENSION:
242:                 return 'File upload was stopped by a PHP extension.';
243:                 break;
244:             case UPLOAD_ERR_OK:
245:             case UPLOAD_ERR_NO_FILE:
246:                 return null;
247:                 break;
248:             default:
249:                 return sprintf('The file "%s" was not uploaded due to an unknown error.', $this->getClientFilename());
250:                 break;
251:         }
252:     }
253: 
254:     /**
255:      * อ่านขนาดของไฟล์อัปโหลด.
256:      *
257:      * @return int|null
258:      */
259:     public function getSize()
260:     {
261:         return $this->size;
262:     }
263: 
264:     /**
265:      * ส่งออกไฟล์อัปโหลดเป็น Stream.
266:      *
267:      * @throws \RuntimeException ถ้าไม่พบไฟล์
268:      *
269:      * @return StreamInterface
270:      */
271:     public function getStream()
272:     {
273:         if (!is_file($this->tmp_name)) {
274:             throw new \RuntimeException(sprintf(Language::get('Uploaded file %1s has already been moved'), $this->name));
275:         }
276:         if ($this->stream === null) {
277:             $this->stream = new Stream($this->tmp_name);
278:         }
279: 
280:         return $this->stream;
281:     }
282: 
283:     /**
284:      * อ่านไฟล์รวม path จากตัวแปร tmp_name.
285:      *
286:      * @return string|null
287:      */
288:     public function getTempFileName()
289:     {
290:         return $this->tmp_name;
291:     }
292: 
293:     /**
294:      * อ่านการตั้งค่าขนาดของไฟลอัปโหลด.
295:      *
296:      * @param bool $return_byte false (default) คืนค่าเป็นข้อความเช่น 2M, true คืนค่าเป็นตัวเลข (byte)
297:      *
298:      * @return string|int
299:      */
300:     public static function getUploadSize($return_byte = false)
301:     {
302:         $val = trim(ini_get('upload_max_filesize'));
303:         if (is_numeric($val)) {
304:             return $val;
305:         } elseif ($return_byte && preg_match('/^([0-9]+)([gmk])$/', strtolower($val), $match)) {
306:             $units = array('k' => 1024, 'm' => 1048576, 'g' => 1073741824);
307:             $val = (int) $match[1] * $units[$match[2]];
308:         }
309: 
310:         return $val;
311:     }
312: 
313:     /**
314:      * ตรวจสอบว่ามีข้อผิดพลาดการอัปโหลดหรือไม่
315:      * คืนค่า false ถ้าไม่มีไฟล์อัปโหลดหรืออัปโหลดสำเร็จ, คืนค่า true ถ้ามีข้อผิดพลาด.
316:      *
317:      * @return bool
318:      */
319:     public function hasError()
320:     {
321:         if ($this->error === null || $this->error === UPLOAD_ERR_OK || $this->error === UPLOAD_ERR_NO_FILE) {
322:             return false;
323:         }
324: 
325:         return true;
326:     }
327: 
328:     /**
329:      * ตรวจสอบไฟล์อัปโหลด
330:      * คืนค่า true ถ้ามีไฟล์อัปโหลด.
331:      *
332:      * @return bool
333:      */
334:     public function hasUploadFile()
335:     {
336:         return $this->error == UPLOAD_ERR_OK;
337:     }
338: 
339:     /**
340:      * ย้ายไฟล์อัปโหลดไปยังที่อยู่ใหม่
341:      * คืนค่า true ถ้าอัปโหลดเรียบร้อย.
342:      *
343:      * @param string $targetPath ที่อยู่ปลายทางที่ต้องการย้าย
344:      *
345:      * @throws \InvalidArgumentException ข้อผิดพลาดหากที่อยู่ปลายทางไม่สามารถเขียนได้
346:      * @throws \RuntimeException         ข้อผิดพลาดการอัปโหลด
347:      *
348:      * @return bool
349:      */
350:     public function moveTo($targetPath)
351:     {
352:         if ($this->isMoved) {
353:             throw new \RuntimeException(sprintf(Language::get('Uploaded file %1s has already been moved'), $this->name));
354:         }
355:         if (!is_writable(dirname($targetPath))) {
356:             throw new \InvalidArgumentException(Language::get('Target directory is not writable'));
357:         }
358:         if (strpos($targetPath, '://') > 0) {
359:             if (!copy($this->tmp_name, $targetPath)) {
360:                 throw new \RuntimeException(sprintf(Language::get('Error moving uploaded file %1s to %2s'), $this->name, $targetPath));
361:             }
362:             if (!unlink($this->tmp_name)) {
363:                 throw new \RuntimeException(sprintf(Language::get('Error removing uploaded file %1s'), $this->name));
364:             }
365:         } elseif ($this->sapi) {
366:             if (!move_uploaded_file($this->tmp_name, $targetPath)) {
367:                 throw new \RuntimeException(sprintf(Language::get('Error moving uploaded file %1s to %2s'), $this->name, $targetPath));
368:             }
369:         } elseif (copy($this->tmp_name, $targetPath)) {
370:             unlink($this->tmp_name);
371:         } else {
372:             throw new \RuntimeException(sprintf(Language::get('Error moving uploaded file %1s to %2s'), $this->name, $targetPath));
373:         }
374:         $this->isMoved = true;
375: 
376:         return true;
377:     }
378: 
379:     /**
380:      * ปรับขนาดของรูปภาพอัปโหลด โดยรักษาอัตราส่วนของภาพตามความกว้างที่ต้องการ
381:      * หากรูปภาพมีขนาดเล็กกว่าที่กำหนด จะเป็นการ copy file
382:      * หากรูปภาพมีความสูง หรือความกว้างมากกว่า $width
383:      * จะถูกปรับขนาดให้มีขนาดไม่เกิน $width (ทั้งความสูงและความกว้าง)
384:      * และเปลี่ยนชนิดของภาพเป็น jpg
385:      * คืนค่าแอเรย์ [name, width, height, mime] ของรูปภาพปลายทาง หรือ false ถ้าไม่สามารถดำเนินการได้.
386:      *
387:      * @param array  $exts      นามสกุลของไฟล์รูปภาพที่ยอมรับ เช่น [jpg, gif, png]
388:      * @param string $target    path ของไฟล์รูปภาพปลายทาง
389:      * @param string $name      ชื่อไฟล์ของรูปภาพปลายทาง
390:      * @param int    $width     ขนาดสูงสุดของรูปภาพที่ต้องการ
391:      * @param string $watermark (optional) ข้อความลายน้ำ
392:      *
393:      * @return array|bool
394:      */
395:     public function resizeImage($exts, $target, $name, $width, $watermark = '')
396:     {
397:         $this->check($exts, $target);
398:         $ret = Image::resize($this->tmp_name, $target, $name, $width, $watermark);
399:         if ($ret === false) {
400:             throw new \RuntimeException(Language::get('Unable to create image'));
401:         } else {
402:             return $ret;
403:         }
404:     }
405: 
406:     /**
407:      * ตรวจสอบนามสกุลของไฟล์อัปโหลด
408:      * คืนค่า true ถ้านามสกุลของไฟล์อัปโหลดอยู่ใน $exts.
409:      *
410:      * @param array $exts รายการนามสกุลของไฟล์อัปโหลดที่ยอมรับ เช่น [jpg, gif, png]
411:      *
412:      * @return bool
413:      */
414:     public function validFileExt($exts)
415:     {
416:         return in_array($this->getClientFileExt(), $exts);
417:     }
418: 
419:     /**
420:      * ฟังชั่นตรวจสอบไฟล์อัปโหลด
421:      * คืนค่า true ถ้าสามารถอัปโหลดได้.
422:      *
423:      * @param array  $exts      นามสกุลของไฟล์รูปภาพที่ยอมรับ เช่น [jpg, gif, png]
424:      * @param string $targetDir ไดเรคทอรี่ปลายทาง
425:      *
426:      * @throws \RuntimeException         ถ้าชนิดของไฟล์อัปโหลดไม่ถูกต้อง
427:      * @throws \InvalidArgumentException ถ้าไดเร็คทอรี่ไม่สามารถเขียนได้
428:      *
429:      * @return bool
430:      */
431:     private function check($exts, $targetDir)
432:     {
433:         if (!$this->validFileExt($exts)) {
434:             throw new \RuntimeException(Language::get('The type of file is invalid'));
435:         }
436:         if (!is_writable($targetDir)) {
437:             throw new \InvalidArgumentException(Language::get('Target directory is not writable'));
438:         }
439: 
440:         return true;
441:     }
442: }
443: 
Kotchasan API documentation generated by ApiGen