Source for file file_get_contents.php
Documentation is available at file_get_contents.php
// $Id: file_get_contents.php,v 1.24 2007/04/17 10:09:56 arpad Exp $
define('PHP_COMPAT_FILE_GET_CONTENTS_MAX_REDIRECTS', 5 );
* Replace file_get_contents()
* @license LGPL - http://www.gnu.org/licenses/lgpl.html
* @copyright 2004-2007 Aidan Lister <aidan@php.net>, Arpad Ray <arpad@php.net>
* @link http://php.net/function.file_get_contents
* @author Aidan Lister <aidan@php.net>
* @author Arpad Ray <arpad@php.net>
* @version $Revision: 1.24 $
* @internal resource_context is only supported for PHP 4.3.0+ (stream_context_get_options)
* @require PHP 4.0.0 (user_error)
$colon_pos = strpos($filename, '://');
$wrapper = $colon_pos === false ? 'file' : substr($filename, 0 , $colon_pos);
$opts = (empty ($opts) || empty ($opts[$wrapper])) ? array () : $opts[$wrapper];
$max_redirects = (isset ($opts[$wrapper]['max_redirects'])
? $opts[$proto]['max_redirects']
for ($i = 0; $i < $max_redirects; $i++ ) {
$filename = rtrim($contents[1 ]);
user_error('redirect limit exceeded', E_USER_WARNING );
if (false === $fh = fopen($filename, 'rb', $incpath)) {
user_error('failed to open stream: No such file or directory',
$data = fread($fh, $fsize);
$data .= fread($fh, 8192 );
* @param string $filename
* the full path to request
* an array of stream context options
* either the contents of the requested path (as a string),
* or an array where $array[1] is the path redirected to.
if (!isset ($path['host'])) {
$fp = fsockopen($path['host'], 80 , $errno, $errstr, 4 );
if (!isset ($path['path'])) {
// enforce some options (proxy isn't supported)
'user_agent' => ini_get('user_agent'),
'request_fulluri' => false
foreach ($opts_defaults as $key => $value) {
if (!isset ($opts[$key])) {
$opts['path'] = $opts['request_fulluri'] ? $filename : $path['path'];
$request = $opts['method'] . ' ' . $opts['path'] . " HTTP/1.0\r\n";
if (isset ($opts['header'])) {
$optheaders = explode("\r\n", $opts['header']);
for ($i = count($optheaders); $i--; ) {
$sep_pos = strpos($optheaders[$i], ': ');
$headers[substr($optheaders[$i], 0 , $sep_pos)] = substr($optheaders[$i], $sep_pos + 2 );
foreach ($headers as $key => $value) {
$request .= " $key: $value\r\n";
$request .= "\r\n" . $opts['content'];
$response .= fgets($fp, 8192 );
$content_pos = strpos($response, "\r\n\r\n");
if (preg_match('/^Location: (.*)$/mi', $response, $matches)) {
return ($content_pos != -1 ? substr($response, $content_pos + 4 ) : $response);
Documentation generated on Mon, 11 Mar 2019 15:26:35 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|