skip to Main Content

I am using dropzone js with chunking to take a large file and break it up, each one of those pieces are getting sent to PHP and all the pieces of that large file have a mime type application/octet-stream (if you put the chunk files together it will be video/mp4) now what I am trying to do is put the file back together in Google Drive and here is my PHP code:

public function addFile($filename, $content, $description = '')
    {
        $file = new GoogleServiceDriveDriveFile();
        $file->setName($filename);
        $file->setParents([$_ENV['FOLDER_ID']]);
        $file->setDescription($description);

        $result = $this->driveService->files->create(
            $file,
            [
                'data' => $content,
                'mimeType' => 'application/octet-stream',
                'uploadType' => 'resumable'
            ]
        );

        return sprintf('https://drive.google.com/file/d/%s/view?usp=sharing', $result->getId());
    }

The problem I am having is each chunk is getting uploaded to my Google Drive when I am expecting 1 file put together, is what I am trying to do even possible?

2

Answers


  1. I think you would have to write the chunks to the server’s local disk into one file. You can use file_put_contents( $filename, $chunk, FILE_APPEND );

    then use, the dropzone complete event handler to notify the server via async request that the upload is complete

    myDropzone.on("complete", function(file) {
      myDropzone.removeFile(file);
      fetch('/upload-complete', {method:'POST', file})
    });
    
    //upload-conplete.php
    $file = $_POST['file'];
    // chack status of uploaded file here
    var_dump($file);
    // add file to Google drive here if all is well
    
    Login or Signup to reply.
  2. Try checking the media uploader from the large-file-upload sample

    $file = new GoogleServiceDriveDriveFile();
    $file->name = "Big File";
    $chunkSizeBytes = 1 * 1024 * 1024;
    
    // Call the API with the media upload, defer so it doesn't immediately return.
    $client->setDefer(true);
    $request = $service->files->create($file);
    
    // Create a media file upload to represent our upload process.
    $media = new GoogleHttpMediaFileUpload(
        $client,
        $request,
        'text/plain',
        null,
        true,
        $chunkSizeBytes
    );
    $media->setFileSize(filesize(TESTFILE));
    
    // Upload the various chunks. $status will be false until the process is
    // complete.
    $status = false;
    $handle = fopen(TESTFILE, "rb");
    while (!$status && !feof($handle)) {
        // read until you get $chunkSizeBytes from TESTFILE
        // fread will never return more than 8192 bytes if the stream is read
        // buffered and it does not represent a plain file
        // An example of a read buffered file is when reading from a URL
        $chunk = readVideoChunk($handle, $chunkSizeBytes);
        $status = $media->nextChunk($chunk);
    }
    
    // The final value of $status will be the data from the API for the object
    // that has been uploaded.
    $result = false;
    if ($status != false) {
        $result = $status;
    }
    
    fclose($handle);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search