comparison e2gallerypro/e2upload/Backend/Upload.php @ 3:3f6b44aa6b35 judyates

[svn r4] added ability to buy stuff, from a Prints page, but it doesn't work well with the css, and it also has not been fitted into the perl make system.
author rlm
date Mon, 22 Feb 2010 08:02:39 -0500
parents
children
comparison
equal deleted inserted replaced
2:670229c4eb4b 3:3f6b44aa6b35
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 */
11
12 class Upload {
13
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;
25
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);
34
35 $file = $_FILES[$file];
36
37 if($options['size'] && $file['size']>$options['size'])
38 throw new UploadException('size');
39
40 $pathinfo = pathinfo($file['name']);
41 if($options['extension']) $pathinfo['extension'] = $options['extension'];
42 if(!$pathinfo['extension'])
43 throw new UploadException('extension');
44
45 if(count($options['mimes'])){
46 $mime = self::mime($file['tmp_name'], array(
47 'default' => $file['type'],
48 'extension' => $pathinfo['extension'],
49 ));
50
51 if(!$mime || !in_array($mime, $options['mimes']))
52 throw new UploadException('extension');
53 }
54
55 $file['ext'] = strtolower($pathinfo['extension']);
56 $file['base'] = basename($pathinfo['basename'], '.'.$pathinfo['extension']);
57
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'];
61
62 if(!$options['overwrite'] && file_exists($to))
63 throw new UploadException('exists');
64
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')));
67
68 chmod($to, $options['chmod']);
69
70 return realpath($to);
71 }
72
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 }
82
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);
95
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 }
101
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 }
107
108 if(!$mime && $options['default']) $mime = $options['default'];
109
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');
113
114 if(!empty($mimes[$options['extension']])) return $mimes[$options['extension']];
115 }
116
117 return $mime;
118 }
119
120 }
121
122 class UploadException extends Exception {}