view e2gallerypro/e2upload/Backend/Upload.php @ 10:ed52b2b7cbc1 judyates

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