RE: [PHP] create textfile if not existing?
| From: | Non-HP-Boise,ex1) | Date: | Mon, 29 Apr 2002 21:36:43 +0000 |
| Subject: | RE: [PHP] create textfile if not existing? | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-95279@lists.php.net to get a copy of this message | ||
If I understand you properly this code will do what you want
<?php
// data to write to file
$contents = 'your data goes here';
// filename base
$filename = 'name';
$startnum = '1';
// get new filename until the filename does not exist
while (is_file($filename)) {
$filename .= $startnum;
}
// open the file, write contents and close the file pointer
$fp = fopen($filename, 'w');
fputs ($fp, $contents);
fclose ($fp);
?>
This code will write your contents to a filename with the base of $filename,
if the file already exists it will loop until it finds a free file name and
then use that to write your contents to.
You could improve this code so that it doesn't have to loop through used
numbers every time but I wrote it as an example for you, use it if you wish.
Jason
-----Original Message-----
From: Hawk [mailto:hw2k@barrysworld.com]
Sent: Monday, April 29, 2002 2:50 PM
To: php-general@lists.php.net
Subject: [PHP] create textfile if not existing?
lets say I have 30 lines of text and I want to store it in a folder called
"txt" with the filename $name.txt and, if $name.txt exists, create a
$name1.txt or similiar?
Håkan
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php