| 01 | function upload($file_id, $folder="", $types="") { |
| 02 | if(!$_FILES[$file_id]['name']) |
| 03 | return array('','No file specified'); |
| 04 | $file_title = $_FILES[$file_id]['name']; |
| 05 | //Get file extension |
| 06 | $ext_arr = split("\.",basename($file_title)); |
| 07 | $ext = strtolower($ext_arr[count($ext_arr)-1]); |
| 08 | $filelog = "filename".$ext_arr[0]." ext.".$ext." original".$file_title; |
| 09 | |
| 10 | //Get the last extension |
| 11 | //Not really uniqe - but for all practical reasons, it is |
| 12 | $uniqer = substr(md5(uniqid(rand(),1)),0,5); |
| 13 | //$file_name =$uniqer.'_'.$file_title; |
| 14 | //$file_name =$ext_arr[0].'_'.$uniqer.'.'.$ext; |
| 15 | $file_name =$ext_arr[0].'_'.$uniqer.'.jpg'; |
| 16 | //Get Unique Name |
| 17 | $all_types = explode(",",strtolower($types)); |
| 18 | if($types) { |
| 19 | if(in_array($ext,$all_types)); |
| 20 | else { |
| 21 | $result = "'".$_FILES[$file_id]['name']."' is not a valid file."; |
| 22 | //Show error if any. |
| 23 | return array('',$result); |
| 24 | } |
| 25 | } |
| 26 | //Where the file must be uploaded to |
| 27 | if($folder) |
| 28 | $folder .= '/'; |
| 29 | //Add a '/' at the end of the folder |
| 30 | $uploadfile = $folder . $file_name; |
| 31 | $result = ''; |
| 32 | //Move the file from the stored location to the new location |
| 33 | if (!move_uploaded_file($_FILES[$file_id]['tmp_name'], $uploadfile)) { |
| 34 | $result = "Cannot upload the file '".$_FILES[$file_id]['name']."'"; |
| 35 | //Show error if any. |
| 36 | if(!file_exists($folder)) { |
| 37 | $result .= " : Folder don't exist."; |
| 38 | } |
| 39 | elseif(!is_writable($folder)) { |
| 40 | $result .= " : Folder not writable."; |
| 41 | } |
| 42 | elseif(!is_writable($uploadfile)) { |
| 43 | $result .= " : File not writable."; |
| 44 | } |
| 45 | $file_name = ''; |
| 46 | } |
| 47 | else { |
| 48 | if(!$_FILES[$file_id]['size']) { |
| 49 | //Check if the file is made |
| 50 | @unlink($uploadfile); //Delete the Empty file |
| 51 | $file_name = ''; |
| 52 | $result = "Empty file found - please use a valid file."; //Show the error message |
| 53 | } else { |
| 54 | chmod($uploadfile,0777);//Make it universally writable. |
| 55 | } |
| 56 | } |
| 57 | return array($file_name,$result); |
| 58 | } |
Post a Comment