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
toasty2
Forum Contributor
Posts: 361 Joined: Wed Aug 03, 2005 10:28 am
Location: Arkansas, USA
Post
by toasty2 » Sun Sep 17, 2006 3:53 pm
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.
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Sun Sep 17, 2006 3:54 pm
$filename comes from..?
toasty2
Forum Contributor
Posts: 361 Joined: Wed Aug 03, 2005 10:28 am
Location: Arkansas, USA
Post
by toasty2 » Sun Sep 17, 2006 3:57 pm
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 » Sun Sep 17, 2006 4:03 pm
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:
volka
DevNet Evangelist
Posts: 8391 Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger
Post
by volka » Sun Sep 17, 2006 4:34 pm
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 » Sun Sep 17, 2006 4:48 pm
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
I have no problem using readfile instead of file_get_contents, but I am curious as to why it displays the output twice.
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Sun Sep 17, 2006 5:03 pm
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 » Sun Sep 17, 2006 5:11 pm
Output from volka's script with L.txt's contents changed to 2:
Code: Select all
file_get_contents -|2|-
2readfile: -|1|-
WEIRD!
volka
DevNet Evangelist
Posts: 8391 Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger
Post
by volka » Sun Sep 17, 2006 5:19 pm
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 » Sun Sep 17, 2006 5:22 pm
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?
volka
DevNet Evangelist
Posts: 8391 Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger
Post
by volka » Sun Sep 17, 2006 5:28 pm
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....
toasty2
Forum Contributor
Posts: 361 Joined: Wed Aug 03, 2005 10:28 am
Location: Arkansas, USA
Post
by toasty2 » Sun Sep 17, 2006 5:32 pm
Nevermind, it works fine
Thanks volka and feyd.