2012-08-09 27 views
0

我有2個功能:一個使用分塊的上傳和上傳整個文件功能不能上傳遠程PDF文件

public function chunkedUpload($file, $filename = false, $path = '', $overwrite = true) 
{ 
    if (file_exists($file)) { 
     if ($handle = @fopen($file, 'r')) { 
      // Set initial upload ID and offset 
      $uploadID = null; 
      $offset = 0; 

      // Read from the file handle until End OF File, uploading each chunk 
      while ($data = fread($handle, $this->chunkSize)) { 
       $chunkHandle = fopen('php://temp', 'rw'); 
       fwrite($chunkHandle, $data); 
       $this->OAuth->setInFile($chunkHandle); 

       // On subsequent chunks, use the upload ID returned by the previous request 
       if (isset($response['body']->upload_id)) { 
        $uploadID = $response['body']->upload_id; 
       } 

       $params = array('upload_id' => $uploadID, 'offset' => $offset); 
       $response = $this->fetch('PUT', self::CONTENT_URL, 'chunked_upload', $params); 
       $offset += mb_strlen($data, '8bit'); 
       fclose($chunkHandle); 
      } 

      // Complete the chunked upload 
      $filename = (is_string($filename)) ? $filename : basename($file); 
      $call = 'commit_chunked_upload/' . $this->root . '/' . $this->encodePath($path . $filename); 
      $params = array('overwrite' => (int) $overwrite, 'upload_id' => $uploadID); 
      $response = $this->fetch('POST', self::CONTENT_URL, $call, $params); 
      return $response; 
     } else { 
      throw new Exception('Could not open ' . $file . ' for reading'); 
     } 
    } 

    // Throw an Exception if the file does not exist 
    throw new Exception('Local file ' . $file . ' does not exist'); 
} 




     public function putFile($file, $filename = false, $path = '', $overwrite = true) 
{ 
    if (file_exists($file)) { 
     if (filesize($file) <= 157286400) { 
      $filename = (is_string($filename)) ? $filename : basename($file); 
      $call = 'files/' . $this->root . '/' . $this->encodePath($path . $filename); 
      // If no filename is provided we'll use the original filename 

      $params = array(
       'filename' => $filename, 
       'file' => '@' . str_replace('\\', '\\', $file) . ';filename=' . $filename, 
       'overwrite' => (int) $overwrite, 
      ); 
      $response = $this->fetch('POST', self::CONTENT_URL, $call, $params); 
      return $response; 
     } 
     throw new Exception('File exceeds 150MB upload limit'); 
    } 

    // Throw an Exception if the file does not exist 
    throw new Exception('Local file ' . $file . ' does not exist'); 
} 

我已經從同一服務器目錄,他們都很好地工作測試這些功能的其它;但是,chunkedUpload可以從遠程http://和ftp:// url上傳,但putFile無法。爲什麼會發生?這兩個函數中是否存在可能導致此問題的問題?

回答

1

這是因爲file_exists沒有PHP 5中遠程服務器上的工作

file_exists()不接受的網址只能本地路徑名

使用捲曲發送HEAD請求,而不是

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, 'your url'); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_HEADER, true); 
curl_setopt($ch, CURLOPT_NOBODY, true); 
curl_exec($ch); 
$size = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD); 
var_dump($size);