0

我有 2 个功能:一个使用分块上传,另一个使用上传整个文件

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不能。为什么会这样?这两个函数中是否存在可能导致此问题的问题?

4

1 回答 1

1

这是因为 file_exists 在远程服务器上不起作用

PHP 5 中的 file_exists() 不接受 URL,只接受本地路径名

改为使用 curl 发送头部请求

$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);
于 2012-08-09T02:06:51.730 回答