FRIHOSTFORUMSFAQTOSBLOGSDIRECTORY
You are invited to Log in or Register a Frihost Account!

Creating a .txt file

 


DanielXP
How do i create a .txt file using php?
mathiaus
Code:
<?php
$filename = "file.txt";
$handle = fopen($filename, 'w') or die("can't open file");
fclose($handle);
?>

This is to open a file but if it doesnt exist it creates that file.

Try these file tutorials for more info Smile
yjwong
Thats a slightly more complicated script. The same could be achieved with the touch() function:

Code:

<?php
touch("file.txt")
?>
BlackSkad
I want to add a little explanation to the two examples above. When the file doesn't exist, it is created in both cases, but there is a difference between the two scripts.
Let's start with the one of yjwong. His script "touches" a file, so that the "last edit"-date changes to the current date. This method is good for like a back-up system or something, but you can't put (or change) any content in the file, which is probably what you want to do...
Mathiaus' script - at the other hand - has this possibility, by adding another function (the fwrite). It's just a logical order: you open the file, write to it and then close it.
Code:
$filename = "file.txt";
$handle = fopen($filename, 'w') or die("can't open file");
fwrite ($handle, "your content");
fclose($handle);


So, if you just want to "created" but NOT write, use the method of yjwong. If you want create AND write to the file, you better use the modified version of Mathiaus' script.
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2007 Frihost, forums powered by phpBB.