"No errors.", UPLOAD_ERR_INI_SIZE => "The uploaded file exceeds the upload_max_filesize directive in php.ini", UPLOAD_ERR_FORM_SIZE => "Larger than form MAX_FILE_SIZE.", UPLOAD_ERR_PARTIAL => "Partial upload.", UPLOAD_ERR_NO_FILE => "No file.", UPLOAD_ERR_NO_TMP_DIR => "No temporary directory.", UPLOAD_ERR_CANT_WRITE => "Can't write to disk.", UPLOAD_ERR_EXTENSION => "File upload stopped by extension." ); public $mail_receiver = ''; public $finish_function =''; public $cross_origin = false; function __construct($deny_ext=array()) { //set data from JAVASCRIPT if(isset($_REQUEST['ax-max-file-size'])) $this->setMaxFileSize($_REQUEST['ax-max-file-size']); if(isset($_REQUEST['ax-file-path'])) $this->setUploadPath($_REQUEST['ax-file-path']); if(isset($_REQUEST['ax-allow-ext'])) $this->setAllowExt( !empty($_REQUEST['ax-allow-ext']) ? explode('|', $_REQUEST['ax-allow-ext']): array() ); if(isset($_REQUEST['ax-override'])) $this->setOverride(true); //set deny $this->deny_ext = $deny_ext; //active parameters neccessary for upload $this->file_name = isset($_REQUEST['ax-file-name']) ? $_REQUEST['ax-file-name']:$_FILES['ax_file_input']['name']; $this->file_size = isset($_REQUEST['ax-file-size']) ? $_REQUEST['ax-file-size']:$_FILES['ax_file_input']['size']; //create a temp folder for uploading the chunks $ini_val = @ini_get('upload_tmp_dir'); $this->temp_path = $ini_val ? $ini_val : sys_get_temp_dir(); $this->temp_path = $this->temp_path.DIRECTORY_SEPARATOR; //$this->makeDir($this->temp_path); } /** * Set the maximum file size, expected string with byte notation * @param string $max_file_size */ public function setMaxFileSize($max_file_size = '10M') { $this->max_file_size = $max_file_size; } /** * Set the allow extension file to upload * @param array $allow_ext */ public function setAllowExt($allow_ext=array()) { $this->allow_ext = $allow_ext; } /** * Set the upload poath as string * @param string $upload_path */ public function setUploadPath($upload_path) { $upload_path = rtrim($upload_path, '\\/'); $this->upload_path = $upload_path.DIRECTORY_SEPARATOR; // Create thumb path if do not exits $this->makeDir($this->upload_path); } public function setOverride($bool){ $this->override=$bool; } private function makeDir($dir) { // Create thumb path if do not exits if(!file_exists($dir) && !empty($dir)) { $done = @mkdir($dir, 0777, true); if(!$done) { $this->message(-1, 'Cannot create upload folder'); } } } //Create a image thumb private function createThumbGD($quality=75) { //Settings for thumbnail generation, can be changed here or from js $maxheight = isset($_REQUEST['ax-thumbHeight'])?$_REQUEST['ax-thumbHeight']:0; $maxwidth = isset($_REQUEST['ax-thumbWidth'])?$_REQUEST['ax-thumbWidth']:0; $postfix = isset($_REQUEST['ax-thumbPostfix'])?$_REQUEST['ax-thumbPostfix']:'_thumb'; $thumb_path = isset($_REQUEST['ax-thumbPath'])?$_REQUEST['ax-thumbPath']:''; $format = isset($_REQUEST['ax-thumbFormat'])?$_REQUEST['ax-thumbFormat']:'png'; $filepath = $this->upload_path.$this->file_name; if( ($maxwidth<=0 && $maxheight<=0) || !is_numeric($maxwidth) || !is_numeric($maxheight) ) { return 'No valid width and height given'; } $web_formats= array('jpg','jpeg','png','gif');//web formats $file_name = pathinfo($filepath); if(empty($format)) $format = $file_name['extension']; if(!in_array(strtolower($file_name['extension']), $web_formats)) { return 'Not supported file type'; } $thumb_name = $file_name['filename'].$postfix.'.'.$format;//filename 5.2++ if(empty($thumb_path)) $thumb_path=$file_name['dirname']; $thumb_path.= (!in_array(substr($thumb_path, -1), array('\\','/') ) )?DIRECTORY_SEPARATOR:'';//normalize path if(!file_exists($thumb_path) && !empty($thumb_path)) { @mkdir($thumb_path, 0777, true); } // Get new dimensions list($width_orig, $height_orig) = getimagesize($filepath); if($width_orig>0 && $height_orig>0) { $ratioX = $maxwidth/$width_orig; $ratioY = $maxheight/$height_orig; $ratio = min($ratioX, $ratioY); $ratio = ($ratio==0)?max($ratioX, $ratioY):$ratio; $newW = $width_orig*$ratio; $newH = $height_orig*$ratio; // Resample $thumb = imagecreatetruecolor($newW, $newH); $image = imagecreatefromstring(file_get_contents($filepath)); imagecopyresampled($thumb, $image, 0, 0, 0, 0, $newW, $newH, $width_orig, $height_orig); // Output switch (strtolower($format)) { case 'png': imagepng($thumb, $thumb_path.$thumb_name, 9); break; case 'gif': imagegif($thumb, $thumb_path.$thumb_name); break; default: imagejpeg($thumb, $thumb_path.$thumb_name, $quality);; break; } imagedestroy($image); imagedestroy($thumb); } else { return false; } } //Check if file size is allowed private function checkSize() { //------------------max file size check from js $max_file_size = $this->max_file_size; $size = $this->file_size; $rang = substr($max_file_size,-1); $max_size = !is_numeric($rang) && !is_numeric($max_file_size)? str_replace($rang, '', $max_file_size): $max_file_size; if($rang && $max_size) { switch (strtoupper($rang))//1024 or 1000?? { case 'Y': $max_size = $max_size*1024;//Yotta byte, will arrive such day??? case 'Z': $max_size = $max_size*1024; case 'E': $max_size = $max_size*1024; case 'P': $max_size = $max_size*1024; case 'T': $max_size = $max_size*1024; case 'G': $max_size = $max_size*1024; case 'M': $max_size = $max_size*1024; case 'K': $max_size = $max_size*1024; } } if(!empty($max_file_size) && $size>$max_size) { return false; } //-----------------End max file size check return true; } //Check if file name is allowed and remove illegal windows chars private function checkName() { //comment if not using windows web server $windowsReserved = array('CON', 'PRN', 'AUX', 'NUL','COM1', 'COM2', 'COM3', 'COM4', 'COM5', 'COM6', 'COM7', 'COM8', 'COM9', 'LPT1', 'LPT2', 'LPT3', 'LPT4', 'LPT5', 'LPT6', 'LPT7', 'LPT8', 'LPT9'); $badWinChars = array_merge(array_map('chr', range(0,31)), array("<", ">", ":", '"', "/", "\\", "|", "?", "*")); $this->file_name = str_replace($badWinChars, '', $this->file_name); //check if legal windows file name if(in_array($this->file_name, $windowsReserved)) { return false; } return true; } /** * Check if a file exits or not and calculates a new name for not oovverring other files * @param string $upload_path */ private function checkFileExits($upload_path='') { if($upload_path=='') $upload_path = $this->upload_path; if(!$this->override) { usleep(rand(100, 900)); $filename = $this->file_name; //$upload_path = $this->upload_path; $file_data = pathinfo($filename); $file_base = $file_data['filename']; $file_ext = $file_data['extension'];//PHP 5.2> //Disable this lines of code to allow file override $c=0; while(file_exists($upload_path.$filename)) { $find = preg_match('/\((.*?)\)/', $filename, $match); if(!$find) $match[1] = 0; else $file_base = str_replace("(".$match[1].")", "", $file_base); $match[1]++; $filename = $file_base."(".$match[1].").".$file_ext; } // end $this->file_name = $filename; } } public function _checkFileExists() { $filename = $this->file_name; $upload_path = $this->upload_path; return file_exists($upload_path.$filename); } public function deleteFile(){ $del = @unlink($this->upload_path.$this->file_name); return $del; } //Check if file type is allowed for upload private function checkExt() { $file_ext = strtolower( pathinfo($this->file_name, PATHINFO_EXTENSION) ); //extensions not allowed for security reason and check if is allowed extension if(in_array($file_ext, $this->deny_ext) || (!in_array($file_ext, $this->allow_ext) && count($this->allow_ext)) ) { return false; } return true; } // Simle email sender function public function setEmail($main_receiver, $from='ajax@uploader') { $this->mail_receiver = $main_receiver; $this->mail_cc = ''; $this->mail_from = $from ? $from : 'ajax@uploader'; } private function sendEmail() { if($this->mail_receiver) { $msg = '
New file uploaded to your site at '.date('Y-m-i H:i'). ' from IP '.$_SERVER['REMOTE_ADDR'].':
'; $msg.= '