2011-01-27 20 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.'); 
     }  
    } 
}