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
isn't the radio buttons yes and no $_POST?
Thats what I was referring to with $_POST, I just tell the page after the stuff is printed if yes is clicked then write the file, if its no unlink it
I'm just real confused, I thought when method is POST and name is say foo $_POST['foo'] would be presumably a call to the choice of foo.
Thanks
--Sean
Thats what I was referring to with $_POST, I just tell the page after the stuff is printed if yes is clicked then write the file, if its no unlink it
I'm just real confused, I thought when method is POST and name is say foo $_POST['foo'] would be presumably a call to the choice of foo.
Thanks
--Sean
well $_POST['name'] would only be set if you had a form post to it with an element named "name"... also, it's good practice to use the <label> tag for radio elements... so something like this:
Code: Select all
<input type="radio" name="name" value="yes" id="yes"><label for="yes">Yes</label><br>
<input type="radio" name="name" value="no" id="no"><label for="no">No</label>- akimm
- Forum Contributor
- Posts: 460
- Joined: Thu Apr 27, 2006 10:50 am
- Location: Ypsilanti Michigan, formally Clipsburgh
So this isn't a case where I use post?
Code: Select all
<?php
foreach (glob("entrys/*.txt") as $files) {
?>
<table>
<form method="POST" action="ad.php">
<tr><td><?php include($files);?></td></tr>
<tr><td><h2>yes</h2></td><br>
<td><input type="radio" name="yes"></td></tr>
<tr><td><h2>no</h2></td><br>
<td><input type="radio" name="no"></td></tr>- akimm
- Forum Contributor
- Posts: 460
- Joined: Thu Apr 27, 2006 10:50 am
- Location: Ypsilanti Michigan, formally Clipsburgh
here is as you've suggested.
Code: Select all
<tr><td><h2>yes</h2></td><br>
<td><input type="radio" name="name" value="yes" id="yes"><label for="yes">Yes</label></td></tr>
<tr><td><h2>no</h2></td><br>
<td><input type="radio" name="name" value="no" id="no"><label for="no">No</label><</td></tr>- akimm
- Forum Contributor
- Posts: 460
- Joined: Thu Apr 27, 2006 10:50 am
- Location: Ypsilanti Michigan, formally Clipsburgh
With the print_r, it prints the file as:
entrys/sean7.txt; this won't work, because, I need the entry to be included so I can read the file, knowing whether it is worth putting into my guestbook. Basically as an anti spam effort.
entrys/sean7.txt; this won't work, because, I need the entry to be included so I can read the file, knowing whether it is worth putting into my guestbook. Basically as an anti spam effort.
Code: Select all
<?php
$fp = fopen('guestbook.txt', 'w');
if($_POST['name'] == "yes" && $fp) {
fwrite($fp, $files);
fclose($fp);
unlink($_POST['name']);
}
if($_POST['name'] == "no") {
unlink($_POST['name']);
}
?>
<?php
foreach (glob("entrys/*.txt") as $files) {
?>
<table>
<form method="POST" action="<?php $_SERVER['PHP_SELF'];?>">
<tr><td><?php print_r($files);?></td></tr>
<tr><td><h2>yes</h2></td><br>
<td><input type="radio" name="name" value="yes" id="yes"><label for="yes">Yes</label></td></tr>
<tr><td><h2>no</h2></td><br>
<td><input type="radio" name="name" value="no" id="no"><label for="no">No</label><</td></tr>
<?php
echo "</table>";
}
?>
<input type="submit" value="submit for addition to guestbook" name="guestbook">
</form>- akimm
- Forum Contributor
- Posts: 460
- Joined: Thu Apr 27, 2006 10:50 am
- Location: Ypsilanti Michigan, formally Clipsburgh
Ok I've tried print_r, even implemented it in such a way that would work, or so I thought. I also now have a confusion, the way I have it set up, I'm afraid it will delete all $_POST['name'] variables, which wouldn't be a desired result, since this is an approval system, not a filecide(word? I doubt it, but now it is) system.
Code: Select all
<?php
$filename = print_r($files);
$fp = fopen('guestbook.txt', 'w');
if($_POST['name'] == "yes" && $fp) {
fwrite($fp, $files);
fclose($fp);
unlink($filename);
}
if($_POST['name'] == "no") {
unlink($filename);
}
?>
<?php
foreach (glob("entrys/*.txt") as $files) {
?>
<table>
<form method="POST" action="<?php $_SERVER['PHP_SELF'];?>">
<tr><td><?php echo $filename;?></td>
<tr><td><?php include($files);?></td></tr>
<tr><td><h2>yes</h2></td><br>
<td><input type="radio" name="name" value="yes" id="yes"><label for="yes">Yes</label></td></tr>
<tr><td><h2>no</h2></td><br>
<td><input type="radio" name="name" value="no" id="no"><label for="no">No</label></td></tr>
<?php
echo "</table>";
}
?>
<input type="submit" value="submit for addition to guestbook" name="guestbook">
</form>First, I wish to make this clear: Ninja is asking you to print_r($_POST) not others.
Second, you have confused yourself. I suggest you should have dig deeper on the language basics before trying to manipulate the file system. Anyway, this is a good try. Just a friendly suggestion.
For a good programming habit, always comment your codes so others will understand better on what you suppose willing to do with these lines.
I have changed your codes and hope this would help:
Parameters:
$files = $filename = contains full path with file name.
$_POST[yes] = file array which picked as Yes.
$_POST[no] = file array which picked as No.
Second, you have confused yourself. I suggest you should have dig deeper on the language basics before trying to manipulate the file system. Anyway, this is a good try. Just a friendly suggestion.
I have changed your codes and hope this would help:
Parameters:
$files = $filename = contains full path with file name.
$_POST[yes] = file array which picked as Yes.
$_POST[no] = file array which picked as No.
Code: Select all
<?php
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 "</PRE>";
$fp = fopen('guestbook.txt', 'w'); //<-- open file to write
if($fp){
foreach($_POST['yes'] as $filename){ //<-- Loop through all approved files.
fwrite($fp, $filename);
unlink($filename); //<-- I dont know why you want to remove it, anyway, I just remain here...
fclose($fp); //<-- close file.
}
foreach($_POST['no'] as $filename) { // Remove files which is not approved.
unlink($filename);
}
/* For those files which is not being choose, will do nothing. */
?>
<?php
foreach (glob("entrys/*.txt") as $files) {
?>
<table>
<form method="POST" action="<?php $_SERVER['PHP_SELF'];?>">
<tr><td><?php include($files);?></td></tr>
<tr><td><h2>yes</h2></td><td><input type="radio" name="yes[]" value="$files" id="yes"></td></tr>
<tr><td><h2>no</h2></td><td><input type="radio" name="no[]" value="$files" id="no"></td></tr>
</table>
<input type="submit" value="submit for addition to guestbook" name="guestbook">
</form>- akimm
- Forum Contributor
- Posts: 460
- Joined: Thu Apr 27, 2006 10:50 am
- Location: Ypsilanti Michigan, formally Clipsburgh
Hello
I honestly and completely thank you for your effort, but as is, this code still doesn't accomplish the goal. It prints this output after I go to the page with the new code
After I select yes it prints this, but the file still remains.
Your code submitted had an unexected $end I had to add an extra curly bracer then all of the above was printed, this is your code as I have it, again, thank you for the help, I know we're close to solving this problem, but not there yet!
Thanks
--Sean
Code: Select all
Print all from POSTArray
(
)
Print all from yesPrint all from no
Warning: Invalid argument supplied for foreach() in /nfs/cust/8/25/05/650528/web/guestbook1/approve.php on line 12
Warning: Invalid argument supplied for foreach() in /nfs/cust/8/25/05/650528/web/guestbook1/approve.php on line 19Code: Select all
Print all from POSTArray
(
[yes] => Array
(
[0] => $files
)
[guestbook] => submit for addition to guestbook
)
Print all from yesArray
(
[0] => $files
)
Print all from no
Warning: unlink($files): No such file or directory in /nfs/cust/8/25/05/650528/web/guestbook1/approve.php on line 14
Warning: Invalid argument supplied for foreach() in /nfs/cust/8/25/05/650528/web/guestbook1/approve.php on line 19Thanks
--Sean
Ops, some mistake.... Change these parts and I think it should be alright.
Code: Select all
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 "</PRE>";
}Code: Select all
<table>
<form method="POST" action="<?php $_SERVER['PHP_SELF'];?>">
<tr><td><?php include($files);?></td></tr>
<tr><td><h2>yes</h2></td><td><input type="radio" name="yes[]" value="<?php echo $files; ?>" id="yes"></td></tr>
<tr><td><h2>no</h2></td><td><input type="radio" name="no[]" value="<?php echo $files; ?>" id="no"></td></tr>
<input type="submit" value="submit for addition to guestbook" name="guestbook">
</form></table>