2011-01-27 21 views
1

请看看这个:http://phpbin.net/x/1841090478PHP:如何在这段代码实现exif_imagetype

现在我想扩大这个代码,并添加更多的棋子,像exif_imagetype(),你可以上线139

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

现在我在质疑整件事情。我不能看到图像上传到if($ this-> file-> save ..但它怎么能检索pathinfo()然后呢?为什么不会['dirname']。['basename']找到文件不工作?为dirname我刚刚得到一个但基名我得到正确的图像文件名我想上传

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

谢谢你在前进

+0

你能发布你用来实现这个类的代码吗? – diagonalbatman 2011-01-27 16:58:57

回答

3

只是快速和肮脏的实施为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.'); 
     }  
    } 
}