file_get_contents wierd?

Hi,

I have been trying for a couple of hours to use file_get_contents on a local file on the server. This works fine with external websites and text files as a local file on the server:

Both of these work fine
file_get_contents($text_file);
file_get_contents($website_url);

When I try and get a PHP file its a different story, What gets returned is an empty string the same length as the file content length so:

file_get_contents($php_file);
when var_dump() return is (string) 99 “”

I’m wondering if it is a secuirty thing within apache.
Any ideas any1

Thank you

Do you know when you kick yourself for something you should of found straight away. :lol:

I viewed the source of the HTML and there it was!!

Thanks guys for looking into though I wouldn’t of found it for ages!

I think I know why I didn’t look at the HTML source. var_dump function output so looks like a pre tag!!

haha

True enough there is a difference between the rendered display and view-source i.e. the bold.

Yet, using my previous code example with this file

test.php

<?php
$var1 = 'a string';
$arr1 = array('str2', 
			'1' => 'str3', 
			'2' => 1, 
			'html' => '<b>Hey!</b>');
var_dump($arr1);
foreach ($arr1 as $piece)
{
	echo $piece . "<br />\\r\
";
}
?>

The view-source is

string(200) "<?php
$var1 = 'a string';
$arr1 = array('str2', 
			'1' => 'str3', 
			'2' => 1, 
			'html' => '<b>Hey!</b>');
var_dump($arr1);
foreach ($arr1 as $piece)
{
	echo $piece . "<br />\\r\
";
}
?>"
string(176) "array(4) {
  [0]=>
  string(4) "str2"
  [1]=>
  string(4) "str3"
  [2]=>
  int(1)
  ["html"]=>
  string(11) "<b>Hey!</b>"
}
str2<br />
str3<br />
1<br />
<b>Hey!</b><br />
"

EDIT: Hmmmm. (I should read more carefully - the caffiene is starting to kick in)
The PHP tags as HTML tags idea is a good possibilty too.
Definately don’t forget to look at view-source!

You weren’t viewing the contents of the PHP file as HTML were you? The PHP tag(s) will be interpreted as HTML tags which is why you wouldn’t see any text on the page.

Try doing view-source in your browser, or send along a plain text content type before echoing the file contents (header(‘Content-Type:text/plain;charset=utf-8’);).

I just tried

<?php
$file1 = 'test.php';
$file2 = 'http://localhost/test.php';
$content1 = file_get_contents($file1);
$content2 = file_get_contents($file2);
var_dump($content1);
var_dump($content2);
?>

on the same PHP file.

The results varied so I’m guessing it has something to do with security - i.e. a known local file vs. a suspected remote file - the local isn’t parsed but the remote is.

It might be caused by a safe_mode setting overriding something.

Check the permissions on the file.