This function is similar to file(), except that file_get_contents() returns the file in a string, starting at the specified offmix up lớn length bytes. On failure, file_get_contents() will return false.
file_get_contents() is the preferred way khổng lồ read the contents of a file inkhổng lồ 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().
filename Name of the file to lớn read.use_include_path
Note: The FILE_USE_INCLUDE_PATH constant can be used to lớn trigger include path search. This is not possible if strict typing is enabled, since FILE_USE_INCLUDE_PATH is an int. Use true instead.context
A valid context resource created with stream_context_create(). If you don't need to lớn use a custom context, you can skip this parameter by null.offphối
The offphối where the reading starts on the original stream. Negative offsets count from the kết thúc of the stream.
Seeking (offset) is not supported with remote files. Attempting khổng lồ seek on non-local files may work with small offsets, but this is unpredictable because it works on the buffered stream.length
Maximum length of data read. The mặc định is khổng lồ read until end of file is reached. chú ý that this parameter is applied to lớn the stream processed by the filters.
The function returns the read data or false on failure.
This function mayreturn Boolean false, but may also return a non-Boolean value whichevaluates to false. Please read the section on Booleans for moreinformation. Use the ===operator for testing the return value of thisfunction.
Bạn đang xem: Get file content from url?
An E_WARNING level error is generated if filename cannot be found, length is less than zero, or if seeking lớn the specified offset in the stream fails.
When file_get_contents() is called on a directory, an E_WARNING màn chơi error is generated on Windows, & as of darkedeneurope.com 7.4 on other operating systems as well.
|7.1.0||Support for negative offsets has been added.|
Example #1 Get and output the source of the homepage of a website
Example #2 Searching within the include_path
Example #3 Reading a section of a file
The above example will outputsomething similar to:
string(14) "lle Bjori Ro"
Example #4 Using stream contexts
//Createastream$opts=array("http"=>array("method"=>"GET","header"=>"Accept-language:en "."Cookie:foo=bar "));$context=stream_context_create($opts);//OpenthefileusingtheHTTPheaderssetabove$file=file_get_contents("http://www.example.com/",false,$context);?>
Note: This function isbinary-safe.
A URL can be used as afilename with this function if the fopen wrappers have sầu been enabled.See fopen() for more details on how to specify thefilename. See the Supported Protocols và Wrappers for link khổng lồ informationabout what abilities the various wrappers have sầu, notes on their usage,và information on any predefined variables they mayprovide.
When using SSL, Microsoft IISwill violate the protocol by closing the connection without sending aclose_notify indicator. darkedeneurope.com will report this as "SSL: FatalProtocol Error" when you reach the kết thúc of the data. To work around this, thevalue of error_reporting should belowered lớn a màn chơi that does not include warnings.darkedeneurope.comhường. can detect buggy IIS server software when you openthe stream using the https:// wrapper & will suppress thewarning. When using fsockopen() to create anssl:// socket, the developer is responsible for detectingand suppressing this warning.