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.

myfile2.php

<?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.

<?PHP
    $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.

Thanks

 Answers

4

You can use the include directive to do this.

File 2:

<?php
    $myvar="prashant";
?>

File 1:

<?php 

include('myfile2.php');
echo $myvar;

?>
Tuesday, September 13, 2022
 
falyna
 
1

Provided allow_url_fopen is enabled, you can just use file_get_contents :

$my_var = file_get_contents('http://yoursite.com/your-page.html');

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);
curl_close($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
4
$url = 'http://api.microsofttranslator.com/V2/Ajax.svc/Translate?text=siapa+rektor+ipb&appId=58C40548A812ED699C35664525D8A8104D3006D2&from=id&to=en';

// 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);
print_r($content);

#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
 
3

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 svN.example.com 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

local="/var/lib/automysqlbackup/daily/dbname"
#replace dbname as appropriate

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

evb="user@user.evbackup.com:"
#user will have to be replaced with your username
evb+=$remote

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

cmd+=$local
cmd+=$evb
#at this point cmd will be something like
#rsync -avz -e "ssh -i /backup/ssh_key" /home bob@bob.evbackup.com:home-data

eval $cmd
Tuesday, November 1, 2022
 
retro
 
5

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 gzip.open(data_file) as f:
  for line in f:
    size+= 1
    pass

return size

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

Saturday, August 20, 2022
 
sarneeh
 
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 :