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
It doesn't display true or false, as I have the code, which I believe is as you've instructed me.
ad1.php
approve.php
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;
}
}
}
?>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
}
?>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:
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;
}
}
?>No wonder the script can't work... I have make a BIG mistake!
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
ad1.php
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>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;
}
}
?>- akimm
- Forum Contributor
- Posts: 460
- Joined: Thu Apr 27, 2006 10:50 am
- Location: Ypsilanti Michigan, formally Clipsburgh
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.
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.
- akimm
- Forum Contributor
- Posts: 460
- Joined: Thu Apr 27, 2006 10:50 am
- Location: Ypsilanti Michigan, formally Clipsburgh
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);
}
}
?>- 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..
If anyone can tell me where I'm erroring, I'll love you for an hour or so.. My love is strong like that.
ad1.php
and the form
approve1.php
Thanks so much for even considering looking, I do appreciate those who know more than me to help in anyway possible.
Goodbye all.
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.
- akimm
- Forum Contributor
- Posts: 460
- Joined: Thu Apr 27, 2006 10:50 am
- Location: Ypsilanti Michigan, formally Clipsburgh
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.
- akimm
- Forum Contributor
- Posts: 460
- Joined: Thu Apr 27, 2006 10:50 am
- Location: Ypsilanti Michigan, formally Clipsburgh
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.
- akimm
- Forum Contributor
- Posts: 460
- Joined: Thu Apr 27, 2006 10:50 am
- Location: Ypsilanti Michigan, formally Clipsburgh
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);
}
}
?>