Code / PHP Snippets / Function: do_upload()

  1. function do_upload($upload_dir, $upload_url) {
  2.  
  3.     $temp_name = $_FILES['userfile']['tmp_name'];
  4.     $file_name = $_FILES['userfile']['name'];
  5.   $file_name = str_replace("","",$file_name);
  6.   $file_name = str_replace("'","",$file_name);
  7.     $file_path = $upload_dir.$file_name;
  8.  
  9.     //File Name Check
  10.   if ( $file_name =="") {
  11.       $message = "Invalid File Name Specified";
  12.       return $message;
  13.   }
  14.  
  15.   $result  =  move_uploaded_file($temp_name, $file_path);
  16.   if (!chmod($file_path,0777))
  17.        $message = "change permission to 777 failed.";
  18.   else
  19.     $message = ($result)?"$file_name was uploaded successfully." :
  20.                "Something is wrong with uploading the file.";
  21.   return $message;
  22. }

Download as Text : January 6th, 2009