php - 使用 PHP 上传大于约 30MB 的文件时,Google API 不返回任何内容
问题描述
为什么我的 PHP 代码在上传大约大于 30MB 的文件时没有返回任何内容,但它可以在 localhost 上运行,问题出在服务器端。有时它会说 503 错误或错误:NET CONNECTION RESET。但大多什么都不返回。
当我上传小于 25MB 的文件时,它们会非常安静地上传。
而且,如果有人可以在驱动器中上传数据时帮助我使用进度条,则在我的代码中显示进度,但在文件上传后,而不是在上传时(我阅读了有关 getURI 和...但没有t理解正确)有人也可以在这里指导我吗?
我正在使用 V3 的 Google-drive-api。
索引文件
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8">
<title>Google Drive Example App</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<script>
$(function () {
$('#btn').click(function () {
$('.myprogress').css('width', '0');
$('.msg').text('');
var filename = $('#filename').val();
var myfile = $('#myfile').val();
var discription = $('#discription').val();
if (filename == '' || myfile == '') {
alert('Please enter file name and select file');
return;
}
var formData = new FormData();
formData.append('pdf', $('#myfile')[0].files[0]);
formData.append('name', filename);
formData.append('discription', discription);
$('#btn').attr('disabled', 'disabled');
$('.msg').text('Uploading in progress...');
$.ajax({
url: 'action.php',
data: formData,
processData: false,
contentType: false,
type: 'POST',
// this part is progress bar
xhr: function () {
var xhr = new window.XMLHttpRequest();
xhr.upload.addEventListener("progress", function (evt) {
if (evt.lengthComputable) {
var percentComplete = evt.loaded / evt.total;
percentComplete = parseInt(percentComplete * 100);
$('.myprogress').text(percentComplete + '%');
$('.myprogress').css('width', percentComplete + '%');
}
}, false);
return xhr;
},
success: function (data) {
$('.msg').text(data);
$('#btn').removeAttr('disabled');
}
});
});
});
</script>
</head>
<body>
<div class="container">
<div class="row">
<h3>File Upload</h3>
<form id="myform" method="post">
<div class="form-group">
<label>File name: </label>
<input class="form-control" type="text" id="filename" />
</div>
<div class="form-group">
<label>File Discription: </label>
<input class="form-control" type="text" id="discription" />
</div>
<div class="form-group">
<label>Select file: </label>
<input class="form-control" type="file" id="myfile" />
</div>
<div class="form-group">
<div class="progress">
<div class="progress-bar progress-bar-success myprogress" role="progressbar" style="width:0%">0%</div>
</div>
<div class="msg"></div>
</div>
<input type="button" id="btn" class="btn-success" value="Upload" />
</form>
</div>
</div>
</body>
</html>
动作.php
<?php
ini_set('upload_max_filesize', '1000M');
ini_set('post_max_size', '1000M');
set_time_limit(5000);
if(isset($_FILES['pdf']) && isset($_POST['name'])){
$name = $_POST['name'];
if($name == ''){
echo "file name is Empty";
}else{
$discription = $_POST['discription'];
$target_dir = "files/";
$file_name = basename($_FILES["pdf"]["name"]);
$filesize = $_FILES['pdf']['size'];
$FileType = strtolower(pathinfo($file_name,PATHINFO_EXTENSION));
$file = "test_file.".$FileType;
$file_path = $target_dir . $file;
$tmpname = $_FILES['pdf']['tmp_name'];
$upload = move_uploaded_file($tmpname, $file_path);
if($upload == true){
include_once __DIR__ . '/google-api-php-client-master/vendor/autoload.php';
$client = new Google_Client();
$client->setAuthConfig('credentials.json');
$client->addScope('https://www.googleapis.com/auth/drive.file');
$service = new Google_Service_Drive($client);
$service = new Google_Service_Drive($client);
$client->getAccessToken();
$file = new Google_Service_Drive_DriveFile();
$chunkSizeBytes = 1 * 1024 * 1024;
$client->setDefer(true);
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$parentId = "PARENT_FOLDER_ID";
$file->setParents(array($parentId));
$mime_type = finfo_file($finfo, $file_path);
$file->setName($name);
$file->setDescription($discription);
$response = $service->files->create($file);
// Create a media file upload to represent our upload process.
$media = new Google_Http_MediaFileUpload(
$client,
$response,
$mime_type,
null,
true,
$chunkSizeBytes
);
$media->setFileSize(filesize($file_path));
function readVideoChunk ($handle, $chunkSize){
$byteCount = 0;
$giantChunk = "";
while (!feof($handle)) {
$chunk = fread($handle, $chunkSize);
$byteCount += strlen($chunk);
$giantChunk .= $chunk;
if ($byteCount >= $chunkSize)
{
return $giantChunk;
}
}
return $giantChunk;
}
// Upload the various chunks. $status will be false until the process is complete.
$status = false;
$handle = fopen($file_path, "rb");
while (!$status && !feof($handle)) {
// An example of a read buffered file is when reading from a URL
$chunk = readVideoChunk($handle, $chunkSizeBytes);
$status = $media->nextChunk($chunk);
//if want to show the progress bar too but not working while uploading , but it shows the uploaded chunks after the files is been uploaded.
if(!$status){ //nextChunk() returns 'false' whenever the upload is still in progress
echo 'sucessfully uploaded file up to byte ' . $media->getProgress() . ' which is ' . ( $media->getProgress() / $chunkSizeBytes ) . '% of the whole file';
}
}
$result = false;
if ($status != false) {
$result = $status;
}
$file_id = $status['id'];
fclose($handle);
// Reset to the client to execute requests immediately in the future.
$client->setDefer(false);
//i am manually creating the wbViewLink by the ID i am getting in response
$webViewLink = "https://drive.google.com/file/d/$file_id/view?usp=drivesdk";
$myobj = new StdClass();
$myobj->{'alt_link'} = $webViewLink;
$myobj->{'size'} = $filesize;
print_r($myobj);
}else{
echo "upload failed";
}
}
}
?>
有人能帮我一下吗 ?如您所见,我正在使用服务帐户代表我对用户进行身份验证...
但是当我上传文件时,结果大于 30MB(仅在服务器上,在本地主机上工作正常){image}
您还可以在第二张图片中看到状态正常但仍然没有响应。
解决方案
You can't always override upload size setting with ini_set, depending on hosting provider. Try this in your .htaccess:
php_value upload_max_filesize 100M
php_value post_max_size 100M
推荐阅读
- javascript - 有没有办法使用从我计算机上的不同文件夹调用的 CSS 来编辑 svg?
- android - 谁以及何时应该处置意图?
- json - Angular 6 - 文本区域中的值显示为对象对象
- ios - 选择器没有快速输出行数据 3
- c - C - 结构变量是指针吗?
- python - 递归函数:“NoneType”对象不可迭代
- python - 使用独立的 python 库显示图像
- node.js - Nodejs - Export Excel not downloading excelfile
- python - Keras 丢弃或忽略不确定的预测结果
- mongodb - 如果不使用正确的索引,MongoDB 查询将失败