我有两个功能:一个使用分块上传,另一个使用上传整个文件
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:// urls上传,但putFile
无法上传。为什么会这样?这两个函数中是否存在可能导致此问题的问题?
答案 0 :(得分: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);