-1

I need a php script that will open an external php file (from the same server folder), go through it line by line, and then normally display the page in the browser, as it would by just opening the external php page directly.

I need to open the external file line by line, so I can do some processing on the content of the file before showing it.

My current code is:

<?php
$handle = fopen("test.php", "r");
if ($handle) {
while (($line = fgets($handle)) !== false) {
    // process the line here, and change if needed
    echo "$line\n";
}
fclose($handle);
}
else {
// error opening the file.
}
?>

This works, and the page is displayed, but any php code in the original external file is not honored - it is written out as text, and not rendered by the browser.

I need the external file to fully display, just as it would if I opened the file (in this case "test.php") by itself.

Other questions I have seen on SO deal with opening or displaying a full file at once, but I need to loop through my file and do some processing on the contents first, so need to evaluate it line by line.

Any ideas would be appreciated.

Thanks

R2D2
  • 2,522
  • 3
  • 22
  • 41
  • Have you tried using "include 'vars.php';" not sure if I am understanding your question correctly – Dave Nov 17 '15 at 12:35
  • I would include the page and handle everything within the page and do all the processing in there, remember that depending on how you have implemented your code php can be both procedural or object oriented as far as I know, you could also pass in variables to the inner page using the include – Dave Nov 17 '15 at 12:39
  • What kind of processing are you doing? Do you want to process the PHP code before executing it? Or post-process the result of the PHP code? Why this post-processing to begin with? Can't you structure the code in that external differently to not need any such post-processing? – deceze Nov 17 '15 at 12:40

2 Answers2

1

I would save the changes to a temporary file, and then include it.

<?php
$handle = fopen("test.php", "r");
if ($handle) {
while (($line = fgets($handle)) !== false) {
    // process the line here, and change if needed
    $newCode .= "$line\n";
}
fclose($handle);
}
else {
// error opening the file.
}

// temporary file name
$temp_file = tempnam(sys_get_temp_dir(), 'myfile').".php";

// save modified code
file_put_contents($temp_file, $newCode);

// include modified code
include $temp_file;

// delete file
unlink($temp_file);

?>
Mario A
  • 3,128
  • 1
  • 12
  • 19
  • This seems a good method. The above code returns the following error "Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 458752 bytes) in /tmp/myfile7KSL8d.php on line 7909" – R2D2 Nov 17 '15 at 12:55
  • it seems your memory_limit is too low in php.ini. I guess the php file you are processing is quite huge. See this question for a solution: http://stackoverflow.com/questions/415801/allowed-memory-size-of-33554432-bytes-exhausted-tried-to-allocate-43148176-byte – Mario A Nov 17 '15 at 12:57
  • the external php file is just a standard web page, of less than 8kb :o) – R2D2 Nov 17 '15 at 13:00
  • It does have included css, jquery, etc - so maybe this script is trying to load all these or execute these external files?? – R2D2 Nov 17 '15 at 13:01
  • no, it will only include the rows in the php file (which seems to have more rows than 7909 according to the above errormessage). Try to increase the memory limit: `ini_set('memory_limit', '-1');` – Mario A Nov 17 '15 at 13:03
  • My fault, due to a silly error! This code works, and seems to be a good solution. Thanks! – R2D2 Nov 17 '15 at 13:06
0

Retrieve the content, process it, keep it in memory then eval() it:

<?php

$newCode = "";
$handle = fopen("test.php", "r");
if ($handle) {
while (($line = fgets($handle)) !== false) {

    // process the line here, and change if needed
    //$line = myLineProcess($line);

    $newCode .= "$line\n";
}
fclose($handle);
}
else {
// error opening the file.
}

//run the code
eval('?>'.$newCode.'<?php;');

?>
Bar-code
  • 276
  • 1
  • 6