PHP - file upload, exif_imagetype() warning

2019-07-25 18:38发布

I was trying to implement a file upload and check if it's .png file.

Even though I get a warning, no errors occur. The output of the code is the following:

Caffpture.PNGNo Errors :) Warning: exif_imagetype(Caffpture.PNG): failed to open stream: No such file or directory in /home/store/fhs36113/public_html/wp2/u4/upload.php on line 16 wrong file type!

I hope you guys can tell me what I am doing wrong.

    <?php

error_reporting(E_ALL);
   ini_set("display_errors", true);

echo $_FILES["thefile"]["name"];

if ($_FILES['thefile']['error'] === UPLOAD_ERR_OK) {
   echo "No Errors :)";
}else{
echo $_FILES['thefile']['error'];
}

if(exif_imagetype($_FILES['thefile']['name']) == IMAGETYPE_PNG)
    {
        $target_path = "uploads/";

        $target_path = $target_path . basename( $_FILES['thefile']['name']); 

        if(move_uploaded_file($_FILES['thefile']['tmp_name'], $target_path)) {
            echo "The file ".  basename( $_FILES['thefile']['name']). 
            " has been uploaded";
        } else{
            echo "There was an error uploading the file, please try again! Remember: only jpeg, pnh and gif files are allowed!";
        }
    }
    else
    {
        echo "wrong file type!";
    }

?>

1条回答
Fickle 薄情
2楼-- · 2019-07-25 18:57

['name'] is the filename as used on the CLIENT side of the process. A PHP upload is actually stored in ['tmp_name'] until you move/copy the file elsewhere. So you're accessing a file which almost guaranteed does not exist on your server.

Try

if(exif_imagetype($_FILES['thefile']['tmp_name']) == IMAGETYPE_PNG)

instead.

查看更多
登录 后发表回答