upload.lib.php
上传用户:jiangbw
上传日期:2022-03-16
资源大小:49k
文件大小:15k
- <?
- /*======================================================================*
- || #################################################################### ||
- || # BUILD UNDER PHP SCRIPTNET 3.2.1 FRAMEWORK ||
- || # ---------------------------------------------------------------- # ||
- || # Code2Art Open Source Software. All Rights Reserved. ||
- || # This file should be redistributed in whole or significant part. # ||
- || # ------------------ SCRIPTNET IS FREE SOFTWARE ------------------ # ||
- || # http://www.code2art.com | http://www.code2art.com/scriptnet # ||
- || # Copyleft by Benediktus Ardian Hersanto, SE # ||
- || # Sorry if this framework is still unavailable for public # ||
- || # because I still searching on my own head. # ||
- || #################################################################### ||
- *======================================================================*/
- //
- // This library must be use under SCRIPTNET Framework or part of its constant's value cannot be
- // recognized, or you can force yourself to modify this class its all up to you
- //
- // Warning :
- // This library and the associated files are non commercial, non professional
- // work.
- // It should not have unexpected results. However if any damage is caused by
- // this software the author can not be responsible.
- // The use of this software is at the risk of the user.
- //
- // --------------------------------------------------------------------------------
- // Free to use to everyone who understand PHP
- // --------------------------------------------------------------------------------
- // author : Benediktus Ardian Hersanto, SE
- // email : ardie_b@yahoo.com
- //
- ////////////////////////////////////////////////////////////////////////////////////
- // NOTES on PHP.INI open_basedir restriction
- //
- // if your hosting company enabling open_basedir restriction based on your host dir
- // and you still can not upload, please check or ask for apache configuration http.conf
- // to be set on your virtual host directory (open_basedir) and (upload_tmp_dir) value
- //
- // <Directory "/path/of/your/virtual/host/account/">
- // Options Indexes FollowSymLinks MultiViews
- // php_admin_value open_basedir "/path/of/your/virtual/host/account/"
- // php_admin_value upload_tmp_dir "/path/of/your/virtual/host/account/tmp/"
- // AllowOverride None
- // Order allow,deny
- // Allow from all
- // </Directory>
- //
- // you have to create /tmp directory based on upload_tmp_dir value on your account root dir
- // and chmod to 0777
- class upload {
- // MIME Type Definition
- var $image = array("image/gif","image/jpeg","image/pjpeg");
- var $text = array("text/plain","text/html","text/richtext","text/xml","text/css");
- var $doc = array("application/msword","application/pdf","application/rtf","application/vnd.ms-excel");
- var $zip = array("application/x-zip-compressed","application/x-compress","application/x-compressed","application/x-tar","application/x-gzip","application/x-gtar","application/mac-binhex40");
- var $mpeg = array("video/mpeg","video/quicktime","video/x-msvideo");
- var $audio = array("audio/mpeg","audio/wav");
- var $files = array();
- var $file_types = array();
- var $count = 0;
- var $debug = false;
- var $lowercase = false;
- var $LOG_ERROR = true;
- var $SHOW_ERROR = true;
- var $error_log = "/logs/upload_logs.log";
- var $target_dir = "";
- var $version = "File_Upload_16.01.06";
-
-
- function upload($target_dir='',$force_create=false) {
- $this->_FILES = (PHPVERSION<4.10)?$GLOBALS['HTTP_POST_FILES']:$_FILES;
- $this->_ENV = (isset($_ENV))?$_ENV:$GLOBALS['HTTP_ENV_VARS'];
- $this->_SERVER = (isset($_SERVER))?$_SERVER:$GLOBALS['HTTP_SERVER_VARS'];
- if(count($this->_FILES)>0) {
- while(list($name,$info)=each($this->_FILES)) {
- if($info['error']=='0') {
- $this->files[] = $target_dir.$info['name'];
- $this->file_types[] = array_pop(explode('.',$info['name']));
- }
- }
- }
- $this->count = count($this->_FILES);
- if(!empty($target_dir)) $this->target_dir = $target_dir;
- if($force_create) define('UPLOAD_FORCE_CREATE_DIR',true);
- if(defined('AUTOSAVE')&&constant('AUTOSAVE')==true) $this->SaveFile();
- }
-
- function RaiseError($msg,$type=E_USER_WARNING) {
- if($this->LOG_ERROR) {
- $date = getdate();
- $REMOTE_ADDRESS = getenv('REMOTE_ADDR');
- error_log("[{$date['weekday']} {$date['month']} {$date['mday']} {$date['hours']}:{$date['minutes']}:{$date['seconds']} {$date['year']}] [client {$REMOTE_ADDRESS}] [Error : {$msg}]rn", 3, $this->error_log);
- }
- if($this->SHOW_ERROR) trigger_error($msg,$type);
- }
-
- function GetMimeType() {
- while(list($key,$array)=each($this->_FILES)) {
- if(is_array($array)&&count($array)>0) {
- $result[$key] = $array['type'];
- } else break;
- }
- if(isset($result)) return $result;
- else return NULL;
- }
-
- function SaveFile($form_input_name="",$dir="./") {
- if(empty($form_input_name)) {
- if(!empty($this->target_dir)) $dir = $this->target_dir;
- else trigger_error('Target directory to save uploaded file was not defined',E_USER_WARNING);
- $pos = strrpos($dir, "/");
- if(!is_dir($dir)) {
- if(defined('UPLOAD_FORCE_CREATE_DIR')) {
- if(mkdir($dir,0777)) if($pos==false||$pos<(strlen($dir)-1)) $dir .= "/";
- else {
- if($this->debug) echo 'Error on '.__FILE__.' line '.__LINE__.'<BR>';
- $this->RaiseError('upload<b>::</b>SaveFile() failed to save file from stream<b>:</b> permission denied for system to copy file');
- return false;
- }
- } else {
- if($this->debug) echo 'Error on '.__FILE__.' line '.__LINE__.'<BR>';
- $this->RaiseError('upload<b>::</b>SaveFile() failed to save file from stream<b>:</b> destination directory was not exists');
- return false;
- }
- } else {
- if ($dirHwnd = opendir($dir)) closedir($dirHwnd);
- else {
- if($this->debug) echo 'Error on '.__FILE__.' line '.__LINE__.'<BR>';
- $this->RaiseError('upload<b>::</b>SaveFile() failed to save file from stream<b>:</b> permission denied for system to copy file');
- return false;
- }
- if ($pos==false||$pos==(strlen($dir)-1)) $dir .= "/";
- }
- if($this->count>0) {
- while(list($key,$array)=each($this->_FILES)) {
- if(trim($array['error'])=='0') {
- if(file_exists($array['tmp_name'])) {
- if($this->lowercase) {
- if(move_uploaded_file($array['tmp_name'],$dir.strtolower($this->_FILES[$key]['name']))) {
- if(chmod($dir.strtolower($this->_FILES[$key]['name']),0777))
- return true;
- else {
- if($this->debug) echo 'Error on '.__FILE__.' line '.__LINE__.'<BR>';
- return false;
- }
- } else {
- if($this->debug) echo 'Error on '.__FILE__.' line '.__LINE__.'<BR>';
- return false;
- }
- } else {
- //$bytes = @file_put_contents($dir.$this->_FILES[$key]['name'],file_get_contents($array['tmp_name']));
- if(move_uploaded_file($array['tmp_name'],$dir.$this->_FILES[$key]['name'])) {
- if(chmod($dir.strtolower($this->_FILES[$key]['name']),0777))
- return true;
- else {
- if($this->debug) echo 'Error on '.__FILE__.' line '.__LINE__.'<BR>';
- return false;
- }
- } else return false;
- }
- //if($bytes==0) copy($array['tmp_name'],$dir.$this->_FILES[$key]['name']);
- } else {
- if($this->debug) echo 'Error on '.__FILE__.' line '.__LINE__.'<BR>';
- $this->RaiseError('upload<b>::</b>SaveFile(''.$dir.$this->_FILES[$key]['name'].'') failed to save file from stream<b>:</b> No such valid source file', E_USER_NOTICE);
- return false;
- }
- } else {
-
- $this->error[$key] = $this->_getErrorDescription($array['error']);
- }
- }
- } else echo 'here';
- } else {
- if(trim($this->_FILES[$form_input_name]['error'])!=0){ $this->RaiseError('No file was uploaded'); return; }
- $pos = strrpos($dir, "/");
- if($pos<(strlen($dir)-1)) $dir .= "/";
- if(!is_dir($dir)) {
- if($pos!=false) {
- $dirs = explode("/",$dir);
- for($i=0;$i<count($dirs);$i++) {
- if($dirs[$i]=='..') {
- $upper[] = $dirs[$i];
- } else {
- if(isset($upper)) {
- if(count($upper)>1) {
- if(!is_dir(implode("/",$upper)."/".$dirs[$i])) mkdir(implode("/",$upper)."/".$dirs[$i],0777);
- $upper[] = $dirs[$i];
- } else if(count($upper)==1) {
- if(!is_dir(implode("/",$upper)."/".$dirs[$i])) mkdir($upper[0]."/".$dirs[$i],0777);
- $upper[] = $dirs[$i];
- }
- }
- }
- }
- }
- } else {
- if($this->debug) echo 'Error on '.__FILE__.' line '.__LINE__.'<BR>';
- }
- if(file_exists($this->_FILES[$form_input_name]['tmp_name'])) {
- if($this->lowercase) {
- if(move_uploaded_file($this->_FILES[$form_input_name]['tmp_name'],$dir.strtolower($this->_FILES[$form_input_name]['name']))) {
- if(chmod($dir.strtolower($this->_FILES[$form_input_name]['name']),0777))
- return true;
- else {
- if($this->debug) echo 'Error on '.__FILE__.' line '.__LINE__.'<BR>';
- return false;
- }
- } else return false;
- } else {
- if(move_uploaded_file($this->_FILES[$form_input_name]['tmp_name'],$dir.$this->_FILES[$form_input_name]['name'])) {
- if(chmod($dir.strtolower($this->_FILES[$form_input_name]['name']),0777))
- return true;
- else {
- if($this->debug) echo 'Error on '.__FILE__.' line '.__LINE__.'<BR>';
- return false;
- }
- } else return false;
- }
- } else {
- if($this->debug) echo 'Error on '.__FILE__.' line '.__LINE__.'<BR>';
- $this->RaiseError('upload<b>::</b>SaveFile(''.$dir.$this->_FILES[$form_input_name]['name'].'') failed to save file from stream<b>:</b> No such valid source file', E_USER_NOTICE);
- return false;
- }
- }
- }
-
- /* Use alternate path for saving file
- for instance : ./filename.txt => to save to current directory;
- ../dir/filename.txt => to save to directory "dir" on same level */
- function SaveAs($form_input_name,$filename) {
- if(!isset($this->_FILES[$form_input_name]['tmp_name'])){ $this->RaiseError('No file was uploaded'); return; }
- $pos = strrpos($filename, "/");
- if ($pos==false) {
- $pos = strrpos($this->target_dir, "/");
- if ($pos==false||($pos<(strlen($this->target_dir)-1))) $filename = $this->target_dir."/".$filename;
- else if($pos==(strlen($this->target_dir)-1)) $filename = $this->target_dir.$filename;
- } else {
- if(dirname($filename)!=$filename&&dirname($filename)!='..') {
- $dirs = explode("/",dirname($filename));
- for($i=0;$i<count($dirs);$i++) {
- if($dirs[$i]=='..') {
- $upper[] = $dirs[$i];
- } else {
- if(isset($upper)) {
- if(count($upper)>1) {
- if(!is_dir(implode("/",$upper)."/".$dirs[$i])) mkdir(implode("/",$upper)."/".$dirs[$i],0777);
- $upper[] = $dirs[$i];
- } else if(count($upper)==1) {
- if(!is_dir(implode("/",$upper)."/".$dirs[$i])) mkdir($upper[0]."/".$dirs[$i],0777);
- $upper[] = $dirs[$i];
- }
- }
- }
- }
- }
- }
- if(file_exists($this->_FILES[$form_input_name]['tmp_name'])) {
- if($this->lowercase) file_put_contents(strtolower($filename),file_get_contents($this->_FILES[$form_input_name]['tmp_name'])); else
- file_put_contents($filename,file_get_contents($this->_FILES[$form_input_name]['tmp_name']));
- } else {
- if($this->debug) echo 'Error on '.__FILE__.' line '.__LINE__.'<BR>';
- $this->RaiseError('upload<b>::</b>SaveFile(''.$dir.$this->_FILES[$form_input_name]['name'].'') failed to save file from stream<b>:</b> No such valid source file', E_USER_NOTICE);
- }
- }
-
- function _getErrorDescription($n) {
- switch ($n) {
- case 0: return 'There is no error, the file was uploaded successfully';
- break;
- case 1: return 'The uploaded file exceeds the upload_max_filesize directive in php.ini';
- break;
- case 2: return 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form';
- break;
- case 3: return 'The uploaded file was only partially uploaded';
- break;
- case 4: return 'No file was uploaded';
- break;
- }
- }
- }
- if (!function_exists('file_get_contents')) {
- function file_get_contents($filename, $incpath = false, $resource_context = null)
- {
- if (false === $fh = fopen($filename, 'rb', $incpath)) {
- trigger_error('file_get_contents() failed to open stream<b>:</b> No such file or directory', E_USER_WARNING);
- return false;
- }
- clearstatcache();
- if ($fsize = @filesize($filename)) {
- $data = fread($fh, $fsize);
- } else {
- $data = '';
- while (!feof($fh)) {
- $data .= fread($fh, 8192);
- }
- }
- fclose($fh);
- return $data;
- }
- }
- if (!function_exists('file_put_contents')) {
- define('FILE_APPEND',1);
- define('FILE_USE_INCLUDE_PATH',2);
- function file_put_contents($filename, $content, $flags = null, $resource_context = null)
- {
- // If $content is an array, convert it to a string
- if (is_array($content)) {
- $content = implode('', $content);
- }
- // If we don't have a string, throw an error
- if (!is_scalar($content)) {
- trigger_error('file_put_contents() The 2nd parameter should be either a string or an array', E_USER_WARNING);
- return false;
- }
- // Get the length of date to write
- $length = strlen($content);
- // Check what mode we are using
- $mode = ($flags & FILE_APPEND) ?
- $mode = 'a' :
- $mode = 'w';
- // Check if we're using the include path
- $use_inc_path = ($flags & FILE_USE_INCLUDE_PATH) ?
- true :
- false;
- // Open the file for writing
- if (($fh = @fopen($filename, $mode, $use_inc_path)) === false) {
- trigger_error('file_put_contents() failed to open stream<b>:</b> Permission denied', E_USER_WARNING);
- return false;
- }
- // Write to the file
- $bytes = 0;
- if (($bytes = @fwrite($fh, $content)) === false) {
- $errormsg = sprintf('file_put_contents() Failed to write %d bytes to %s',
- $length,
- $filename);
- trigger_error($errormsg, E_USER_WARNING);
- return false;
- }
- // Close the handle
- @fclose($fh);
- // Check all the data was written
- if ($bytes != $length) {
- $errormsg = sprintf('file_put_contents() Only %d of %d bytes written, possibly out of free disk space.',
- $bytes,
- $length);
- trigger_error($errormsg, E_USER_WARNING);
- return false;
- }
- // Return length
- return $bytes;
- }
- }
- ?>