Forum Moderators: coopster

Message Too Old, No Replies

PHP remote file get contents, file, include not working

         

dtmuk

11:53 pm on Aug 10, 2006 (gmt 0)

10+ Year Member



I have a dedicated Fedora Core 4 box with a typical LAMP setup

I have a strange problem that is really annoying me!

Take test.php
<?
$data = file_get_contents("http://example.com/example.html");
echo $data;
?>

Goto [localhost...] in a web browser and nothing at all happens! Blank Page...

Goto the command line and type "php ./test.php" and theres the contents of example.html

OK maybe this is to do with permissions so lets see who the php scripts are running as
<?
$output=shell_exec("whoami");
echo $output;
?>
It gives "apache apache"

Lets look up the user id of apache type "cat /etc/passwd"

ok apache has the user id of 48

chown 48 ./test.php
why not chmod 777 ./test.php for the hell of it

Goto [localhost...] in a web browser and nothing at all happens again! Blank Page...

Any Ideas!?!

I did the exact same with a test.pl to do the same and it worked from command line but not from a web browser

dreamcatcher

7:25 am on Aug 11, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi dtmuk,

Do you have short tags enabled in your PHP.ini file?

Have you tried:


<?php
$data = file_get_contents("http://example.com/example.html");
echo $data;
?>

dc

dtmuk

3:18 pm on Aug 11, 2006 (gmt 0)

10+ Year Member



Yeah they are enabled as other scripts work with just <?>

I tried it anyway and its the same story :-(

dreamcatcher

3:20 pm on Aug 11, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Add the following after your opening tag:

error_reporting(E_ALL);

does that throw any errors?

dc

dtmuk

3:28 pm on Aug 11, 2006 (gmt 0)

10+ Year Member



Nope, the new test.php now reads

<?php
error_reporting(E_ALL);
$data = file_get_contents("URL HERE");
echo $data;
?>

and it's returns a completely blank page :S

Birdman

3:58 pm on Aug 11, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ok, try running this one:

<?php
error_reporting(E_ALL);
$file = 'URL HERE';

if ( file_exists( $file ) ) {
print 'FILE EXISTS! <br>';
$data = file_get_contents();
print $data;
} else {
print 'FILE DOES NOT EXIST!';
}

?>

dtmuk

5:02 pm on Aug 11, 2006 (gmt 0)

10+ Year Member



Ran that and it comes back with FILE DOES NOT EXIST!

Although the file_exists function only works for local files

barns101

5:15 pm on Aug 11, 2006 (gmt 0)

10+ Year Member



Is it possible that you have a firewall blocking web-based requests for external files (like my host does), but allowing ones from the command line? (I'm no server expert so don't laugh if I'm way off the mark! ;) )

Birdman

5:18 pm on Aug 11, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ah, you're right! It only works for URLs since PHP5. Oops...

Have you tried fopen()?

<?php
error_reporting(E_ALL);
$url = "URL HERE";
$handle = fopen($url, "r");
$contents = fread($handle, filesize($url));
fclose($handle);
print $contents;
?>

dtmuk

6:36 pm on Aug 11, 2006 (gmt 0)

10+ Year Member



Nope same Birdman, thanks for your help so far anyway though

Is it possible that you have a firewall blocking web-based requests for external files (like my host does), but allowing ones from the command line? (I'm no server expert so don't laugh if I'm way off the mark! ;) )

barns101, this could be the issue, i'm going to speak to a unix expert tonight and see what he makes of the problem and i'll report back here later ;-)