0

Currently, I am using plupload.js to upload files to google drive using Google Drive API. But, I can't upload a bigger file that is configured in php.ini upload_max_filesize.

I am getting the below error:

The uploaded file exceeds the upload_max_filesize directive in php.ini

Is there any way to skip the server upload_max_filesize limitation to upload files to Google Drive using the Google Drive PHP SDK API?

$file = $files['file'];

        try {

            $this->client->setDefer( true );

            $params = [
                'fields'              => '*',
                'supportsAllDrives'   => true,
                'enforceSingleParent' => true
            ];

            $parent_id = ! empty( $folder['id'] ) ? $folder['id'] : $folder;

            $request = $this->service->files->create( new \IGDGoogle_Service_Drive_DriveFile( [
                'name'     => $file['name'],
                'parents'  => [ $parent_id ],
                'mimeType' => $file['type']
            ] ), $params );

            $chunk_size_bytes = 5 * 1024 * 1024;

            $media = new \IGDGoogle_Http_MediaFileUpload( $this->client, $request, $file['type'], null, true, $chunk_size_bytes );
            $media->setFileSize( $file['size'] );

            $status        = false;
            $upload_status = [
                'url'           => $media->getResumeUri(),
                'size_uploaded' => 0,
                'percent'       => 0,
            ];

            $fileHandler = fopen( $file['tmp_name'], 'rb' );

            while ( ! $status and ! feof( $fileHandler ) ) {

                $chunk  = fread( $fileHandler, $chunk_size_bytes );
                $status = $media->nextChunk( $chunk );

                $upload_status['size_uploaded'] += strlen( $chunk );
                $upload_status['percent']       = round( ( $upload_status['size_uploaded'] / $file['size'] ) * 100 );

                // Update the upload status
                set_transient( 'igd_upload_' . $file_id, $upload_status, HOUR_IN_SECONDS );

            }

            fclose( $fileHandler );

            $this->client->setDefer( false );

            if ( $status ) {
                $file = igd_file_map( $status );
                Files::instance()->add_file( $file, $parent_id );

                // Delete upload status
                //delete_transient( 'igd_upload_' . $file_id );

                return [
                    'success' => true,
                    'file'    => $file,
                ];
            }

        } catch ( \Exception $exception ) {
            return [
                'error' => $exception->getMessage(),
            ];
        }
Prince Ahmed
  • 1,038
  • 10
  • 10
  • Have you tried modifying the php.ini file? What is the biggest possible file size that you are trying to upload? – NightEye May 31 '22 at 01:37
  • I am using the Google Drive API to a WordPress plugin that let the user upload any size of the file. I want that users don't need to edit the php.ini file. – Prince Ahmed May 31 '22 at 04:11
  • Does this answer your question? [The uploaded file exceeds the upload\_max\_filesize directive in php.ini error while uploading plugin](https://stackoverflow.com/questions/36963262/the-uploaded-file-exceeds-the-upload-max-filesize-directive-in-php-ini-error-whi) – NightEye May 31 '22 at 06:08
  • I don't think there is another way. Your only option is [modifying files/settings](https://phoenixnap.com/kb/fix-the-uploaded-file-exceeds-the-upload-max-filesize-directive-in-php-ini-wordpress), and here is a [similar post](https://stackoverflow.com/q/36963262/17842569). – NightEye May 31 '22 at 06:08

0 Answers0