首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >上传不带扩展名的iphone图片(PHP)

上传不带扩展名的iphone图片(PHP)
EN

Stack Overflow用户
提问于 2019-02-22 02:29:49
回答 1查看 1.5K关注 0票数 0

在我的php代码中,我正在处理从不同来源上传的图片:桌面、移动设备等。通常,图片要么是PNG格式,要么是JPG格式,但我注意到,从IOS/Iphone (可能也是android)上拍摄的图片没有扩展名,只是“图片”。我已经尝试重新处理上传到服务器的图片(使用exif_imagetype和getExtension()来检查文件是否是图片以及它是否为扩展名,以便能够根据检测到的类型添加一个缺失的文件。但我做不到,因为我的脚本只接受PNG或JPG。解决这个问题最有效、最简单的方法是什么?

下面是我的代码:

代码语言:javascript
复制
if(!empty($_FILES['file']['name'])){
    $uploadedFile = '';
    if(!empty($_FILES["file"]["type"]))
    {
        $valid_extensions = array("jpeg", "jpg", "png");
        $temporary = explode(".", $_FILES["file"]["name"]);
        $file_extension = end($temporary);
        if((($_FILES["file"]["type"] == "image/png") || ($_FILES["file"]["type"] == "image/jpg") ||
         ($_FILES["file"]["type"] == "image/jpeg")) && in_array($file_extension, $valid_extensions))
        {
            $filetype=$_FILES["file"]["type"];
            $sourcePath = $_FILES['file']['tmp_name'];
            $targetPath = "upload/".$fileName;
            $temp = explode(".", $_FILES["file"]["name"]);      
            $newfilename = 'code' . '.' . end($temp);           
            move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $newfilename);

        }
    }
EN

回答 1

Stack Overflow用户

发布于 2019-02-22 03:10:35

IOS设备上的一些镜像是heic [ https://en.wikipedia.org/wiki/High_Efficiency_Image_File_Format ],这些是生活镜像。

所以你正在上传一些东西,当你检查扩展名和mime类型时,脚本会跳过并转到最后,如果你放了一个else语句,你可以让用户知道他们提交的文件不兼容。

代码语言:javascript
复制
if((($_FILES["file"]["type"] == "image/png") || ($_FILES["file"]["type"] == "image/jpg") ||
         ($_FILES["file"]["type"] == "image/jpeg")) && in_array($file_extension, $valid_extensions))
        {
            $filetype=$_FILES["file"]["type"];
            $sourcePath = $_FILES['file']['tmp_name'];
            $targetPath = "upload/".$fileName;
            $temp = explode(".", $_FILES["file"]["name"]);      
            $newfilename = 'code' . '.' . end($temp);           
            move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $newfilename);

        }else{
            print "Invalid File Format :" . $_FILES["file"]["type"];

        }
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54813919

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档