Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP readfile() of external URL

Tags:

php

readfile

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');
like image 606
user583311 Avatar asked Jan 20 '11 20:01

user583311


People also ask

What is the use of Readfile ()?

The ReadFile function returns when one of the following conditions occur: The number of bytes requested is read. A write operation completes on the write end of the pipe. An asynchronous handle is being used and the read is occurring asynchronously.

How do I get the URL of a PHP file?

To get the current page URL, PHP provides a superglobal variable $_SERVER. The $_SERVER is a built-in variable of PHP, which is used to get the current page URL. It is a superglobal variable, means it is always available in all scope.

How do I echo content in PHP?

The file_get_contents function takes the name of the php file and reads the contents of the text file and displays it on the console. get the contents, and echo it out.

How do I read the contents of a file in PHP?

The file_get_contents() reads a file into a string. This function is the preferred way to read the contents of a file into a string. It will use memory mapping techniques, if this is supported by the server, to enhance performance.


2 Answers

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');
like image 126
Jacob Relkin Avatar answered Sep 30 '22 08:09

Jacob Relkin


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.

like image 32
wajiw Avatar answered Sep 30 '22 07:09

wajiw