Volatil3
Volatil3

Reputation: 14978

is_uploaded_file says error but no error code returns

I have the following code:

 if (!is_uploaded_file($_FILES['sb_file']['tmp_name']))
                           {
                               echo "Error in Uploading File <br>".$_FILES["sb_file"]["error"][$count];
                           }

is_uploaded_file returns false , even $_FILES['sb_file']['errors'] return 0 but file is not moving in destination folder. How do I trace error?

foreach ($_FILES['sb_file']['name'] as $filename)
        {
            //make sure no empty thing goes
            if($filename!=""&&$filename!=null)
            {
                $temp = explode(".", $filename);
                $extension = end($temp);
                $upload_folder = "/home/content/w/k/1/wk1989/html/".trim($config["UPLOAD_FOLDER"]);
                 if(!(in_array($extension, $allowedExts)))
                    print "$filename has Invalid file Type <br />";
                 else
                 {
                     $uploaded_file_name = $filename."-".date("YmdHis").".".$extension;
                     if ($_FILES["sb_file"]["error"][$count] > 0)
                        {
                            echo "<br />Error: " . $_FILES["sb_file"]["error"][$count] . "<br />";
                        }
                        else
                        {
                            $target_path = "uploads/";
                            $target_path = $target_path.basename( $file_upload['name']); 
                            print "Path = $target_path <br>";
//                            if(move_uploaded_file($file_upload['tmp_name'], $target_path)) {
//                            //    echo "The file ".  basename( $file_upload['name']). " has been uploaded";
//                            } else{
//                                //echo "There was an error uploading the file, please try again!";
//                            }
                           move_uploaded_file($_FILES["sb_file"]["tmp_name"][$count],$upload_folder.$uploaded_file_name);
//                           if (!is_uploaded_file($_FILES['sb_file']['tmp_name'][$count]))
//                           {
//                               echo "Error in Uploading File <br>".$_FILES["sb_file"]["error"][$count];
//                           }
//                           else
//                           {
//                               
//                           }
                           $is_upload = true;
                           echo "Uploaded = ".$upload_folder.$uploaded_file_name;

                        }
                        if($is_upload)
                        {
                            $uploaded_files[] = $upload_folder.$uploaded_file_name;
                        }
                 } 
            }
             $count++;
        }

Upvotes: 0

Views: 2453

Answers (1)

Radon8472
Radon8472

Reputation: 4931

First I would suggest you to use move_uploaded_file instead of moving the file manually. To debug you current code you should do a print_r($_FILES) and check if the path is correct and what informations about the file are delivered to you script.

I found a post say the function is_uploaded_file handles the filename case-sensitive (even on windows) (read the original post).

I think the best way for handling an uploaded file is somethink like this:

$upload_dir = "D:/user_uploads/";
foreach($_FILES as $key => $file_arr)
{
  switch($file_arr["error"])
  {
    case UPLOAD_ERR_OK:
    {
      if($file_arr["size"]>0)
      { 
        $result = move_uploaded_file($file_arr["tmp_name"], $upload_dir.$file_arr["name"]);
        if($result === true) echo "Upload ok";
        else                 echo "Upload failed";
      }
      else echo "The file has no content";
    }
    default: 
    {
      echo "Upload Error #".$file_arr["error"]." for field" . $key;
      print_r($files_arr); // only for debuging, remove this on live systems
    }
  }
}

If you need, you can add more case-blocks for e.g. UPLOAD_ERR_FORM_SIZE. And on Unix/linux or ntfs drives you should check if the upload_dir is_writeable().

Upvotes: 1

Related Questions