Page 1 of 1

Opening text files in php

Posted: Tue Feb 15, 2005 11:36 pm
by KyleReed
I tried the fopen function and it requires a user login other than anonymous, and only through a URL to write to the file? What's up with that? not to mention the documents I've read states that it looks for php code or something .. very weird.

Anyway, all I need to do is open up some data files in ASCII text format to retrieve data and print them onto the web page from a text file that I've printed data to from another php script.

I know in perl it's just this:

Code: Select all

# Opening / Reading the file #

open(FILE, "data.txt") or die("Error, could not open file, $!");
print <FILE>;
close(FILE);

# Opening / Creating / Overwriting A File #

open(FILE, ">data.txt") or die("Error, could not open file, $!");
print FILE "some text";
close(FILE);

There's gotta be something similar like this in php, something to open / read / and write to files on-the-fly without authorization from the user besides the user who wrote the script himself.

Anyone know what I can do here?

Thanks

Posted: Tue Feb 15, 2005 11:43 pm
by feyd
"login" is only required if php doesn't have rights to open/read/write to files in the requested path. A url is not needed for a local file. If you want to read the contents of the file: file_get_contents() creates a single string of the entire file. file() will create an array element of each line.

Code: Select all

$fp = fopen('filename','rt');
will open a file for reading in text mode.

Posted: Wed Feb 16, 2005 1:35 pm
by KyleReed
I just tried it out with the permissions thing, very different from perl, but it did work.

World rights must be enabled? I remember that it used to be just the owner would have the rights to open and write to a file, now I have to set the World's rights?

Is this the more logical way of doing it? I don't see how in perl that can right to file with where the World's rights were only on read, and here, it must be set to writing privilages, what has changed?

Posted: Wed Feb 16, 2005 1:38 pm
by feyd
it all hinges on what user php and perl are run under.

Posted: Wed Feb 16, 2005 1:43 pm
by KyleReed
So you're saying that the /cgi-bin/ in perl pretty much has writing writes to all files with Owner permissions? Because the files I've been trying to write to were not in perl and outside of the cgi-bin.

Posted: Wed Feb 16, 2005 1:45 pm
by timvw
my guess is that your hosts has suEXEC (or a similar wrapper) enabled... which means that perl runs in your user acount instead of the user that is running apache

Posted: Wed Feb 16, 2005 1:52 pm
by KyleReed
What I don't understand is that I had to give the World writing permissions to write to this file, what does this mean for anonymous users attempting to access the remote server? It just bothers me, that's all.

Posted: Wed Feb 16, 2005 1:55 pm
by timvw
anonymous users can't access your files if they are not in public_html directory :)


but users with a shell account (login viah ssh/telnet/whatever)... they can read your files...

Posted: Wed Feb 16, 2005 1:57 pm
by KyleReed
Well if you say so. The term "World" is just scary itself.

Thanks for the tips!