-1

Im using fopen to let users download a audio in the code below as the code attribute doesn't always works in all situations and browsers.

Does this downloads the file to my server temporarily or lets the user download it from the external sourced

<?php
$file=fopen('link','r');
header("Content-Type:audio/mp4");
header("Content-Disposition: attachment; filename='example.m4a' ");
fpassthru($file);
?>
  • No, `fopen()` just opens it within your code; it's `fpassthru()` that sends the file data to `php://output`; but a simple [readfile()](http://www.php.net/manual/en/function.readfile.php) is more efficient, because you don't need to open the file – Mark Baker Jun 16 '16 at 13:33
  • are u sure it doesnt downloads it @MarkBaker cuz if it does would stress my server –  Jun 16 '16 at 13:34
  • Read what I actually wrote: `fopen()` does not download any file; `fpassthru()` downloads a file/sends a file to php://output.... you're filename for `fopen()` is a literal `'link'`.... that's a reference to a file called `link` on your server, not a remote server, not the client browser – Mark Baker Jun 16 '16 at 13:54

1 Answers1

1

All the data will be read (from wherever the file handle points) by your PHP program on your server. That may involve copying data from a remote URL to your server. That data may exist entirely in RAM. It may hit swap space on the disk.

The PHP program then outputs it to the browser.

The browser never has direct access to 'link'.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • is there any way to give direct access –  Jun 16 '16 at 13:38
  • Use [an HTTP redirect](http://stackoverflow.com/questions/768431/how-to-make-a-redirect-in-php) (assuming that it is available a URL accessible to the browser in the first place). – Quentin Jun 16 '16 at 13:39
  • but that opens the file in the browser doesnt downloads it –  Jun 16 '16 at 13:44
  • Then you'd have to change the headers sent for the URL you are redirecting to. If that isn't under your control then you can't do that. – Quentin Jun 16 '16 at 13:45