John Davidson

Video file not playing after downloading using php

0 comments
Message:


I use the script below to download a video, but the video file does not play after downloading to my pc, it's having a glitch when i try to play it, can any help me out.


My code below


<?php

$name = 'APM1M10VLOUO07C9.mp4';

header("Content-type: application/mp4");
header('Content-Disposition: attachment;filename='.$name);
header("Pragma: no-cache");
header("Expires: 0");
readfile($name);
exit;


?>


My Answer:
It looks like you are using the `file_get_contents` function to download the video file. This function reads a file into a string, which may not work correctly for video files.

Instead, you should use the `readfile` function to download the video file. This function reads a file and sends it directly to the output buffer, which is more suitable for downloading large files like videos.

Here is an example of how you can modify your code to use the `readfile` function:

php
$file = 'path/to/video.mp4';

header('Content-Description: File Transfer');
header('Content-Type: video/mp4');
header('Content-Disposition: attachment; filename=' . basename($file));
header('Content-Length: ' . filesize($file));
readfile($file);
exit;


Make sure to replace `'path/to/video.mp4'` with the actual path to your video file. This code will send the video file directly to the user's browser for download, without reading it into memory first.

I hope this helps! Let me know if you have any other questions.

Rate this post

3 of 5 based on 8490 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us