Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.97 KB | None | 0 0
  1. function upload($file_id, $folder="", $types="") {
  2. if(!$_FILES[$file_id]['name']) return array('','No file specified');
  3.  
  4. $file_title = $_FILES[$file_id]['name'];
  5. //Get file extension
  6. $ext_arr = explode(".",basename($file_title));
  7. $ext = strtolower($ext_arr[count($ext_arr)-1]); //Get the last extension
  8.  
  9. //Not really uniqe - but for all practical reasons, it is
  10. //Change the rules to create file name here
  11. //$uniqer = substr(md5(uniqid(rand(),1)),0,5);
  12. //$file_name = $uniqer . '_' . $file_title;//Get Unique Name
  13.  
  14. $file_name = date('YmdHis').rand(999,9999).'-DP.jpg';
  15.  
  16. $all_types = explode(",",strtolower($types));
  17. if($types) {
  18. if(in_array($ext,$all_types));
  19. else {
  20. $result = "'".$_FILES[$file_id]['name']."' is not a valid file."; //Show error if any.
  21. return array('',$result);
  22. }
  23. }
  24.  
  25. //Where the file must be uploaded to
  26. if($folder) $folder .= '/';//Add a '/' at the end of the folder
  27. $uploadfile = $folder . $file_name;
  28.  
  29. $result = '';
  30. //Move the file from the stored location to the new location
  31. if (!move_uploaded_file($_FILES[$file_id]['tmp_name'], $uploadfile)) {
  32. $result = "Cannot upload the file '".$_FILES[$file_id]['name']."'"; //Show error if any.
  33. if(!file_exists($folder)) {
  34. $result .= " : Folder don't exist.";
  35. } elseif(!is_writable($folder)) {
  36. $result .= " : Folder not writable.";
  37. } elseif(!is_writable($uploadfile)) {
  38. $result .= " : File not writable.";
  39. }
  40. $file_name = '';
  41.  
  42. } else {
  43. if(!$_FILES[$file_id]['size']) { //Check if the file is made
  44. @unlink($uploadfile);//Delete the Empty file
  45. $file_name = '';
  46. $result = "Empty file found - please use a valid file."; //Show the error message
  47. } else {
  48. chmod($uploadfile,0777);//Make it universally writable.
  49. }
  50. }
  51.  
  52. return array($file_name,$result);
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement