The Server Pages
:: TSP Menu
- Home
- Authors
- Glossary
- RSS Feed

:: Servers
- CPanel
- Hosting
- Linux
- Windows

:: Webmaster
- Databases
- Design
- Search Engines

:: Scripts
- Scripts

:: Documentation
- PHP Manual

:: Other
- Errors & Solutions
- TechPunt
- Wojjie

search for in the

file_put_contents> <file_exists
[edit] Last updated: Sat, 12 May 2012

view this page in


(PHP 4 >= 4.3.0, PHP 5)

file_get_contentsReads entire file into a string


string file_get_contents ( string $filename [, bool $use_include_path = false [, resource $context [, int $offset = -1 [, int $maxlen ]]]] )

This function is similar to file(), except that file_get_contents() returns the file in a string, starting at the specified offset up to maxlen bytes. On failure, file_get_contents() will return FALSE.

file_get_contents() is the preferred way to read the contents of a file into a string. It will use memory mapping techniques if supported by your OS to enhance performance.


If you're opening a URI with special characters, such as spaces, you need to encode the URI with urlencode().



Name of the file to read.



As of PHP 5 the FILE_USE_INCLUDE_PATH can be used to trigger include path search.


A valid context resource created with stream_context_create(). If you don't need to use a custom context, you can skip this parameter by NULL.


The offset where the reading starts on the original stream.

Seeking (offset) is not supported with remote files. Attempting to seek on non-local files may work with small offsets, but this is unpredictable because it works on the buffered stream.


Maximum length of data read. The default is to read until end of file is reached. Note that this parameter is applied to the stream processed by the filters.

Return Values

The function returns the read data or FALSE on failure.


An E_WARNING level error is generated if either maxlength is less than zero, or if offset exceeds the length of the stream.


Example #1 Get and output the source of the homepage of a website


Example #2 Searching within the include_path

// <= PHP 5
$file file_get_contents('./people.txt'true);
// > PHP 5
$file file_get_contents('./people.txt'FILE_USE_INCLUDE_PATH);

Example #3 Reading a section of a file

// Read 14 characters starting from the 21st character
$section file_get_contents('./people.txt'NULLNULL2014);

The above example will output something similar to:

string(14) "lle Bjori Ro" 

Example #4 Using stream contexts

// Create a stream
$opts = array(
'header'=>"Accept-language: en\r\n" .
"Cookie: foo=bar\r\n"

$context stream_context_create($opts);

// Open the file using the HTTP headers set above
$file file_get_contents(''false$context);


Version Description
5.1.0 Added the offset and maxlen parameters.
5.0.0 Added context support.


Note: This function is binary-safe.


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.


When using SSL, Microsoft IIS will violate the protocol by closing the connection without sending a close_notify indicator. PHP will report this as "SSL: Fatal Protocol Error" when you reach the end of the data. To work around this, the value of error_reporting should be lowered to a level that does not include warnings. PHP 4.3.7 and higher can detect buggy IIS server software when you open the stream using the https:// wrapper and will suppress the warning. When using fsockopen() to create an ssl:// socket, the developer is responsible for detecting and suppressing this warning.

See Also

file_put_contents> <file_exists
[edit] Last updated: Sat, 12 May 2012
add a note add a note User Contributed Notes file_get_contents
dustin at dumontproject dot com-spam sux 25-Apr-2012 09:25
For those who use file_get_contents for JSON or other RESTful services - like my architecture did for a big site - this will probably help a lot.

We struggled with having the site using get urls that would go through our load balancer instead of hitting the local server.

What we did was load this function through a local url and set the Host: header for our virtualhost entries on the site we wanted to laod.

This code solved our issue:

//set the header context stream for virtualhost lookup
$context = stream_context_create(array('http' => array('header' => 'Host:')));

//use a localhost url or alternatively ip
$url = 'http://localhost/rest-service/?get-user&id=######';

//fetch the data through webserver using the Host http header we set
$data = json_decode(file_get_contents($url, 0, $context));

//verify you have your data

Bart Friederichs 16-Apr-2012 12:17
file_get_contents can do a POST, create a context for that first:

$opts = array('http' =>
    'method'  => 'POST',
    'header'  => "Content-Type: text/xml\r\n".
      "Authorization: Basic ".base64_encode("$https_user:$https_password")."\r\n",
    'content' => $body,
    'timeout' => 60
$context  = stream_context_create($opts);
$url = 'https://'.$https_server;
$result = file_get_contents($url, false, $context, -1, 40000);
pperegrina 21-Dec-2011 11:30
For those having this problem when trying to get_file_contents(url):

Warning: file_get_contents(url): failed to open stream: HTTP request failed!  in xx on line yy

If you are behind a SonicWall firewall, read this:
(this little line: uncheck a box in the internal settings of the firewall labled "Enforce Host Tag Search with for CFS")

Apparently by default SonicWall blocks any HTTP request without a "Host:" header, which is the case in the PHP get_file_contents(url) implementation.

This is why, if you try to get the same URL from the same machine with cURL our wget, it works.

I hope this will be useful to someone, it took me hours to find out :)
godwraith01 at yahoo dot com 11-Oct-2011 04:16
I experienced a problem in using hostnames instead straight IP with some server destinations.

If i use file_get_contents("www.jbossServer.example/app1",...)
i will get an 'Invalid hostname' from the server i'm calling.

This is because file_get_contents probably will rewrite your request after getting the IP, obtaining the same thing as :

And you know that many servers will deny you access if you go through IP addressing in the request.

With cURL this problem doesn't exists. It resolves the hostname leaving the request as you set it, so the server is not rude in response.
fibrefox at dynamicfiles dot de 05-Jul-2011 05:45
If you want to insert tracking-scripts into your shopping-system, some scripts doesn't support intelligent detection of HTTPS, so i made a script i put on the server that rewrites 'http' to 'https' in the script, assuming everything has to be UTF-8 encoded (as a fallback it makes a redirect).

It is important that the HTTPS-source DOES exist!


function file_get_contents_utf8($fn) {
$opts = array(
'http' => array(
'header'=>"Content-Type: text/html; charset=utf-8"

$context = stream_context_create($opts);
$result = @file_get_contents($fn,false,$context);
header("Content-Type: text/html; charset=utf-8");
$result = file_get_contents_utf8("http://".$tPath);

$result == false){
header("Location: https://".$tPath); // fallback
andre-s at home dot nl 18-Jun-2011 12:46
If you are getting a failed to open stream message on your windows machine check your hosts file.       localhost

must be in it and the IP6 line must be commented

#    ::1             localhost
eric at midkotasolutions dot com 04-Mar-2011 11:26
At least as of PHP 5.3, file_get_contents no longer uses memory mapping.

See comments on this bug report:
Stas Trefilov, OpteamIS 22-Feb-2011 07:30
here is another (maybe the easiest) way of doing POST http requests from php using its built-in capabilities. feel free to add the headers you need (notably the Host: header) to further customize the request.

note: this method does not allow file uploads. if you want to upload a file with your request you will need to modify the context parameters to provide multipart/form-data encoding (check out ) and build the $data_url following the guidelines on

make an http POST request and return the response content and headers
@param string $url    url of the requested script
@param array $data    hash array of request variables
@return returns a hash array with response content and headers in the following form:
    array ('content'=>'<html></html>'
        , 'headers'=>array ('HTTP/1.1 200 OK', 'Connection: close', ...)
function http_post ($url, $data)
$data_url = http_build_query ($data);
$data_len = strlen ($data_url);

    return array (
'content'=>file_get_contents ($url, false, stream_context_create (array ('http'=>array ('method'=>'POST'
, 'header'=>"Connection: close\r\nContent-Length: $data_len\r\n"
, 'content'=>$data_url
Yoga Wibowo Aji 08-Feb-2011 09:10
read text per line and convert to array

for example, the input file is input.txt
the input file containt text below



read value per line

= file_get_contents("input.txt"); //read the file
$convert = explode("\n", $data); //create array separate by new line

for ($i=0;$i<count($convert);$i++) 
$convert[$i].', '; //write value by index


Output :

one, two, three, four, five,
john_schlick at hotmail dot com 27-Jan-2011 12:25
For those that need to know the default of $maxLen, it's defined in the php source code as PHP_STREAM_COPY_ALL, and because that is not available to us mere mortal users, that constant is defined as  ((size_t)-1) or -1
contact at webapp dot fr 31-Dec-2010 03:57
When using a URI with a login / password (HTTP or FTP, for an example), you may need to urlencode the password if it contains special characters.
Do not urlencode the whole URI, just the password.

Don't do :

Do :
'ftp://login:' . urlencode('mdp%?special') . '@host/dir/file';

Might seem obvious, but is worth noting.
slymak at gmail dot com 06-Dec-2010 03:07
If working file is bigger than 64kb and you getting deadlock. Your buffer is overflow. Here are two way how to avoid that.
1) use temporary file for descriptor
= array(
0 => array("file", "/tmp/ens/a.ens","r"),  // stdin is a pipe that the child will read from
1 => array("file", "/tmp/ens/a.html","w"),  // stdout is a pipe that the child will write to
2 => array("file", "/tmp/ens/error-output.txt", "a") // stderr is a file to write to

2) inline read using stream_set_blocking. PHP doesn't proper handle last part of file.
= 64*1024;

$url = "".$db."/".$member."/contents";
$src = fopen($url,"r");

$cwd = '/tmp';
$cmd['enscript'] = "/usr/bin/enscript";
$cmd['enscript-options'] = " -q --language=html --color -Ejcl -o -";
$descriptorspec = array(
0 => array("pipe", "r"),  // stdin is a pipe that the child will read from
1 => array("pipe", "w")   // stdout is a pipe that the child will write to

$ph=proc_open($cmd['enscript']." ".$cmd['enscript-options'],$descriptorspec,$pipes,$cwd);


$CMD_OUT_OPEN = TRUE; $k = 0;
  while (!
feof($pipes[1]) || !feof($src) || $k > 0) {
    if (!
feof($src) && $k+$READ_LEN <= $MAX_BUF_LEN) {
$input .= fread($src,$READ_LEN);
$k = strlen($input);
    if (
$k > 0) {
$l = fwrite($pipes[0],$input);
$k -= $l;
$input = substr($input,$l);
    if (
$CMD_OUT_OPEN && $k == 0 && feof($src)) {
$output = fread($pipes[1],$READ_LEN);
$outputn = str_replace("<H1>(stdin)</H1>","",$output);


$return_value = proc_close($ph);
Anonymous 04-Dec-2010 07:33
The offset is 0 based.  Setting it to 1 will skip the first character of the stream.
kam 26-Nov-2010 03:18
Recently I experienced unexpected behaviour (for me) of file_get_contents('https://......')
Script gets images and file_get_contents('https://......') is in loop operator.
After several iterations Apache Server FastCGI timed out with Internal Server Error in ~30 seconds even if max_execution_time was set to 0. With fopen() fread() pair result was the same.
In different environment Apache mod_php the same script worked without problems.
I solved this changing https requests to http
KrisWebDev 21-Nov-2010 01:17
If your file_get_contents freezes during several seconds, here is maybe your answer:

Beware that the default keepalive timeout of Apache 2.0 httpd is 15 seconds. This is true for HTTP/1.1 connections, which is not the default behavior of file_get_contents but you can force it, especially if you are trying to act as a web browser. I don't know if this is also the case for HTTP/1.0 connections.

Forcing the server to close the connection would make you gain those 15 seconds in your script:

= stream_context_create(array('http' => array('header'=>'Connection: close')));
$content = file_get_contents("");

Another way of resolving slowness issues is to use cURL or fsockopen. Bear in mind that contrary to the behavior of web browsers, file_get_contents doesn't return the result when the web page is fully downloaded (i.e. HTTP payload length = value of the response HTTP "Content-Length" header) but when the TCP connection is closed.
I hope this behavior will change in future releases of PHP.
This has been experienced with PHP 5.3.3.
vlad dot wing at gmail dot com 27-Mar-2010 02:03
If you want to check if the function returned error, in case of a HTTP request an, it's not sufficient to test it against false. It may happen the return for that HTTP request was empty. In this case it's better to check if the return value is a bool.

if (
$result === false)
// treat error
} else {
// handle good case

[EDIT BY thiago: Has enhacements from an anonymous user]
rutger at webjin dot nl 14-Jan-2010 06:22
Sometimes you might get an error opening an http URL.
even though you have set "allow_url_fopen = On" in php.ini

For me the the solution was to also set "user_agent" to something.
ken at wetken dot net 28-Oct-2009 05:00
On Centos 5, and maybe other Red Hat based systems, any attempt to use file_get_contents to access a URL on an http  port other than 80 (e.g. "") may fail with a permissions violation (error 13) unless the box you are running php on has its seLinux set to 'permissive' not 'enforcing' . Otherwise the request doesn't even get out of the box, i.e. the permissions violation is generated locally by seLinux.
corey at effim dot com 09-Jun-2009 02:35
In my dev environment with a relatively low-speed drive (standard SATA 7200RPM) reading a 25MB zip file in 10 times...


= `cat /tmp/`;
// 1.05 seconds

$fh = fopen('/tmp/', 'r');
$data = fread($fh, filesize('/tmp/'));
// 1.31 seconds

$data = file_get_contents('/tmp/');
// 1.33 seconds


However, on a 21k text file running 100 iterations...


= `cat /tmp/test.txt`;
// 1.98 seconds

$fh = fopen('/tmp/test.txt', 'r');
$data = fread($fh, filesize('/tmp/test.txt'));
// 0.00082 seconds

$data = file_get_contents('/tmp/test.txt');
// 0.0069 seconds


Despite the comment about file_get_contents being faster do to memory mapping, file_get_contents is slowest in both of the above examples. If you need the best performance out of your production box, you might want to throw together a script to check out which method is fastest for what size files on that particular machine, then optimize your code to check the file size and use the appropriate function for it.
colnector bla-at_bla 10-Aug-2008 02:34
A UTF-8 issue I've encountered is that of reading a URL with a non-UTF-8 encoding that is later displayed improperly since file_get_contents() related to it as UTF-8. This small function should show you how to address this issue:

function file_get_contents_utf8($fn) {
$content = file_get_contents($fn);
mb_convert_encoding($content, 'UTF-8',
mb_detect_encoding($content, 'UTF-8, ISO-8859-1', true));
EOD 07-Aug-2008 05:34
if $filename has a relative path file_get_contents returns the uninterpreted sourcecode of the php-file with all comments etc.

I don't know whether this is a bug or intented or caused by server-configuration.

I think this behaviour should be included in the description of the function.
daniele dot ricci at staff dot dada dot net 05-Aug-2008 04:13
I recently upgraded my server to Slackware 12.0.

After this, a program of mine stopped working: the call to file_get_contents (to an URL served by a custom HTTP server) was returning false without generating any error!

After some investigations I saw this: my custom HTTP server closes the connection at the end of the content. This (without the header "Connection: close") seems to cause the problem I described.

To solve the problem I simply added that header to the answer of my custom HTTP server.
joachimb at gmail dot com 15-Apr-2008 02:38
Setting the timeout properly without messing with ini values:

= stream_context_create(array(
'http' => array(
'timeout' => 1
file_get_contents("", 0, $ctx);
3n1gm4 [at] gmail [dot] com 02-Apr-2008 01:12
This is a nice and simple substitute to get_file_contents() using curl, it returns FALSE if $contents is empty.

function curl_get_file_contents($URL)
$c = curl_init();
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_URL, $URL);
$contents = curl_exec($c);

        if (
$contents) return $contents;
            else return

Hope this help, if there is something wrong or something you don't understand let me know :)
php [spat] 15-Jan-2008 03:58
I decided to make a similar function to this, called file_post_contents, it uses POST instead of GET to call, kinda handy...

function file_post_contents($url,$headers=false) {
$url = parse_url($url);

    if (!isset(
$url['port'])) {
      if (
$url['scheme'] == 'http') { $url['port']=80; }
      elseif (
$url['scheme'] == 'https') { $url['port']=443; }


$headers "POST ".$url['protocol'].$url['host'].$url['path']." HTTP/1.0".$eol.
"Host: ".$url['host'].$eol.
"Referer: ".$url['protocol'].$url['host'].$url['path'].$eol.
"Content-Type: application/x-www-form-urlencoded".$eol.
"Content-Length: ".strlen($url['query']).$eol.
$fp = fsockopen($url['host'], $url['port'], $errno, $errstr, 30);
$fp) {
fputs($fp, $headers);
$result = '';
feof($fp)) { $result .= fgets($fp, 128); }
      if (!
$headers) {
//removes headers
francois hill 03-Dec-2007 08:56
Seems file looks for the file inside the current working (executing) directory before looking in the include path, even with the FILE_USE_INCLUDE_PATH flag specified.

Same behavior as include actually.

By the way I feel the doc is not entirely clear on the exact order of inclusion (see include). It seems to say the include_path is the first location to be searched, but I have come across at least one case where the directory containing the file including was actually the first to be searched.

bearachute at gmail dot com 11-Jul-2007 02:38
If you're having problems with binary and hex data:

I had a problem when trying to read information from a ttf, which is primarily hex data. A binary-safe file read automatically replaces byte values with their corresponding ASCII characters, so I thought that I could use the binary string when I needed readable ASCII strings, and bin2hex() when I needed hex strings.

However, this became a problem when I tried to pass those ASCII strings into other functions (namely gd functions). var_dump showed that a 5-character string contained 10 characters, but they weren't visible. A binary-to-"normal" string conversion function didn't seem to exist and I didn't want to have to convert every single character in hex using chr().

I used unpack with "c*" as the format flag to see what was going on, and found that every other character was null data (ordinal 0). To solve it, I just did

str_replace(chr(0), "", $string);

which did the trick.

This took forever to figure out so I hope this helps people reading from hex data!
tobsn at php dot net 01-May-2007 09:26
you'll find the http response headers in: $http_response_header

Greg Ambrose (greg at catalina-it dot com dot au) 16-Apr-2007 09:37
[Editors note: As of PHP 5.2.1 you can specify `timeout` context option and pass the context to file_get_contents()]

The only way I could get get_file_contents() to wait for a very slow http request was to set the socket timeout as follows.

 ini_set('default_socket_timeout',    120);   
$a = file_get_contents("");

Other times like execution time and input time had no effect.
siegfri3d at gmail dot com 05-Dec-2006 12:52
Use the previous example if you want to request the server for a special part of the content, IF and only if the server accepts the method.
If you want a simple example to ask the server for all the content, but only save a portion of it, do it this way:

This will echo the 20 first bytes of the source code.
fcicqbbs at gmail dot com 04-Aug-2006 01:55
the bug #36857 was fixed.

Now you may use this code,to fetch the partial content like this:
=array('http' => array ('header'=> 'Range: bytes=1024-', ),);
$xcontext = stream_context_create($context);
that's all.
richard dot quadling at bandvulc dot co dot uk 15-Nov-2005 02:47
If, like me, you are on a Microsoft network with ISA server and require NTLM authentication, certain applications will not get out of the network. SETI@Home Classic and PHP are just 2 of them.

The workaround is fairly simple.

First you need to use an NTLM Authentication Proxy Server. There is one written in Python and is available from You will need Python from

Both sites include excellent documentation.

Python works a bit like PHP. Human readable code is handled without having to produce a compiled version. You DO have the opportunity of compiling the code (from a .py file to a .pyc file).

Once compiled, I installed this as a service (instsrv and srvany - parts of the Windows Resource Kit), so when the server is turned on (not logged in), the Python based NTLM Authentication Proxy Server is running.

Then, and here is the bit I'm really interested in, you need to tell PHP you intend to route http/ftp requests through the NTLM APS.

To do this, you use contexts.

Here is an example.


// Define a context for HTTP.
$aContext = array(
'http' => array(
'proxy' => 'tcp://', // This needs to be the server and the port of the NTLM Authentication Proxy Server.
'request_fulluri' => True,
$cxContext = stream_context_create($aContext);

// Now all file stream functions can use this context.

$sFile = file_get_contents("", False, $cxContext);


Hopefully this helps SOMEONE!!!
aidan at php dot net 30-Jan-2005 11:23
This functionality is now implemented in the PEAR package PHP_Compat.

More information about using this function without upgrading your version of PHP can be found on the below link:

show source | credits | sitemap | contact | advertising | mirror sites