PHP-文件上传,exif_imagetype()警告 [英] PHP - file upload, exif_imagetype() warning

查看:212
本文介绍了PHP-文件上传,exif_imagetype()警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现文件上传,并检查它是否是.png文件.

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.PNG无错误:)警告:exif_imagetype(Caffpture.PNG): 无法打开流:在该目录中没有此类文件或目录 第16行的/home/store/fhs36113/public_html/wp2/u4/upload.php错误 文件类型!

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!";
    }

?>

推荐答案

['name']是在进程的CLIENT端使用的文件名.实际上,PHP上传文件存储在['tmp_name']中,直到您将文件移动/复制到其他位置为止.因此,您正在访问的文件几乎可以保证在您的服务器上不存在.

['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.

尝试

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

相反.

这篇关于PHP-文件上传,exif_imagetype()警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆