Page 1 of 1
fopen php files
Posted: Mon Feb 26, 2007 4:14 pm
by SidewinderX
Hello, im trying to use fopen to open a local php file. Something as simple as this should get my point accrost
Code: Select all
<?php
$filename = "C:\\www\\test.php";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);
echo $contents;
?>
but $contents dosnt actually display anything, if i look at the page source the content of the php file will be there but i want the content to be displayed in the browser.
NOTE: This file is being opened locally - NOT over the internet
Posted: Mon Feb 26, 2007 4:17 pm
by veridicus
Try running the path through file_exists to see if the web server and php can see the file. Could be a permissions thing, also.
Posted: Mon Feb 26, 2007 4:20 pm
by SidewinderX
Well im on windows, so i doubt it is an issue with the permissions, and being that the content of the php file is the html source of the page i assume the web server and php can see the file
Posted: Mon Feb 26, 2007 4:24 pm
by veridicus
Instead of fopen/fclose you could try file_get_contents. Shouldn't make a difference, but can't hurt. Less code needed, so less chance of error.
Posted: Mon Feb 26, 2007 4:27 pm
by SidewinderX
It still does the same thing - displays the content of the php file as the html source
Posted: Mon Feb 26, 2007 4:34 pm
by veridicus
Oh, I see. I think I misunderstood. If you want PHP to execute $contents simply use eval($contents); Just don't eval the contents of the current script or you'll have an infinite loop.

Posted: Mon Feb 26, 2007 4:34 pm
by s.dot
.. which is what it should do!
(if by html source, you mean everything included (not the output of the file))
Posted: Mon Feb 26, 2007 4:42 pm
by SidewinderX
Well maybe I wasn't clear enough. Suppose the name of file that contains this code below is named test.php
Code: Select all
<?php
$filename = "C:\\www\\test.php";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);
echo $contents;
?>
and go to
http://localhost/test.php all i see is a blank page. If I go to View => Page Source i see
Code: Select all
<?php
$filename = "C:\\www\\test.php";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);
echo $contents;
?>
But I dont want to have to go to View => Page Source to see that, I want it to be displayed in the browser, something similar to what this would do
Code: Select all
echo show_source("C:\\www\\test.php");
but without all the fancy syntax highlighting
Posted: Mon Feb 26, 2007 4:47 pm
by feyd
Posted: Mon Feb 26, 2007 5:10 pm
by SidewinderX
perfect
thank you