move_uploaded_file failure

前端 未结 10 1078
挽巷
挽巷 2020-12-10 13:25

Can someone help me understand why this is returning false?

if ((move_uploaded_file($_FILES[$field][\'tmp_name\'], $path))) {

As in, potent

相关标签:
10条回答
  • 2020-12-10 13:56

    Loïc Février, thank you, you've saved me a lot of time!

    Here is my part... printing possible errors during upload.

    First create directory Uploader/UploadedFiles Then use code below...

    $destination=$_SERVER[DOCUMENT_ROOT]."/Uploader/UploadedFiles/" . $_FILES["file"]["name"];
    
    if(move_uploaded_file($_FILES["file"]["tmp_name"],  $destination)){
       echo ("Stored in".$_SERVER[DOCUMENT_ROOT]."/Uploader/UploadedFiles/".$_FILES["file"]["name"]);
    }else{
       $html_body = '<h1>File upload error!</h1>';
       switch ($_FILES[0]['error']) {
       case 1:
          $html_body .= 'The file is bigger than this PHP installation allows';
          break;
       case 2:
          $html_body .= 'The file is bigger than this form allows';
          break;
       case 3:
          $html_body .= 'Only part of the file was uploaded';
          break;
       case 4:
          $html_body .= 'No file was uploaded';
          break;
       default:
          $html_body .= 'unknown errror';
       } 
       echo ($html_body);
    }
    
    0 讨论(0)
  • 2020-12-10 13:56

    Check:

    enctype="multipart/form-data"
    

    Inside the form itself, without it file upload won't work at all.

    0 讨论(0)
  • 2020-12-10 13:57

    I have checked the variables

    Do not check variables but check error messages.
    It's the only thing you need.
    Add these lines at the top of your code

    ini_set('display_errors',1);
    error_reporting(E_ALL);
    

    and see what it says.
    If move_uploaded_file failed, it will always raise an error with detailed explanation.
    You won't believe it, but reading error messages is way more efficient way to find a problem than guesswork you tried before

    I can't believe noone mentioned it already.

    0 讨论(0)
  • 2020-12-10 13:57

    Did you edit your php.ini to make sure upload_tmp_dir points to a temporary directory?

    0 讨论(0)
提交回复
热议问题