fopen php files

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
SidewinderX
Forum Contributor
Posts: 407
Joined: Fri Jul 16, 2004 9:04 pm
Location: NY

fopen php files

Post 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
User avatar
veridicus
Forum Commoner
Posts: 86
Joined: Fri Feb 23, 2007 9:16 am

Post 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.
SidewinderX
Forum Contributor
Posts: 407
Joined: Fri Jul 16, 2004 9:04 pm
Location: NY

Post 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
User avatar
veridicus
Forum Commoner
Posts: 86
Joined: Fri Feb 23, 2007 9:16 am

Post 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.
SidewinderX
Forum Contributor
Posts: 407
Joined: Fri Jul 16, 2004 9:04 pm
Location: NY

Post by SidewinderX »

It still does the same thing - displays the content of the php file as the html source
User avatar
veridicus
Forum Commoner
Posts: 86
Joined: Fri Feb 23, 2007 9:16 am

Post 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. :)
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

.. which is what it should do!

(if by html source, you mean everything included (not the output of the file))
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
SidewinderX
Forum Contributor
Posts: 407
Joined: Fri Jul 16, 2004 9:04 pm
Location: NY

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

SidewinderX
Forum Contributor
Posts: 407
Joined: Fri Jul 16, 2004 9:04 pm
Location: NY

Post by SidewinderX »

perfect :!:

thank you
Post Reply