PHP Uploading file unsuccessful

前端 未结 3 447
旧时难觅i
旧时难觅i 2021-01-27 05:43

I\'m trying to upload a file to my local server, but it keeps being unsuccessful.

All my files are inside /var/www/html/ However I made a folder called uploads in the h

相关标签:
3条回答
  • 2021-01-27 06:16

    Your Input file is

    <input name="uploadedfile" type="file" />

    so change $_FILES['fileToUpload']['name'] to $_FILES['uploadedfile']['name']

    $_FILES['uploadedfile']['name'] Must have the value of Name attribute of your file field

    0 讨论(0)
  • 2021-01-27 06:21

    You didn't set the variable

    $target_path
    

    you meant but not used

    $target_file
    

    instead.

    0 讨论(0)
  • 2021-01-27 06:22

    Try This:

    index.html

     <!DOCTYPE html>
     <html>
     <body>
     <form enctype="multipart/form-data" action="upload.php" method="POST">
     Choose a file to upload: <input name="uploadedfile" type="file" /><br />
     <input type="submit" value="Upload File" />
     </form>
     </body>
     </html>
    

    upload.php

    if(isset($_FILES["uploadedfile"]["type"]) && ($_FILES["uploadedfile"]["size"] < 5000000)){
          $sourcePath = $_FILES['uploadedfile']['tmp_name'];
          $file = $_FILES['uploadedfile']['name'];
          $targetPath = "/uploads/".$file;
          if(move_uploaded_file($sourcePath,$targetPath)){
          echo "The file: ".$_FILES['uploadedfile']['name']." has been uploaded";
       }else{
          echo "Looks like it failed.";
       }
    }else{
       echo "You forgot to select a file, or the file size is too large.";
    }
    

    So what this does is checks if a file exists and checks if it's smaller than 5MB. If so it moves on to the upload part.

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