2012-08-09 99 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);