php header question
I'm trying to mask my download links using the code below.
It is almost working - it seems to get the file correctly however when it downloads the file is only 4kB in size.
Can anyone offer any suggestions??
Thanks!
Also please let me know if you need any additional details - I'm running this on MAMP using FF3.5.13
<?php
$filename="download.zip";
$folder = 'downloads';
$abs_path = $_SERVER['DOCUMENT_ROOT'];
$path = $abs_path . "/" . $folder ."/" .$filename; // the location of the file.
$mm_type="application/zip"; //this is for .zip files - Change this for other file types.
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Type: " . $mm_type);
header('Content-Disposition: attachment; filename="'.basename($path).'"');
header("Content-Length: " . filesize($path)); // 开发者_如何转开发**code edited as per comments below**
header("Content-Transfer-Encoding: binary");
readfile($path); //Output file for download.
exit();
?>
UPDATE: here is what is inside the file generated
Warning: filesize() [function.filesize]: stat failed for /Applications/MAMP/htdocs/downloads/download.zip in /Applications/MAMP/htdocs/CURRENT/test.php on line 15 Warning: Cannot modify header information - headers already sent by (output started at /Applications/MAMP/htdocs/CURRENT/test.php:15) in /Applications/MAMP/htdocs/CURRENT/test.php on line 15 Warning: Cannot modify header information - headers already sent by (output started at /Applications/MAMP/htdocs/CURRENT/test.php:15) in /Applications/MAMP/htdocs/CURRENT/test.php on line 16 Warning: readfile(/Applications/MAMP/htdocs/downloads/download.zip) [function.readfile]: failed to open stream: No such file or directory in /Applications/MAMP/htdocs/CURRENT/test.php on line 17I think this is where you got wrong:
header("Content-Length: " . filesize($file));
Change it to:
header("Content-Length: " . filesize($path));
header("Content-Length: " . filesize($file));
Where is $file being set?
This message
Warning: filesize() [function.filesize]: stat failed for /Applications/MAMP/htdocs/downloads/download.zip in /Applications/MAMP/htdocs/CURRENT/test.php on line 15
is clear enough: Your file doesn't exist.
Try:
header ("Content-Disposition: attachment; filename=".$name."\n\n");
header ("Content-Type: application/octet-stream");
header ("Content-Length: ".filesize($complete_path_file));
readfile($complete_path_file);
or also changing the content type to header("Content-type: application/force-download");
精彩评论