diff 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
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/e2gallerypro/e2upload/Backend/Upload.php	Mon Feb 22 08:02:39 2010 -0500
     1.3 @@ -0,0 +1,122 @@
     1.4 +<?php
     1.5 +/**
     1.6 + * Styx::Upload - Handles file uploads
     1.7 + *
     1.8 + * @package Styx
     1.9 + * @subpackage Utility
    1.10 + *
    1.11 + * @license MIT-style License
    1.12 + * @author Christoph Pojer <christoph.pojer@gmail.com>
    1.13 + */
    1.14 +
    1.15 +class Upload {
    1.16 +	
    1.17 +	/**
    1.18 +	 * Moves the uploaded file to the specified location. It throws a UploadException
    1.19 +	 * if anything goes wrong except for if the upload does not exist. This can be checked with {@link Upload::exists()}
    1.20 +	 *
    1.21 +	 * @param string $file
    1.22 +	 * @param string $to
    1.23 +	 * @param array $options
    1.24 +	 * @return bool|string Path to moved file or false if the specified upload does not exist
    1.25 +	 */
    1.26 +	public static function move($file, $to, $options = null){
    1.27 +		if(!self::exists($file)) return false;
    1.28 +		
    1.29 +		$options = array_merge(array(
    1.30 +			'name' => null,
    1.31 +			'extension' => null,
    1.32 +			'size' => null,
    1.33 +			'chmod' => 0777,
    1.34 +			'overwrite' => false,
    1.35 +			'mimes' => array(),
    1.36 +		), $options);
    1.37 +		
    1.38 +		$file = $_FILES[$file];
    1.39 +		
    1.40 +		if($options['size'] && $file['size']>$options['size'])
    1.41 +			throw new UploadException('size');
    1.42 +		
    1.43 +		$pathinfo = pathinfo($file['name']);
    1.44 +		if($options['extension']) $pathinfo['extension'] = $options['extension'];
    1.45 +		if(!$pathinfo['extension'])
    1.46 +			throw new UploadException('extension');
    1.47 +		
    1.48 +		if(count($options['mimes'])){
    1.49 +			$mime = self::mime($file['tmp_name'], array(
    1.50 +				'default' => $file['type'],
    1.51 +				'extension' => $pathinfo['extension'],
    1.52 +			));
    1.53 +			
    1.54 +			if(!$mime || !in_array($mime, $options['mimes']))
    1.55 +				throw new UploadException('extension');
    1.56 +		}
    1.57 +		
    1.58 +		$file['ext'] = strtolower($pathinfo['extension']);
    1.59 +		$file['base'] = basename($pathinfo['basename'], '.'.$pathinfo['extension']);
    1.60 +		
    1.61 +		$real = realpath($to);
    1.62 +		if(!$real) throw new UploadException('path');
    1.63 +		if(is_dir($real)) $to = $real.'/'.($options['name'] ? $options['name'] : $file['base']).'.'.$file['ext'];
    1.64 +		
    1.65 +		if(!$options['overwrite'] && file_exists($to))
    1.66 +			throw new UploadException('exists');
    1.67 +		
    1.68 +		if(!move_uploaded_file($file['tmp_name'], $to))
    1.69 +			throw new UploadException(strtolower($_FILES[$file]['error']<=2 ? 'size' : ($_FILES[$file]['error']==3 ? 'partial' : 'nofile')));
    1.70 +		
    1.71 +		chmod($to, $options['chmod']);
    1.72 +		
    1.73 +		return realpath($to);
    1.74 +	}
    1.75 +	
    1.76 +	/**
    1.77 +	 * Returns whether the Upload exists or not
    1.78 +	 *
    1.79 +	 * @param string $file
    1.80 +	 * @return bool
    1.81 +	 */
    1.82 +	public function exists($file){
    1.83 +		return !(empty($_FILES[$file]['name']) || empty($_FILES[$file]['size']));
    1.84 +	}
    1.85 +	
    1.86 +	/**
    1.87 +	 * Returns (if possible) the mimetype of the given file
    1.88 +	 *
    1.89 +	 * @param string $file
    1.90 +	 * @param array $options
    1.91 +	 */
    1.92 +	public function mime($file, $options = array()){
    1.93 +		$file = realpath($file);
    1.94 +		$options = array_merge(array(
    1.95 +			'default' => null,
    1.96 +			'extension' => strtolower(pathinfo($file, PATHINFO_EXTENSION)),
    1.97 +		), $options);
    1.98 +		
    1.99 +		$mime = null;
   1.100 +		if(function_exists('finfo_open') && $f = finfo_open(FILEINFO_MIME, getenv('MAGIC'))){
   1.101 +			$mime = finfo_file($f, $file);
   1.102 +			finfo_close($f);
   1.103 +		}
   1.104 +		
   1.105 +		if(!$mime && in_array($options['extension'], array('gif', 'jpg', 'jpeg', 'png'))){
   1.106 +			$image = getimagesize($file);
   1.107 +			if(!empty($image['mime']))
   1.108 +				$mime = $image['mime'];
   1.109 +		}
   1.110 +		
   1.111 +		if(!$mime && $options['default']) $mime = $options['default'];
   1.112 +		
   1.113 +		if((!$mime || $mime=='application/octet-stream') && $options['extension']){
   1.114 +			static $mimes;
   1.115 +			if(!$mimes) $mimes = parse_ini_file(pathinfo(__FILE__, PATHINFO_DIRNAME).'/MimeTypes.ini');
   1.116 +			
   1.117 +			if(!empty($mimes[$options['extension']])) return $mimes[$options['extension']];
   1.118 +		}
   1.119 +		
   1.120 +		return $mime;
   1.121 +	}
   1.122 +	
   1.123 +}
   1.124 +
   1.125 +class UploadException extends Exception {}
   1.126 \ No newline at end of file