file_get_contents() in PHP to open file to read

file_get_contents return the output of a file as displayed in browser. Here it is
echo file_get_contents(url);
In addition to the URL we can also use addtinal ( optional ) parameters.

include_path, context, start, max_length.

We will use them and develop a sample script

We will keep our function in one main file and try to read another file by sending request. In second file we will keep minimum code to just return a welcome message with name. From our main file we will send the name by using GET method.

Here is a sample code to get output content from a file. While sending the request we will send name variable and try to get the welcome message with the name. We have used localhost for this. You should use your host or path in this code.
$v1=urlencode('Alex John');
echo $str;
echo "<hr>";
echo file_get_contents($str,1,null,0,20);
Here is the second file where we will read the output file_get_contents-demo.php.
$v1=$_GET['name']; // collect from url 
echo "Welcome $v1";
You can change the output by changing the start and max_length optional parameters of the function.

Getting external URL content

This is a security risk to allow file_get_contents(). It can be used to get content from external URL if allow_url_fopen is set to On in php.ini file.

Reading external url by curl

Reading a file by fread()

Subscribe to our YouTube Channel here


* indicates required
Subscribe to plus2net

    Post your comments , suggestion , error , requirements etc here .

    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2023 All rights reserved worldwide Privacy Policy Disclaimer