FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free 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.
Related topics
php admin and mysql admin console
Create a txt file
PHP: Writing to txt file
.txt file to .php Lined to tables
Open, Write to and Close a txt file using php
does someone have a simple "save to txt file" scri
Creating or writing to a file in javascript.
Displaying .txt file on web page
Flat file-based, MySQL-based counter
I can't open directly .txt file on my desktop
Saving a .txt file to a ftp using vb6. i need help please
i cannot open a file with fopen()
VB 6.0 need help loading a .txt file into a text box
is it possible to get a html text box to load a .txt file?
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

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