Using unlink() with an if/else statement to delete files
Moderator: General Moderators
- akimm
- Forum Contributor
- Posts: 460
- Joined: Thu Apr 27, 2006 10:50 am
- Location: Ypsilanti Michigan, formally Clipsburgh
well ninja, I am trying my very best to figure this problem out, I post reasonable posts, its not as if I'm bumping, I am posting different solutions, praying someone wiser than I will find my flaws, show me how to fix them, then I can continue with the next tier of this program, until then, I will wait til my topic is froze, this is when i'll stop posting once again reasonable responses.
I will heed your advice to a point, but honestly i have no idea what else to do, if i don't post the code, I will lose the post among the newer ones, and still be at a loss as to what to do to solve my problem.
I will heed your advice to a point, but honestly i have no idea what else to do, if i don't post the code, I will lose the post among the newer ones, and still be at a loss as to what to do to solve my problem.
- akimm
- Forum Contributor
- Posts: 460
- Joined: Thu Apr 27, 2006 10:50 am
- Location: Ypsilanti Michigan, formally Clipsburgh
Ninja if you view this, i finally understood after shneoh helped explain, what you were trying to say, I hope with this script I've added what you told me to add, and now instead of how i was doing it before,I'm doing a select menu, glob through all the files, then take the value of select as the value to be written or deleted. I just need to figure out how to tell it to write the entire contents of the file and write it to my guestbook if $_POST['yes'];
I'm going to try something like file_get_contents as I was typng contents up there it made me think of this function. Maybe that will help me.
I'm going to try something like file_get_contents as I was typng contents up there it made me think of this function. Maybe that will help me.
Code: Select all
<?php
if(isset($_POST)){
echo "<PRE>";
echo "Print all from POST";
print_r($_POST);
echo "Print all from yes";
print_r($_POST["yes"]);
echo "Print all from no";
print_r($_POST["no"]);
echo "print select, file name";
print_r($_POST['select']);
echo "</PRE>";
echo "<br>" . "<br>";
echo $_POST['select'];
}
$check_del = unlink($_POST['select']);
$fp = fopen('guestbook.txt', 'a'); //<-- open file to write
if($_POST['yes']){
fwrite($fp, $_POST['select']);
$check_del;
} fclose($fp); //<-- close file
if($_POST['no']) {
check_del;
}
?>What I can say is: akimm, you have effort but goes a wrong path...
I have no time now. Will come back tomorrow but hopefully you can spare some time on the basic of programming. Especially on the "variables" and "array". Your codes shows you didn't understand the logical flows of a scripts/codes.
C Ya.
I have no time now. Will come back tomorrow but hopefully you can spare some time on the basic of programming. Especially on the "variables" and "array". Your codes shows you didn't understand the logical flows of a scripts/codes.
C Ya.
I'm feeling generous, so here... this works fine. I am running php 5 though, but this should be compatible for a ways back. PLEASE make sure you FULLY understand this script before using it. I only wrote it for you because you seem to be having SOO much trouble. BUT FYI... I'm pretty sure that shotgun posting is both bumping and against the rules just on its own, so please don't do it. I have commented the script fully, but please ask questions if you don't understand. I wrote it in hopes that it would help you learn, not just to get it done.
Code: Select all
<?php
// I set error reporting to E_ALL when in development... it makes it easier to debug
error_reporting(E_ALL);
// This is the path (relative or full) to your directory of files
$filebin = "./entries";
// 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);
}
}
// 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();
// 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
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>File Deleter for Akimm</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<form method="post" action="#">
<input type="hidden" name="process" value="yes">
<table border="1" cellspacing="0" cellpadding="3" width="500">
<?php foreach($files as $name => $content): ?>
<tr>
<td colspan="2"><strong><?php echo $name ?></strong><br><div style="overflow: auto; height: 200px; width: 490px;"><?php echo htmlentities($content); ?></div></td>
</tr>
<tr>
<td>Approve?</td>
<td><input type="checkbox" name="keep_files[]" value="<?php echo $name; ?>" id="yes"><label for="yes"> Yes </label></td>
</tr>
<?php endforeach; ?>
<tr>
<td colspan="2"><input type="submit" value="Submit"></td>
</tr>
</table>
</form>
</body>
</html>- akimm
- Forum Contributor
- Posts: 460
- Joined: Thu Apr 27, 2006 10:50 am
- Location: Ypsilanti Michigan, formally Clipsburgh
Well Ninja, I appreciate the help very much, right now I have a math course to go to, but once my math is done I'll be back to review the script. As you asked I'll make sure I fully understand what is happening and why it is, especially considering, this is the reason i program anyway. To learn.
Thanks
-Sean
Thanks
-Sean
- akimm
- Forum Contributor
- Posts: 460
- Joined: Thu Apr 27, 2006 10:50 am
- Location: Ypsilanti Michigan, formally Clipsburgh
I should rephrase.
No, because I wish to be able to deny those entrys which are say spam, like
"Hello, would you like to increase the size of your erection, go to http://www.<span style='color:red;text-decoration:blink' title='Alert a moderator!'>grilled spam</span>.com for a free sample of our miracle drug"
et cetera, I find that being able to deny spammers is fun, however, I could try to get that part done myself, you've done me a great favor.
My second question, as of now it unlinks all the files but the last one when there are say a group of 3 files, not sure why that is, but when I do approve every file by click yes, it doesn't write them to guestbook.txt. So What I am asking is, after all is processed on this page, I could either add a few more commands to be done, or go to a confirmation page, where it will write the approved entries to guestbook.txt.
No, because I wish to be able to deny those entrys which are say spam, like
"Hello, would you like to increase the size of your erection, go to http://www.<span style='color:red;text-decoration:blink' title='Alert a moderator!'>grilled spam</span>.com for a free sample of our miracle drug"
et cetera, I find that being able to deny spammers is fun, however, I could try to get that part done myself, you've done me a great favor.
My second question, as of now it unlinks all the files but the last one when there are say a group of 3 files, not sure why that is, but when I do approve every file by click yes, it doesn't write them to guestbook.txt. So What I am asking is, after all is processed on this page, I could either add a few more commands to be done, or go to a confirmation page, where it will write the approved entries to guestbook.txt.
there's no need to put the "no" in there because if you don't check "yes", no is assumed
Checkboxes are essentially a way to answer yes or no. Checked = yes, not checked = no. but if you really want to, go right ahead.
As for writing to the guestbook, try and figure it out... I will help you as much as I can, but try some things first.
As for writing to the guestbook, try and figure it out... I will help you as much as I can, but try some things first.
- akimm
- Forum Contributor
- Posts: 460
- Joined: Thu Apr 27, 2006 10:50 am
- Location: Ypsilanti Michigan, formally Clipsburgh
Ok just a few short questions.
htmlentities()
I will look it up if you don't answer, but could you explain why you used this?
$_POST['keep_files']
Will keep the array of to save files yes?
and when designing the final parts i wish to have, do you think I should just keep all the code on here? Or maybe make the form action go to one more bit of code, which will finsih the job?
Why did you put all PHP above document type declaration?
Probably a very good reason, I just never knew it.
Thanks if you get to these questions, otherwise I'll figure them out in due time.
htmlentities()
I will look it up if you don't answer, but could you explain why you used this?
$_POST['keep_files']
Will keep the array of to save files yes?
and when designing the final parts i wish to have, do you think I should just keep all the code on here? Or maybe make the form action go to one more bit of code, which will finsih the job?
Why did you put all PHP above document type declaration?
Probably a very good reason, I just never knew it.
Thanks if you get to these questions, otherwise I'll figure them out in due time.
- akimm
- Forum Contributor
- Posts: 460
- Joined: Thu Apr 27, 2006 10:50 am
- Location: Ypsilanti Michigan, formally Clipsburgh
Well ninja!
I finally got this thing working. After all your hard work, the few changes I wanted were relatively easy, just a bit of trial and error
thank you very much for the assistance, now I must let this topic die, I only hope you read my thanks, and thanks to shnoeh for giving all the help. I do really appreciate it, i'll be on within the next few days with my next enigma, but for now, this one works!
Thanks
-Sean!
I finally got this thing working. After all your hard work, the few changes I wanted were relatively easy, just a bit of trial and error
thank you very much for the assistance, now I must let this topic die, I only hope you read my thanks, and thanks to shnoeh for giving all the help. I do really appreciate it, i'll be on within the next few days with my next enigma, but for now, this one works!
Thanks
-Sean!
It prevents characters like <> so html doesn't render on the page if somebody puts html in thereakimm wrote:Ok just a few short questions.
htmlentities()
I will look it up if you don't answer, but could you explain why you used this?
It stores the names of the files you checked "yes" forakimm wrote: $_POST['keep_files']
Will keep the array of to save files yes?
That's a matter of preference. Do it whichever way makes you feel most comforableakimm wrote: and when designing the final parts i wish to have, do you think I should just keep all the code on here? Or maybe make the form action go to one more bit of code, which will finsih the job?
PHP is completely unaware of HTML. It makes sense to do all logic before the page is rendered. It just prevents a lot of headaches such as "headers already sent" and you will learn to start doing this as well as it is just good practiceakimm wrote: Why did you put all PHP above document type declaration?