Why is my file not loading?

I am just trying to upload a file and nothing works.

 <!DOCTYPE html>
 <html><body>

            <form id="exp" enctype = "multipart/form-data" action="../cgi-bin/uploadPic.php">
                <input type="file" id="profilePic2" name="newPic"></input> <br /><br /><br />
                <input type="submit" value="Upload Pic" style="color:black;"></input>
            </form>
  </body></html>

Running my php file. Please pay attention to the inaccurate code, I am trying to debug via echo.

 <html>
 <body>
 <?php

$loadFile = true; // error code
$allowedExts = array("jpg","jpeg","gif","png");
$extension = end(explode(".", $_FILES["newPic"]["name"]));

$file = $_FILES["newPic"];
$picIncluded = false;

if((    ($_FILES["newPic"]["type"] == "image/gif")
||  ($_FILES["newPic"]["type"] == "image/jpeg")
||  ($_FILES["newPic"]["type"] == "image/png")
||  ($_FILES["newPic"]["type"] == "image/pjpeg"))
&&  ($_FILES["newPic"]["size"] < 5000000)
&&  in_array($extension, $allowedExts)){
    if($_FILES["newPic"]["error"] > 0){
        echo "pnl"; // error code returned to client
    }
    else if(file_exists("../profile-pics/" . $file["name"])){
        echo "pld"; // picture loaded duplicate
    } else {
        $picIncluded = true;
        echo "finally";
        if(move_uploaded_file($file["name"]["tmp_name"],"./" . $file["name"])){
            echo "Success again!";
        }
        $path = "../profile-pics/" . $file["name"];
    }
}
if(is_uploaded_file($_FILES["newPic"]["name"]["tmp_name"])){
    echo "Good news!";
}

 ?>

 </body>
 </html>

In any case, the move_uploaded_file () function returns false since it cannot find the temp file. is_uploaded_file () also returns false. My extensions and everything is correct, but still unsuccessfully, the file is not being downloaded. How can I debug this?

+5
source share
2 answers

change this value

if(move_uploaded_file($file["name"]["tmp_name"],"./" . $file["name"])){

to

if(move_uploaded_file($file["tmp_name"],"./" . $file["name"])){

because you appointed

$file = $_FILES["newPic"];
+5
source

Also, to check the security before downloading, check is_uploaded_file ():

if(is_uploaded_file($_FILES["newPic"]["tmp_name"])){
 if(move_uploaded_file($_FILES["newPic"]["tmp_name"],"../profile-pics/" . $file["name"])){
  echo 'file uploaded...';
 }
}else{
  echo "Possible file upload attack: ";
}
+2
source

All Articles