file_get_contents() issue

Hi,

I am writing a simple php code on my localhost. I want to see the content of a text file which is in the same directory as the page. I have tried many codes, but the only outputs i see are warnings or just couple of question marks, like:

when i use this path “./sameFolder/file.txt”;
I get, ??? in firefox browser
when I use this path “file.txt” I get Warning: file_get_contents(file.txt): failed to open stream: Permission denied

apparently the first one is the right format, but can’t figure out why i don’t see the content, and instead see question marks


<?php
   
   //Get the content of file
   $myFile = "./sameFolder/file.txt";
   echo file_get_contents("$myFile");
  
?>

any ideas?

Thanks

if file.txt and the file containing your posted php code are in the same folder then

[COLOR=#0000bb]

 
[COLOR=#0000bb]$myFile [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#dd0000]"./sameFolder/file.txt"[/COLOR][COLOR=#007700];[/COLOR]

[/COLOR]

should be

[COLOR=#0000bb]

 
[COLOR=#0000bb]$myFile [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#dd0000]"file.txt"[/COLOR][COLOR=#007700];[/COLOR]

or

[COLOR=#0000bb]

 
[COLOR=#0000bb]$myFile [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#dd0000]"./file.txt"[/COLOR][COLOR=#007700];[/COLOR]

your code is looking for file.txt in a subfolder from the one your php file is in.
[/COLOR][/COLOR]

i tried both, it gives me the same warning: permission denied

ok, then it’s not a php issue.

try setting the permissions to 777 and then rerun your code.

Thanks

this works fine on my local server when index.php and test.txt are in the same folder.

index.php

 
<?php

$filePath = 'test.txt';
$contents = file_get_contents($filePath);
echo $contents;
 
?>

test.txt

 
this is line 1
this is line 2
this is line 3

I think you have unicode text in the text files. Set correct character set before you output the content.

It was the permission. I thought i changed it to 777, which obviously didn’t. and that’s why i was confused and couldn’t figure out what was the problem.

Thanks Kalon