13

Can I use external URLs in readfile()?

    header('Content-type: application/pdf');
    header('Content-Transfer-Encoding: binary');
    header('Content-Disposition: inline; filename="'.$file.'" ');
    //header('Content-Length: ' . filesize("http:...z/pub/".$file.'.pdf'));
    @readfile("http://...z/pub/".$file.'.pdf');
2
  • 2
    Try it and/or RTM: "A URL can be used as a filename with this function if the fopen wrappers have been enabled." ;-) Commented Jan 20, 2011 at 20:49
  • Should I do 'exec("wget $url -O $filename")'? Commented Jan 20, 2011 at 20:51

2 Answers 2

23

The PHP manual on readfile states:

A URL can be used as a filename with this function if the fopen wrappers have been enabled. See fopen() for more details on how to specify the filename. See the Supported Protocols and Wrappers for links to information about what abilities the various wrappers have, notes on their usage, and information on any predefined variables they may provide.

As an alternative you can also use file_get_contents:

echo file_get_contents("http://...z/pub/".$file.'.pdf');
Sign up to request clarification or add additional context in comments.

8 Comments

In both solutions there is some problem with filesize or something else, because is not working.
@user583311 What seems to be the problem?
@user583311: 'not working' is pretty vague. At least remove the error suppressing operator (@) so that you can see (and tell us) WHY it is not working
It downloads a file with 300b, and the original has 300Kb. And the script works if the URL is local. So, must be something about external URL.
What are the contents of this file? I'm pretty sure you can open it in text editor and see PHP's error message.
|
3

Yes, according to the readfile page:

A URL can be used as a filename with this function if the fopen_wrappers have been enabled. See fopen() for more details on how to specify the filename. See the Supported Protocols and Wrappers for links to information about what abilities the various wrappers have, notes on their usage, and information on any predefined variables they may provide.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.