Probem in force downloading

0 MAK Leung · October 8, 2014
I have write a php code to download a mp4 file from my web hosting. However, the downloaded file does not have any content ?  Any problem in my code ?

Thanks
MAK
<?php
//php file code
if(isset($_POST['file_name'])){
$file=$_POST['file_name'];
header('Cache-Control:public');
header('Content-Description: File Transfer');
header('Content-Disposition: attachment;filename="'.$file.'"');
header('Content-Type: video/x-generic');
header('Content-Transfer-Encoding: binary');
readfile($file);
exit();
}
?>

<form action="force_download.php" method ="post" name="downloadform">
<input name="file_name" value="china.mp4" type="hidden">
<input type="submit" value="Download the MP4">
</form>


Post a Reply

Replies

Oldest  Newest  Rating
0 Ron Butcher · October 12, 2014
Here is the code that I have been using for file downloads.  It is a bit more code than yours, but it checks for the file before pushing it out, then breaks the file down into smaller chunks to easily handle large file downloads.


<?php
if(isset($_POST['file_name']))
{
$file = $_POST['file_name'];

//  Check to see if file exists, and push it to browser
if(file_exists($fileLocation.$file))  
//if file is not in same folder as php file, you need to define absolute path of file
{
//  Define HTML header info for file transfer to start
header('Content-Description:  File Transfer');
header('Content-Type:  video/mp4');
//  According to RFC 4337 video/mp4 is the correct content type for mp4 files
// see -> http://stackoverflow.com/questions/9929940/correct-mime-type-for-mp4
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding:  binary');
header('Expires:  0');
header('Cache-Control:  must-revalidate, post-check=0, pre-check=0');
header('Pragma:  public');
header('Content-Length:  '.filesize($fielLocation.$file));

//  Break file into manageable chunks for download
set_time_limit(0);
$file = @fopen($fileInfo->file_link.$fileInfo->file_name, "rb");
while(!feof($file))
{
print(@fread($file, 1024*8));
ob_flush();
flush();
}
}
else
{
echo '<h3>File Does Not Exist</h3>';
$errMsg = 'Unable to find file:  '.$file;
}
}
?>
  • 1

PHP

107,345 followers
About

Server-side, HTML embedded scripting language used to create dynamic Web pages.

Links
Moderators