you could use a database or a text file to keep track of the last file name. A text file would be simpler. try this:
PHP Code:
<?php
if (isset($_POST['msg'])){
//process message if submitted
if (file_exists("filecount.txt")){
//find out if there is a file count file
$filename = file_get_contents("filecount.txt");
//if so, use the stored number as the filename
} else {
//if not,
$filename = 1;
//set the filename to "1"
$fCount = fopen("filecount.txt", "w");
//then create the file count file
fwrite($fCount, "2");
//set its contents to "2", which will be the next filename
fclose($fCount);
//and close it
}
$input = $_POST['msg'];
if (!file_exists("$filename.txt")){
//if the filename is not already in use,
$fp = fopen("$filename.txt", "w");
fwrite($fp, $input).' ';
fclose($fp);
//write the submitted message to a file using the file name
$fInc = fopen("filecount.txt", "w");
//then, open the file count file
fwrite($fInc, (++$filename));
//add one to the value and rewrite it (now it's ready for the next submission)
fclose($fInc);
//and close it
} else {
echo "Couldn't save your message: File name already exists!";
//otherwise, send an error message and DON'T OVERWRITE THE FILE!
//some code could be developed here to choose a suitable filename, but I'm not sure how it would go
}
}
?>
The first else{} block (the one that writes the initial "filecount.txt" file) is only intended to be used the first time a message is submitted. It could be left out if you prefer to create that file manually and upload it to the same directory.
There's probably a way to read through the filenames and find the next one in line, but I'm not sure how to do it. But at that point, you wouldn't need the "filecount.txt" file in the first place.
Bookmarks