I'm trying to create a remote upload system which will upload a file to Google drive from any given link. Which partially works. Now when I supply a download link from cloud storage provider like Google
as input it fails after finishing 92-99% upload process. The remote file is no longer readable. Not that the link expired or Google threw any error. The error log was empty on the server running the script. The code I'm using:
if ($_SERVER['REQUEST_METHOD'] == 'POST' && $client->getAccessToken()) {
$remote = $_POST['REMOTE_DL_LINK']; //Eg. https://docs-.....google...
$file = new Google_Service_Drive_DriveFile();
$file->name = "Big File";
$chunkSizeBytes = 1 * 1024 * 1024;
$client->setDefer(true);
$request = $service->files->create($file);
$media = new Google_Http_MediaFileUpload(
$client,
$request,
'text/plain',
null,
true,
$chunkSizeBytes
);
$media->setFileSize(filesize($remote));
// Upload the various chunks. $status will be false until the process is
// complete.
$status = false;
$handle = fopen($remote , "rb");
while (!$status && !feof($handle)) {
$chunk = readVideoChunk($handle, $chunkSizeBytes);
$status = $media->nextChunk($chunk);
}
$result = false;
if ($status != false) {
$result = $status;
}
fclose($handle);
}
function readVideoChunk ($handle, $chunkSize)
{
$byteCount = 0;
$giantChunk = "";
while (!feof($handle)) {
// fread will never return more than 8192 bytes if the stream is read buffered and it does not represent a plain file
$chunk = fread($handle, 8192);
$byteCount += strlen($chunk);
$giantChunk .= $chunk;
if ($byteCount >= $chunkSize)
{
return $giantChunk;
}
}
return $giantChunk;
}
?>
Expected : Upload the full file and return result object
Current result : returning false
in $result
Now if I use download link from services like letsupload.co I've no error. The operation succeed.
When using a Google Drive link upload stuck at 92-99% mark.
What's wrong with the Google link?
Why the read fails after sometimes?
Is there any alternative to fread()
? I tried to use guzzle http client but soon realised it was creating too many tcp connection
请问你是怎么解决的?fread()每次只能读取8192字节的数据。按照你上面的写法,$chunk 似乎是整个文件,
并没有符合批量上传的做法,请问你后面有更好的方法吗?