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 »

Ok Thank You I'm adding it now
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Post by akimm »

It echoes nothing, which confuses me, is the variable information not being passed from page to page properly maybe?
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Post by akimm »

I was trying a code which would initialize variables with $_POST['yes'] already in them, so we called foreach as that variable, maybe that is somehow throwing the code, but i'm not sure.
shneoh
Forum Commoner
Posts: 38
Joined: Mon Sep 25, 2006 2:23 am
Location: Malaysia

Post by shneoh »

It should display True or False if you have checked on the "no" option...

Can I have the full code?
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Post by akimm »

It doesn't display true or false, as I have the code, which I believe is as you've instructed me.

ad1.php

Code: Select all

<?php
//  First check if any form being posted to this page.  //
 if(!isset($_POST)){
     $fp = fopen('guestbook.txt', 'a');

     //  Start to do something if the file open sucessful. //
     if($fp){
         foreach($_POST['yes'] as $filename){  //<-- Loop through all approved files.
            fwrite($fp, $filename);
        }

      fclose($fp);    //<-- Since the file write is completed, close file.

     //  Remove files which is not approved.  //
      foreach($_POST['no'] as $filename) {
            $check_del = unlink($filename);
            echo $check_del;
       }
       }
}
?>
approve.php

Code: Select all

<?php
        foreach (glob("entrys/*.txt") as $files) {
?>
<table>
  <form method="POST" action="ad1.php">
    <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>
<?php
        }
?>
shneoh
Forum Commoner
Posts: 38
Joined: Mon Sep 25, 2006 2:23 am
Location: Malaysia

Post by shneoh »

I think mostly you should have getting some error... The code is not right. I have make a mistake. Sorry. Please let me know if any output.

Change to this:

Code: Select all

<?php
//  First check if any form being posted to this page.  //
 if(!isset($_POST)){
     $fp = fopen('guestbook.txt', 'a');

     //  Start to do something if the file open sucessful. //
     if($fp){
         foreach($_POST['yes'] as $filename)  //<-- Loop through all approved files.
            fwrite($fp, $filename);
        }

      fclose($fp);    //<-- Since the file write is completed, close file.

     //  Remove files which is not approved.  //
      foreach($_POST['no'] as $filename) {
            $check_del = unlink($filename);
            echo $check_del;
       }
}
?>
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Post by akimm »

as it is after your last post, it does not output anything.
shneoh
Forum Commoner
Posts: 38
Joined: Mon Sep 25, 2006 2:23 am
Location: Malaysia

Post by shneoh »

No wonder the script can't work... I have make a BIG mistake! 8O

While checking $_POST, I have misplaced a ! in front... :(

This should work now. I have tested it and tuned a bit. Just copy the whole thing and it should work!
approve.php

Code: Select all

<table>
  <form method="POST" action="ad1.php">
<?php
        foreach (glob("txt/*.txt") as $files) {
?>
    <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>
<?php
        }
?> 
     <input type="submit" value="submit for addition to guestbook" name="guestbook">
    </form>
</table>
ad1.php

Code: Select all

<?php
//  First check if any form being posted to this page.  //
 if(isset($_POST)){
     $fp = fopen('openfile.txt', 'a');

     //  Start to do something if the file open sucessful. //
     if($fp){
         foreach($_POST['yes'] as $filename)  //<-- Loop through all approved files.
            fwrite($fp, $filename);
        }

      fclose($fp);    //<-- Since the file write is completed, close file.

     //  Remove files which is not approved.  //
      foreach($_POST['no'] as $filename) {
            $check_del = unlink($filename);
            echo $check_del;
       }
}
?>
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Post by akimm »

well, its an improvement, but yes won't write to the file, it brings this error:

Warning: Invalid argument supplied for foreach() in /nfs/cust/8/25/05/650528/web/guestbook1/ad1.php on line 15


whereas no brings up this error:



Warning: Invalid argument supplied for foreach() in /nfs/cust/8/25/05/650528/web/guestbook1/ad1.php on line 8
1

then when i refresh the page:


Warning: Invalid argument supplied for foreach() in /nfs/cust/8/25/05/650528/web/guestbook1/ad1.php on line 8

Warning: unlink(entrys/sean8.txt): No such file or directory in /nfs/cust/8/25/05/650528/web/guestbook1/ad1.php on line 16

it then tells me that it did unlink the post.

I am going to try to mess with this, hopefully by the time you return i'll have figured it out. Thank you very much for your efforts.
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 my latest attempt. It works as far as to write files names, and delete files.

Code: Select all

<table>
  <form method="POST" action="<?php $_SERVER['PHP_SELF']; ?>">
<?php
        foreach (glob("entrys/*.txt") as $files) {
?>
    <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>
<?php
        }
?>
     <input type="submit" value="submit for addition to guestbook" name="guestbook">
    </form>
</table>
<?php
// establish all variables needed
$yes = $_POST['yes'];
$no = $_POST['no'];
$fp = fopen('guestbook.txt', 'a');
$check_del = unlink($files);
$approved = include($files);
// set an array for POST values
$check = array(
$_POST['name'] => "yes",
$_POST['name'] => "no",
$_POST['name'] => ""
);
//if that array is working, while its an instance of yes, fwrite the contents, of $files to the guestbook.txt
 if(is_array($check)) {
	while($yes) {
if ($fp) {
fwrite($fp, $approved);
$check_del;
// break the array of course
break;
	}
fclose($fp);
 	}
}
// ditto, only for no.
if(is_array($check)) {
	while($no) {
$check_del;
// break the array
break;
fclose($fp);
 	}
  }
?>
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Actually this is my last try until after BIO so..

Post by akimm »

If anyone can tell me where I'm erroring, I'll love you for an hour or so.. My love is strong like that.

8O :D

ad1.php

Code: Select all

<?php
// establish all variables needed
foreach (glob("entrys/*.txt") as $files) {
	}
$yes = $_POST['yes'];
$no = $_POST['no'];
$fp = fopen('guestbook.txt', 'a');
$check_del = unlink($files);

$approved = include($files);
// set an array for POST values
$check = array(
$_POST['name'] => "yes",
$_POST['name'] => "no",
$_POST['name'] => ""
);
//if that array is working, while its an instance of yes, fwrite the contents, of $files to the guestbook.txt
 if(is_array($check)) {
	foreach($yes as $value) {
if ($value) {
fwrite($fp, $approved);
$check_del;
// break the array of course
break;
	}
fclose($fp);
 	}
}
// ditto, only for no.
if(is_array($check)) {
  if(!$value) {
$check_del;
// break the array
break;
}
fclose($fp);
				}

?>

and the form

approve1.php

Code: Select all

<table>
  <form method="POST" action="ad1.php">
<?php
        foreach (glob("entrys/*.txt") as $files) {
?>
    <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>
<?php
        }
?>
     <input type="submit" value="submit for addition to guestbook" name="guestbook">
    </form>
</table>


Thanks so much for even considering looking, I do appreciate those who know more than me to help in anyway possible.

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

Post by akimm »

I think a problem with this code is my usage of foreach, for my next attempts i'm going to attempt to stop its usage, I think I use it incorrectly.
Last edited by akimm on Wed Sep 27, 2006 6:26 pm, edited 1 time in total.
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 the other code. Its still in works

Code: Select all

<table>
  <form method="POST" action="<?php $_SERVER['PHP_SELF'];?>">
<?php
        foreach (glob("entrys/*.txt") as $files) {
?>
    <tr><td><?php include($files);?></td></tr>
    <tr><td><?php echo printit(); ?></td></tr>
    <tr><td><h2>yes</h2></td><td><input type="radio" name="yes[0]" value="<?php echo $files; ?>" id="yes"></td></tr>
     <tr><td><h2>no</h2></td><td><input type="radio" name="no[1]" value="<?php echo $files; ?>" id="no"></td></tr>
<?php
        }
?>
     <input type="submit" value="submit for addition to guestbook" name="guestbook">
    </form>
</table>
<?php
function printit(){
foreach (glob("entrys/*.txt") as $files) {
return print_r($files);
	}
  }
function delete_it () {
       foreach (glob("entrys/*.txt") as $files) {
		   if($no) {
unlink($files);
fclose($fp);
	}
  }
return delete_it;
  }
?>

<?php
## variables ################################
$yes = $_POST['yes'];                       #
$no = $_POST['no'];                         #
$fp = fopen('guestbook.txt','a');           #
$confirmation = "It worked stupid";         #
$delete_it = delete_it();                   #
$print_it = printit();                      #
$write_format = $print_it . "<br>" . $files;#
#############################################
if($yes) {
fwrite($fp, $files);
$delete_it;
}  else {
	$delete_it;
	}
fclose($fp);
?>
Last edited by akimm on Wed Sep 27, 2006 6:25 pm, edited 1 time in total.
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Post by akimm »

This is yet another try, once again, unsuccessful, I am really struggling with this crap, I know its not a hard task, but i'm quite inexperienced. I have another set of code I'll post soon, which may be better off than this.

Code: Select all

<?php
foreach (glob("entrys/*.txt") as $files) {
// establish all variables needed
$yes = $_POST['yes'];
$no = $_POST['no'];
$fp = fopen('guestbook.txt', 'a');
$check_del = unlink($files);
$approved = include($files);
// set an array for POST values
$check = array(
$yes => "yes",
$no => "no"
);

// $approved is the info that is included, the guests actual entry
	foreach($approved as $key => $content) {
//if $check is an array then do foreach
 if(is_array($check)) {
//$yespost is equivalent to $_POST['yes']
	foreach($yes as $id => $yespost) {
if ($yespost) {
fwrite($fp, $content);
$check_del;
	}
fclose($fp);
 	}
}
// ditto, only for no. and if !$yespost is $_POST['no'] then delete file
if(is_array($check) && !yespost) {
$check_del;
  			}

fclose($fp);
		}
	}
?>
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

might want to stop with the shotgun-posting... lest the mods be monkey slapping you.
Post Reply