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 :
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.