PDA

View Full Version : [PHP] Can PHP write to a .txt file?



arks
July 10th, 2006, 12:28 AM
Hi, :huh:
I'm developing a Flash-based website for a "Clan".
Using PHP, is it possible to write to a .txt file, which is then read by the .swf?
And would anyone like to post a PHP script which does this??? (I'm not very familiar with PHP).

Thanks =-) :esmirk:

-arks

bwh2
July 10th, 2006, 12:45 AM
google is your friend: http://www.google.com/search?q=Can+PHP+write+to+a+.txt+file

bigmtnskier
July 10th, 2006, 01:00 AM
Hi there, welcome to kirupa! :)

Yeah, this definitally possible with PHP.

A user here named nokrev actually wrote a tutorial about writing files with php a couple days ago: http://www.kirupa.com/developer/php/filehandling.htm



//The file to write
$filename = 'file.txt';
//This is the content to write to the text file
$content = 'Hello world';

$fhandle = fopen($filename,'w');
fwrite($fhandle, $content);
fclose($fhandle);


I'm not the greatest with loading data with actionscript, so, someone else will have to answer that part of the question :P

-bigmtnskier

Edit: bwh beat me (that only answers part of the question, although, so does mine)

Jeff Wheeler
July 10th, 2006, 01:01 AM
Aye, the fwrite function is what you want. Or just read my tutorial. :D

ironikart
July 10th, 2006, 02:36 AM
I think we'd all be in a bit of trouble if it couldn't

λ
July 10th, 2006, 02:50 AM
Nokrev, in your tutorial, it says:


Please be aware, both of these require PHP5.

1. Rather than fopen and fread, you can simply use file_get_contents. The only required argument is the filename, and it'll return a string of the contents.

file_get_contents needs PHP >= 4.3.0, not PHP 5.

bigmtnskier
July 10th, 2006, 03:38 AM
That has already been mentioned ;)




One other small inaccuracy is that you stated that file_get_contents() requires PHP5.

Yes, I forgot to mention that. Somebody reminded me, and I almost did it, but then I forgot again. :P

Zendra
July 10th, 2006, 04:10 AM
Perhaps this is something most people don't know, but see the example and futher explanation below:

Example:


//Number 1
fopen("file.txt","r");
//Number 2
fopen("file.txt","r");

fclose(fopen("file.txt","r"));

Even though you open the file twice with the EXACT same syntax, when you call fclose, it closes neither resource, because resources have no relation no matter if they open the exact same thing with the exact same permissions. The same goes with ANYTHING in PHP that results in a resource being returned (which includes basically every database function), that should be stored to be closed properly later on.

Jeff Wheeler
July 10th, 2006, 11:51 AM
That has already been mentioned ;)

I think I should've stuck with what I had before. :P

I never use anything before PHP5, so I just went by what other people said… they told me it required PHP5, now people are saying the opposite. I didn't check until yesterday when Temp told me it didn't require PHP5.

I'm sure Kirupa can fix it.