annotate e2gallerypro/e2upload/Backend/Upload.php @ 8:1b77e8657088 judyates

[svn r9] saving for transference to cd
author rlm
date Fri, 19 Mar 2010 07:17:36 -0400
parents 3f6b44aa6b35
children
rev   line source
rlm@3 1 <?php
rlm@3 2 /**
rlm@3 3 * Styx::Upload - Handles file uploads
rlm@3 4 *
rlm@3 5 * @package Styx
rlm@3 6 * @subpackage Utility
rlm@3 7 *
rlm@3 8 * @license MIT-style License
rlm@3 9 * @author Christoph Pojer <christoph.pojer@gmail.com>
rlm@3 10 */
rlm@3 11
rlm@3 12 class Upload {
rlm@3 13
rlm@3 14 /**
rlm@3 15 * Moves the uploaded file to the specified location. It throws a UploadException
rlm@3 16 * if anything goes wrong except for if the upload does not exist. This can be checked with {@link Upload::exists()}
rlm@3 17 *
rlm@3 18 * @param string $file
rlm@3 19 * @param string $to
rlm@3 20 * @param array $options
rlm@3 21 * @return bool|string Path to moved file or false if the specified upload does not exist
rlm@3 22 */
rlm@3 23 public static function move($file, $to, $options = null){
rlm@3 24 if(!self::exists($file)) return false;
rlm@3 25
rlm@3 26 $options = array_merge(array(
rlm@3 27 'name' => null,
rlm@3 28 'extension' => null,
rlm@3 29 'size' => null,
rlm@3 30 'chmod' => 0777,
rlm@3 31 'overwrite' => false,
rlm@3 32 'mimes' => array(),
rlm@3 33 ), $options);
rlm@3 34
rlm@3 35 $file = $_FILES[$file];
rlm@3 36
rlm@3 37 if($options['size'] && $file['size']>$options['size'])
rlm@3 38 throw new UploadException('size');
rlm@3 39
rlm@3 40 $pathinfo = pathinfo($file['name']);
rlm@3 41 if($options['extension']) $pathinfo['extension'] = $options['extension'];
rlm@3 42 if(!$pathinfo['extension'])
rlm@3 43 throw new UploadException('extension');
rlm@3 44
rlm@3 45 if(count($options['mimes'])){
rlm@3 46 $mime = self::mime($file['tmp_name'], array(
rlm@3 47 'default' => $file['type'],
rlm@3 48 'extension' => $pathinfo['extension'],
rlm@3 49 ));
rlm@3 50
rlm@3 51 if(!$mime || !in_array($mime, $options['mimes']))
rlm@3 52 throw new UploadException('extension');
rlm@3 53 }
rlm@3 54
rlm@3 55 $file['ext'] = strtolower($pathinfo['extension']);
rlm@3 56 $file['base'] = basename($pathinfo['basename'], '.'.$pathinfo['extension']);
rlm@3 57
rlm@3 58 $real = realpath($to);
rlm@3 59 if(!$real) throw new UploadException('path');
rlm@3 60 if(is_dir($real)) $to = $real.'/'.($options['name'] ? $options['name'] : $file['base']).'.'.$file['ext'];
rlm@3 61
rlm@3 62 if(!$options['overwrite'] && file_exists($to))
rlm@3 63 throw new UploadException('exists');
rlm@3 64
rlm@3 65 if(!move_uploaded_file($file['tmp_name'], $to))
rlm@3 66 throw new UploadException(strtolower($_FILES[$file]['error']<=2 ? 'size' : ($_FILES[$file]['error']==3 ? 'partial' : 'nofile')));
rlm@3 67
rlm@3 68 chmod($to, $options['chmod']);
rlm@3 69
rlm@3 70 return realpath($to);
rlm@3 71 }
rlm@3 72
rlm@3 73 /**
rlm@3 74 * Returns whether the Upload exists or not
rlm@3 75 *
rlm@3 76 * @param string $file
rlm@3 77 * @return bool
rlm@3 78 */
rlm@3 79 public function exists($file){
rlm@3 80 return !(empty($_FILES[$file]['name']) || empty($_FILES[$file]['size']));
rlm@3 81 }
rlm@3 82
rlm@3 83 /**
rlm@3 84 * Returns (if possible) the mimetype of the given file
rlm@3 85 *
rlm@3 86 * @param string $file
rlm@3 87 * @param array $options
rlm@3 88 */
rlm@3 89 public function mime($file, $options = array()){
rlm@3 90 $file = realpath($file);
rlm@3 91 $options = array_merge(array(
rlm@3 92 'default' => null,
rlm@3 93 'extension' => strtolower(pathinfo($file, PATHINFO_EXTENSION)),
rlm@3 94 ), $options);
rlm@3 95
rlm@3 96 $mime = null;
rlm@3 97 if(function_exists('finfo_open') && $f = finfo_open(FILEINFO_MIME, getenv('MAGIC'))){
rlm@3 98 $mime = finfo_file($f, $file);
rlm@3 99 finfo_close($f);
rlm@3 100 }
rlm@3 101
rlm@3 102 if(!$mime && in_array($options['extension'], array('gif', 'jpg', 'jpeg', 'png'))){
rlm@3 103 $image = getimagesize($file);
rlm@3 104 if(!empty($image['mime']))
rlm@3 105 $mime = $image['mime'];
rlm@3 106 }
rlm@3 107
rlm@3 108 if(!$mime && $options['default']) $mime = $options['default'];
rlm@3 109
rlm@3 110 if((!$mime || $mime=='application/octet-stream') && $options['extension']){
rlm@3 111 static $mimes;
rlm@3 112 if(!$mimes) $mimes = parse_ini_file(pathinfo(__FILE__, PATHINFO_DIRNAME).'/MimeTypes.ini');
rlm@3 113
rlm@3 114 if(!empty($mimes[$options['extension']])) return $mimes[$options['extension']];
rlm@3 115 }
rlm@3 116
rlm@3 117 return $mime;
rlm@3 118 }
rlm@3 119
rlm@3 120 }
rlm@3 121
rlm@3 122 class UploadException extends Exception {}