0
votes

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 17

4
What does the file contain? I bet it's a PHP error message - Pekka
Try opening the file in a text editor. - Emil Vikström
Your code worked for me. Check that your file is actually in the right place (/downloads/download.zip), and you haven't put it somewhere else (e.g. /CURRENT/downloads/download.zip). Or if that's where you want it, change your path to reflect that. - Aether
/Applications/MAMP/htdocs/downloads/download.zip is not exists: failed to open stream: No such file or directory - ariefbayu
Thanks everyone! The path to the file was incorrect - All Fixed Now!!!! - Simon

4 Answers

3
votes

I think this is where you got wrong:

header("Content-Length: " . filesize($file));

Change it to:

header("Content-Length: " . filesize($path));
1
votes
header("Content-Length: " . filesize($file)); 

Where is $file being set?

1
votes

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.

0
votes

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");