For some reason this code prints the entry twice, when entered. I've tried a few different solutions and am at a loss as to how to fix this. Thanks for any help you can give.
-Sean.
Code: Select all
<?php
// This is the path (relative or full) to your directory of files
$filebin = "entrys";
// Initialize array that will eventually store the files
$files = array();
// This is for compatibility with PHP < 4.3.0
// If your version of php doesn't have file_get_contents
// PHP will use this function instead
if(!function_exists('file_get_contents')){
function file_get_contents($filename){
if(file_exists($filename)){
$filesize = filesize($filename);
$fh = fopen($filename, 'r');
$contents = fread($fh, $filesize);
fclose($fh);
return $contents;
}
return false;
}
}
// Check that the directory supplied in $filebin is, in fact a valid directory
if(is_dir($filebin)){
// Loop through the directory to find .txt files
foreach(glob($filebin . '/*.txt') as $file){
// Store all files found in the $files array with the filenames as keys and the content as values
$files[$file] = file_get_contents($file);
}
}
$write = $files[$file];
// This checks to see if the form has been submitted
if(isset($_POST['process']) && $_POST['process'] == 'yes'){
// If so, loop through the $files array
foreach($files as $name => $file){
// Assign all files that were checked "approved" to an array called $keep_files
$keep_files = isset($_POST['keep_files']) ? $_POST['keep_files'] : array();
$fp = fopen('guestbook.txt','a');
# if($name) {
## this is where I think the error might be. I am not fully sure. As I said it prints it twice.
if(in_array($name, $keep_files)) {
fwrite($fp, $file);
break;
fclose($fp);
unlink($name);
unset($files[$name]);
}
// If this particular file is not in the keep_files array, delete it both from the directory and from out $files array
if(!in_array($name, $keep_files)){
unlink($name);
unset($files[$name]);
}
}
}
// Now display the form
?>