Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

PHP fwrite chunks to video file

I have a very large video file and I am sending it in chunks using dropzone js, I can see the chunks are getting sent to my php code in the correct order and I am able to create the file from the first chunk, but when I try to fwrite to the file from other chunks, nothing happens and the file size of my video is 0 bytes when I am expecting 27.7MB, what am I doing wrong?

//Check if file exists
        if(file_exists($uploadPath . $uploadedFiles['videos']->getClientFilename())) {
          //if yes, add onto the file
          $file = fopen($uploadPath . $uploadedFiles['videos']->getClientFilename(), "w");
          //$chunk = fread($uploadedFiles['videos'], 1024);
          fwrite($file, $uploadedFiles['videos']->getStream()->getContents());
          fclose($file);

        }
        else
        {
          //if no, create the file from first chunk
          $uploadedFiles['videos']->moveTo($uploadPath . $uploadedFiles['videos']->getClientFilename());
        }

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You should use fopen in mode a.

$file = fopen($uploadPath . $uploadedFiles['videos']->getClientFilename(), "a");

https://www.php.net/manual/en/function.fopen.php

The mode w clears the whole file and set a new content. The mode a appends a content at the end.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading