【问题标题】:PHP: How to implement exif_imagetype in this codePHP:如何在这段代码中实现 exif_imagetype
【发布时间】:2011-06-16 16:41:26
【问题描述】:

请看这个:http://phpbin.net/x/1841090478

现在我正在尝试扩展此代码并添加更多检查器,如您在第 139 行看到的 exif_imagetype()。

我无法让它工作,我刚刚收到 exif_imagetype 无法打开流,没有这样的......(图像不存在)。我还在参数中尝试了 $this->file->getName() ,但仍然是同样的错误。

现在我在质疑整件事。在图像到达 if($this->file->save .. 但它如何检索 pathinfo() 之前,我无法看到图像上传的任何地方?为什么不会 ['dirname'].['basename'] 到发现文件不起作用?对于目录名,我只得到一个 . 但基本名称我得到了我要上传的正确图像文件名。

那么它是如何工作的,我应该在哪里实现这个 exif_imagetype 检查器到这段代码中?

谢谢转发。

【问题讨论】:

  • 你能贴出你用来实现这个类的代码吗?
  • 我将它与 javascript 中的 ajax 调用请求一起使用。那是你想看到的吗?这是一个 javascript 插件,我只设置设置以及它应该向哪里发出请求。

标签: php image types exif


【解决方案1】:

只是 XHR 案例的快速而肮脏的实现(如聊天中所述):

/**
 * Handle file uploads via XMLHttpRequest
 */
class qqUploadedFileXhr {
    protected $_tempFile; 

    public function __construct() {
        $input = fopen("php://input", "r");
        $this->_tempFile = tempnam(sys_get_temp_dir(), 'xhr_upload_');
        file_put_contents($this->_tempFile, $input);
        fclose($input);
    }

    public function checkImageType() {
        switch(exif_imagetype( $this->_tempFile )) {
            case IMAGETYPE_GIF:
            case IMAGETYPE_JPEG:
            case IMAGETYPE_PNG:
                return true;
                break;
            default:
                return false;
                break;
        }
    }

    /**
     * Save the file to the specified path
     * @return boolean TRUE on success
     */
    function save($path) {
        if (filesize($this->_tempFile) != $this->getSize()){           
            return false;
        }
        rename($this->_tempFile, $path);
        return true;
    }
    function getName() {
        return $_GET['qqfile'];
    }
    function getSize() {
        if (isset($_SERVER["CONTENT_LENGTH"])){
            return (int)$_SERVER["CONTENT_LENGTH"];           
        } else {
            throw new Exception('Getting content length is not supported.');
        }     
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-10
    • 2019-04-05
    • 1970-01-01
    • 2021-10-13
    • 2020-03-06
    相关资源
    最近更新 更多