|
file_get_contents
Reads entire file into a string
(PHP 4 >= 4.3.0, PHP 5)
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 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.
Note:
If you're opening a URI with special characters, such as spaces, you need to encode the URI with urlencode(). Parameters
ChangeLog
Notes
Note:
This function is binary-safe.
Tip:
You can use a URL as a filename with this function if the fopen wrappers have been enabled. See fopen() for more details on how to specify the filename and Appendix O, List of Supported Protocols/Wrappers for a list of supported URL protocols.
Warning:
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 workaround this, you should lower your error_reporting level not to 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 for you. If you are using fsockopen() to create an ssl:// socket, you are responsible for detecting and suppressing the warning yourself. Related Examples ( Source code ) » file_get_contents Examples ( Source code ) » Read text file into array and output Examples ( Source code ) » Display XML file Examples ( Source code ) » FeedAggregator using HttpRequest Examples ( Source code ) » SAX Parser Code Examples / Notes » file_get_contentsgreg ambrose greg
[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("http://abcxyz.com"); Other times like execution time and input time had no effect. tobsn
you'll find the http response headers in: $http_response_header ;o) siegfri3d
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: <? $content=file_get_contents("http://www.google.com",FALSE,NULL,0,20); echo $content; ?> This will echo the 20 first bytes of the google.com source code. aidan
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: http://pear.php.net/package/PHP_Compat fcicqbbs
the bug #36857 was fixed. http://bugs.php.net/36857 Now you may use this code,to fetch the partial content like this: <?php $context=array('http' => array ('header'=> 'Range: bytes=1024-', ),); $xcontext = stream_context_create($context); $str=file_get_contents("http://www.fcicq.net/wp/",FALSE,$xcontext); ?> that's all. richard dot quadling
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 http://apserver.sourceforge.net/. You will need Python from http://www.python.org/. 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. <?php // Define a context for HTTP. $aContext = array( 'http' => array( 'proxy' => 'tcp://127.0.0.1:8080', // 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("http://www.php.net", False, $cxContext); echo $sFile; ?> Hopefully this helps SOMEONE!!! bearachute
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! |
Change Languagebasename chgrp chmod chown clearstatcache copy delete dirname disk_free_space disk_total_space diskfreespace fclose feof fflush fgetc fgetcsv fgets fgetss file_exists file_get_contents file_put_contents file fileatime filectime filegroup fileinode filemtime fileowner fileperms filesize filetype flock fnmatch fopen fpassthru fputcsv fputs fread fscanf fseek fstat ftell ftruncate fwrite glob is_dir is_executable is_file is_link is_readable is_uploaded_file is_writable is_writeable lchgrp lchown link linkinfo lstat mkdir move_uploaded_file parse_ini_file pathinfo pclose popen readfile readlink realpath rename rewind rmdir set_file_buffer stat symlink tempnam tmpfile touch umask unlink |