file_Get_contents() not working.

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
toasty2
Forum Contributor
Posts: 361
Joined: Wed Aug 03, 2005 10:28 am
Location: Arkansas, USA

file_Get_contents() not working.

Post by toasty2 »

file_get_contents() doesnt work for me.

Code: Select all

<?php $filename="l.txt"; $file = file_get_contents($filename); echo $file; ?>
Is returning a blank page. I've never used file_get_contents before.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

$filename comes from..?
toasty2
Forum Contributor
Posts: 361
Joined: Wed Aug 03, 2005 10:28 am
Location: Arkansas, USA

Post by toasty2 »

I added it. And I did have filename specified in my test script, that's just sort of a snippet from it.
toasty2
Forum Contributor
Posts: 361
Joined: Wed Aug 03, 2005 10:28 am
Location: Arkansas, USA

Post by toasty2 »

Ok, now it works since I changed some stuff. But, it is displaying the contents twice. The file contains "1" but it is showing me "11"...?
Here's where I tried both readfile and file_get_contents:

Code: Select all

echo "file_get_contents:" . file_get_contents("l.txt");
echo "<Br>readfile:" . readfile("l.txt");
Output:

Code: Select all

file_get_contents:11
readfile:1
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

When is readfile() executed? What does it return? (rhethorical questions)

try

Code: Select all

echo "file_get_contents -|" . file_get_contents("l.txt") . "|-<br />\n";
echo "readfile: -|" . readfile("l.txt") . "|-<br />\n"-;
toasty2
Forum Contributor
Posts: 361
Joined: Wed Aug 03, 2005 10:28 am
Location: Arkansas, USA

Post by toasty2 »

Code: Select all

file_get_contents -|1|-
1readfile: -|1|-
Thats the output. And notice that 1 before readfile?

By the way, I removed the "-" before your last semicolon, that provided an error :P

I have no problem using readfile instead of file_get_contents, but I am curious as to why it displays the output twice.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Change the file's contents to "2" .. maybe that will illuminate the results better.
toasty2
Forum Contributor
Posts: 361
Joined: Wed Aug 03, 2005 10:28 am
Location: Arkansas, USA

Post by toasty2 »

Output from volka's script with L.txt's contents changed to 2:

Code: Select all

file_get_contents -|2|-
2readfile: -|1|-
WEIRD! 8O
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

No, not weird. Just what readfile does: It outputs the file's content immediatly.
file_get_contents returns the content as string, readfile outputs it and returns how many bytes it read (see manual link).
toasty2
Forum Contributor
Posts: 361
Joined: Wed Aug 03, 2005 10:28 am
Location: Arkansas, USA

Post by toasty2 »

OH, dangit. Well, from my understanding, doesn't file_get_contents put the file into an array? What If I want it in a string?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

doesn't file_get_contents put the file into an array?
The output of the test script would be file_get_contents -|Array|-
file_get_contents returns....
http://de2.php.net/file_get_contents wrote:Description
string file_get_contents ( string filename [, bool use_include_path [, resource context [, int offset [, int maxlen]]]] )
toasty2
Forum Contributor
Posts: 361
Joined: Wed Aug 03, 2005 10:28 am
Location: Arkansas, USA

Post by toasty2 »

Nevermind, it works fine :P

Thanks volka and feyd.
Post Reply