[使用PHP上传的文件大于30MB时,Google API不会返回任何内容

问题描述 投票:1回答:2

为什么我的PHP代码在上传大约30MB以上的文件时什么也不返回,但是在localhost上有效,问题出在服务器端。有时它显示503错误或错误:NET CONNECTION RESET。但大多数都不会返回任何内容。

当我上传小于25MB的文件时,它们会非常安静地上传。

[而且如果有人可以在驱动器中上传数据时帮助进度条,在我的代码中显示的是进度,但是在文件上传之后,而不是在上传时(我阅读了getURI和...没有正确理解),有人也可以在这里引导我吗?

我正在使用Google-drive-api的V3。

INDEX.php

<!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>

ACTION.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";
        }
    }
  }
?>

有人可以在这里帮助我吗?我正在使用服务帐户,因为您可以看到代表我对用户进行身份验证...

when everything is fine then result is something like this{image}

but when i upload file then is greater then 30MB result (only on server, on localhost is works fine) {image}

您还可以在第二张图像中看到OK的状态,但仍然没有响应。

php jquery ajax google-drive-api
2个回答
0
投票

您不能始终使用ini_set覆盖上传大小设置,具体取决于托管服务提供商。在您的.htaccess中尝试一下:

php_value upload_max_filesize 100M
php_value post_max_size 100M

-1
投票

此链接可能有助于您进行上传https://developers.google.com/drive/api/v3/manage-uploads#resumable

还可以使用phpinfo()检查您的php设置,查看上传数据是否有任何限制。

您可以分享您的phpinfo()结果

© www.soinside.com 2019 - 2024. All rights reserved.