Viewed   87 times

I want to get contents of a .php file in a variable on other page.

I have two files, myfile1.php and myfile2.php.


    $myvar="prashant"; // 
    echo $myvar;

Now I want to get the value echoed by the myfile2.php in an variable in myfile1.php, I have tried the follwing way, but its taking all the contents including php tag () also.

    $root_var .= file_get_contents($_SERVER['DOCUMENT_ROOT']."/myfile2.php", true);

Please tell me how I can get contents returned by one PHP file into a variable defined in another PHP file.




You can use the include directive to do this.

File 2:


File 1:


echo $myvar;

Tuesday, September 13, 2022

Provided allow_url_fopen is enabled, you can just use file_get_contents :

$my_var = file_get_contents('');

And, if you need more options, take a look at Stream Functions -- there is an example on the stream_context_create manual page where a couple of HTTP-headers are set.

If allow_url_fopen is disabled, another solution is to work with curl -- means a couple more lines of code, though.

Something as basic as this should work in the simplest situations :

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://.com/");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$my_var = curl_exec($ch);

But note that you might need some additional options -- see the manual page of curl_setopt for a complete list.

For instance :

  • I often set CURLOPT_FOLLOWLOCATION, so redirects are followed.
  • The tiemout-related options are quite often useful too.
Tuesday, August 23, 2022
$url = '';

// using file_get_contents function
$content = file_get_contents($url);
echo $content;
#output# "who is the Rector of the University"

// using file function // read line by line in array
$content = file($url);

#output# Array (0] => ?"who is the Rector of the University")

// using cURL
$ch = curl_init($url);  
curl_setopt($ch, CURLOPT_HEADER, 0);  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
$content = curl_exec($ch);
echo $content;
#output# "who is the Rector of the University"
Wednesday, November 30, 2022

For the benefit of anyone running into this thread I give below the fully functional script I created. It assumes that you have created and shared your public ssh_key with the remote server (EVBackup in my case) as described here.

In my case I had to deal with one additional complication - I am in the process of setting up the first of my servers but will have several others. Their domain names bear the form where N is 1, 2, 3 etc.

On my EVBackup account I have created folders bearing the same names, sv1, sv2 etc. I wanted to create a script that would safely run from any of my servers. Here is that script with some comments

#! /bin/bash
# Backup to EVBackup Server

#replace dbname as appropriate

svr="$(uname -n)"
#strip out the bit to get the destination folder on the remote server

#user will have to be replaced with your username

cmd='rsync -avz -e "ssh -i /backup/ssh_key" '
#your ssh key location may well be different

#at this point cmd will be something like
#rsync -avz -e "ssh -i /backup/ssh_key" /home

eval $cmd
Tuesday, November 1, 2022

To summerize, I need to open huges compressed files (> 4GB) so the technique of Dan won't work and I want the length (number of line) of the file so the technique of Mark Adler is not appropriate.

Eventually, I found for uncompressed files a solution( not the most optimized but it works!) which can be transposed easily to compressed files:

size = 0

with as f:
  for line in f:
    size+= 1

return size

Thank you all, people in this forum are very effective!

Saturday, August 20, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :