Using unlink() with an if/else statement to delete files

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Post by akimm »

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
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

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>
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Post by akimm »

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>
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

yea it is... but your html is wrong... I posted what would be correct. However, this is not your only problem... your script has other issues, but let's tackle one thing at a time
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Post by akimm »

ok, well i'll make your prescribed fixes, thanks thus far.
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Post by akimm »

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>
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Post by akimm »

Now with my code it still won't unlink the files, is there maybe a different way of deleting those, since this, after pressing no simply goes to ad, then i go back to the page and the file remains.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

go to the last response on the first page of this thread... I posted your code with my comments. Read my comments... try and fix it, and come back with some new code, and I'll help you.
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Post by akimm »

Your comments confused me royally, mainly because, after we agreed that $_POST was indeed applicable, the only real fix would be print_r which prints arrays, although what do I want it to print? Use print_r in every instance where I used echo?
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Post by akimm »

I've put all the code on one page, and used PHP_SELF as the form action. as for print_r i'll play with it, til you are able to come back, or someone, hopefully i'll get lucky and fix it before anyone has a chance to help.
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Post by akimm »

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.

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>
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Post by akimm »

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>
shneoh
Forum Commoner
Posts: 38
Joined: Mon Sep 25, 2006 2:23 am
Location: Malaysia

Post by shneoh »

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. :wink: 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.

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>
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Hello

Post by akimm »

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

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 19
After I select yes it prints this, but the file still remains.

Code: 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 19
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
shneoh
Forum Commoner
Posts: 38
Joined: Mon Sep 25, 2006 2:23 am
Location: Malaysia

Post by shneoh »

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>
Post Reply