Php 如何将视频上传到谷歌硬盘?
我已经创建了Google服务帐户,并成功地将我的应用程序与Google drive连接Php 如何将视频上传到谷歌硬盘?,php,file-upload,google-api,google-drive-api,google-api-php-client,Php,File Upload,Google Api,Google Drive Api,Google Api Php Client,我已经创建了Google服务帐户,并成功地将我的应用程序与Google drive连接 $client = new Google_Client(); $credentialsFile = 'PATH TO JSON CREDENTIALS'; if (!file_exists($credentialsFile)) { throw new RuntimeException('Service account credentials Not Found!'); } $client->
$client = new Google_Client();
$credentialsFile = 'PATH TO JSON CREDENTIALS';
if (!file_exists($credentialsFile)) {
throw new RuntimeException('Service account credentials Not Found!');
}
$client->setAuthConfig($credentialsFile);
$client->setApplicationName("Reflektions Drive");
$client->setScopes(Google_Service_Drive::DRIVE);
return new Google_Service_Drive($client);
我在将视频上传到谷歌硬盘时遇到了问题。由于视频大小很大,我得到了最大的执行错误
$driveService = drive_get_service();
$file = new Google_Service_Drive_DriveFile();
$file->setName('vid.mp4');
$file->setDescription('A test document');
$file->setMimeType('video/mp4');
$data = file_get_contents(base_url('vid.mp4'));
$createdFile = $driveService->files->create($file, array(
'data' => $data,
'mimeType' => 'video/mp4',
'uploadType' => 'multipart'
));
echo "<pre>";
print_r($createdFile);
$driveService=drive\u get\u service();
$file=新的Google_服务_驱动器_驱动器文件();
$file->setName('vid.mp4');
$file->setDescription(“测试文档”);
$file->setMimeType('video/mp4');
$data=文件内容(基本url('vid.mp4');
$createdFile=$driveService->files->create($file,array)(
“数据”=>$data,
'mimeType'=>'video/mp4',
“uploadType”=>“多部分”
));
回声“;
打印文件($createdFile);
使用相同的代码,我成功上传了图像文件。我也无法在浏览器中显示图像。
所以我的问题是:
函数上传(){
定义(“TESTFILE”,“C:\xampp\htdocs\project\vid.mp4”);
$driveService=drive_get_service();
$file=新的Google_服务_驱动器_驱动器文件();
$file->name=“大文件”;
$chunkSizeBytes=1*1024*1024;
$request=$driveService->files->create($file);
$media=新的Google\u Http\u MediaFileUpload(
$driveService->getClient(),
$request,
“视频/mp4”,
无效的
是的,
$chunkSizeBytes
);
$media->setFileSize(filesize(TESTFILE));
$status=false;
$handle=fopen(TESTFILE,“rb”);
while(!$status&&!feof($handle)){
$chunk=$this->readVideoChunk($handle,$chunkSizeBytes);
$status=$media->nextChunk($chunk);
}
$result=false;
如果($status!=false){
$result=$status;
}
fclose($handle);
回声$结果;
回声“;
打印(文件);
}
函数readVideoChunk($handle,$chunkSize)
{
$byteCount=0;
$giantChunk=“”;
而(!feof($handle)){
$chunk=fread($handle,8192);
$byteCount+=strlen($chunk);
$giantChunk.=$chunk;
如果($byteCount>=$chunkSize){
返回$giantChunk;
}
}
返回$giantChunk;
}
我得到的最大执行时间超过了30秒的错误。它在文档中。寻找可恢复的上传。使用该方法向我们展示问题。而且应该有帮助
function upload(){
DEFINE("TESTFILE", 'C:\xampp\htdocs\project\vid.mp4');
$driveService = drive_get_service();
$file = new Google_Service_Drive_DriveFile();
$file->name = "Big File";
$chunkSizeBytes = 1 * 1024 * 1024;
$request = $driveService->files->create($file);
$media = new Google_Http_MediaFileUpload(
$driveService->getClient(),
$request,
'video/mp4',
null,
true,
$chunkSizeBytes
);
$media->setFileSize(filesize(TESTFILE));
$status = false;
$handle = fopen(TESTFILE, "rb");
while (!$status && !feof($handle)) {
$chunk = $this->readVideoChunk($handle, $chunkSizeBytes);
$status = $media->nextChunk($chunk);
}
$result = false;
if ($status != false) {
$result = $status;
}
fclose($handle);
echo $result;
echo "<pre>";
print_r($file);
}
function readVideoChunk($handle, $chunkSize)
{
$byteCount = 0;
$giantChunk = "";
while (!feof($handle)) {
$chunk = fread($handle, 8192);
$byteCount += strlen($chunk);
$giantChunk .= $chunk;
if ($byteCount >= $chunkSize) {
return $giantChunk;
}
}
return $giantChunk;
}