[PHP] Reg - Video File Chunk issue

Hi Team,

In one of the projects, I have faced the issue of chunking the file. Below is the code I used to chunk the file.

$fp = fopen($mediaUrl, ‘r’);
$segment_id = 0;

while (!feof($fp)) {
$chunk = fread($fp, 4 * 1024 * 1024); // chunk the file as 4MB
}
fclose($fp);

If we try to push any video file with above 10MB, we didn’t get the proper chunk file. Its generated randomly with different bytes.

Can you please help with this?

Thanks & Regards,
Raghul Velliangiri
Mobile - +91 94882 51474

fread does not return the exactly size of bytes. You should process it manually

while (!feof($fp)) {
$chunk += fread($fp, 4 * 1024 * 1024); // chunk the file as 4MB
while( $chunk.length >= 4M) {
$chunk4m = $chunk.substring(0, 4M);
fwrite(file, chunk4m)
$chunk = $chunk.substring(4M)
}
}

On Jul 18, 2024, at 4:34 PM, Raghul Tlv <tlvraghul@gmail.com> wrote:

while (!feof($fp)) {
$chunk = fread($fp, 4 * 1024 * 1024); // chunk the file as 4MB
}