Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I want to require a file to be downloaded upon the user visiting a web page with PHP. I think it has something to do with file_get_contents, but am not sure how to execute it.

$url = "http://example.com/go.exe";

Edit
After downloading a file with header(location) it is not redirecting to another page. It just stops.

share|improve this question
2  
You should mark it as answer if it was helpful. –  Pit Digger Sep 8 '11 at 18:49
    
possible duplicate of forcing page download in php –  hakre Oct 19 '13 at 12:54
    
possible duplicate of Forcing to download a file using PHP –  user May 15 at 5:57

4 Answers 4

Read the docs about built-in PHP function readfile

$file_url = 'http://www.myremoteserver.com/file.exe';
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary"); 
header("Content-disposition: attachment; filename=\"" . basename($file_url) . "\""); 
readfile($file_url); // do the double-download-dance (dirty but worky)

Also make sure to add proper content type based on your file application/zip, application/pdf etc. - but only if you do not want to trigger the save-as dialog.

share|improve this answer
3  
Why is that required? –  john Aug 31 '11 at 22:01
    
I mean if its other than .exe file than only .Else this should work fine. –  Pit Digger Aug 31 '11 at 22:02
header("Content-Type: application/octet-stream");
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"file.exe\""); 
echo readfile($url);

is correct

or better one for exe type of files

header("Location: $url");
share|improve this answer
    
This wouldn't work for a lot of files (pdf, txt and so on) –  Fabio Aug 31 '11 at 21:57
    
@Fabio: Will add more detials –  genesis Aug 31 '11 at 21:59
    
That was super easy. It worked. What is file-Get_contents for then? Just curious. Thanks. –  john Aug 31 '11 at 22:00
    
to download it to your server –  genesis Aug 31 '11 at 22:01
2  
Just remove 'echo' prior to 'readfile()' since the return value is specified as "Returns the number of bytes read from the file. If an error occurs, FALSE is returned and unless the function was called as @readfile(), an error message is printed.". So you'll end up with the content of the file + integer number at the end of the content. –  Mladen B. Jun 28 '13 at 8:08
<?php
$file = "http://example.com/go.exe"; 

header("Content-Description: File Transfer"); 
header("Content-Type: application/octet-stream"); 
header("Content-Disposition: attachment; filename=\"$file\""); 

readfile ($file); 
?>

Or, when the file is not openable with the browser, you can just use the Location header:

<?php header("Location: http://example.com/go.exe"); ?>
share|improve this answer
1  
filename in your header shouldn't be $file (which contains the http part) but a valid filename. –  Fabio Aug 31 '11 at 21:56
1  
There is no application/force-download media type; use application/octet-stream instead. –  Gumbo Aug 31 '11 at 21:58
1  
Awesome. Thanks. It worked. –  john Aug 31 '11 at 22:00
    
@Gumbo: see this php.net/manual/en/function.header.php#83384 –  Marek Sebera Aug 31 '11 at 22:01

try this:

header('Content-type: audio/mp3'); 
header('Content-disposition: attachment; 
filename=“'.$trackname'”');                             
readfile('folder name /'.$trackname);          
exit();
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.