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

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

Post by akimm »

There are a few errors in this. My basic purpose to to allow users to submit their messages via another form, which then places seperate .txt files with their messages in the directory entrys/. Anyway, after that is done, I wanted to set up an approval system. Basically I want the glob to loop with foreach through entrys/ in serpate tables, with yes or no underneath them, if I click yes, I want the file to be written to guestbook.txt, which will then be able to be included as approved messages, otherwise i would like it to be unliked, but the program isn't really working.

Code: Select all

<table>
<tr>
<?php
$fp = fopen( 'guestbook.txt', 'w');
$files = glob("entrys/*.txt");##Display $filesforeach ($files as $key){    echo "<td>" . include($files) . "</td>;    echo

    echo  "<form method" . "POST" . "action=" . $_SERVER['PHP_SELF'] . ">";
    echo  "<tr>" . "<td>"  . "<h2>" . "Yes" . "</h2>" . "<br>" . "</td>";
    echo "<td>" . "<input type=" . "radio" . "name=" . "yes" . ">" . "<br>"  . "</td>" . "</tr>";
    echo "<tr>" . "<td>" . "<h2>" . "No" . "</h2>" . "<br>" . "</td>";
    echo "<td>" . "<input type=" . "radio" . "name=" . "no" . ">" . "<br>"  . "</td>" . "</tr>";
?>
<input type="submit" value="submit for addition to guestbook" name="guestbook">
</form>
</tr>
</table>
<?php
if($_POST['name'] = "no") {
unlink ($files);
} else {
fwrite($fp, $files);
fclose($fp);
}
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I'm not too sure what you're asking...
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Post by akimm »

My Code is not doing as it is supposed to, it should be unlinking the file when I select yes, and its calling an error, before i have a chance to ever choose yes or no.

this is the error that is produced


Warning: unlink(Array): No such file or directory in /nfs/cust/8/25/05/650528/web/guestbook1/approve.php on line 18
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

unlink() takes a string for the file name...you're passing it an array.
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Post by akimm »

Is there a function to delete a file, perhaps unset()?
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

unlink() is the correct function, you're just not using it correctly. You're passing it an array which is being generated from glob(). You need to pass it a string for a single file name. Click the link I posted in my last post.
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Post by akimm »

but I don't know specific names, I'm pulling them with glob, it pulls all that have .txt,
is there a way to print their names into a global so I can say specifiy?


EDIT, this just occured to me

Code: Select all

<?php
if($_POST['name'] = "no") {
fclose($fp);
unlink($_POST['name']);
} else {
fwrite($fp, $files);
fclose($fp);
}
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

You're looking to use a loop to iterate over all the elements in the results from glob(). I would caution you to filter the list to make sure files you don't want to be deleted don't get deleted by mistake due to some blind acceptance on the authority for the request to wipe files.
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Post by akimm »

Yes feyd, after they are printed, it puts all of them in a table with yes or no as radios each yes writes to a file, each no deletes the file.
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Post by akimm »

If you view this, this is the program I have so far.

http://www.akimm.com/guestbook1/approve ... +guestbook

output:

Code: Select all

Warning: main(entrys/sean7.txt): failed to open stream: No such file or directory in /nfs/cust/8/25/05/650528/web/guestbook1/approve.php on line 5

Warning: main(): Failed opening 'entrys/sean7.txt</td>' for inclusion (include_path='.:/usr/local/php/include') in /nfs/cust/8/25/05/650528/web/guestbook1/approve.php on line 5
approve.php

Code: Select all

<?php
$fp = fopen( 'guestbook.txt', 'w');
foreach(glob("entrys/*.txt") as $files) {
	echo "<table>" . "<tr>";
	echo "<td>" . include($files) . "</td>";
    echo  "<form method" . "POST" . "action=" . $_SERVER['PHP_SELF'] . ">";
	echo "</table>";
	}
?>
<table>
<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>
<input type="submit" value="submit for addition to guestbook" name="guestbook">
</form>
</tr>
</table>
<?php
if($_POST['name'] = "yes") {
fwrite($fp, $files);
fclose($fp);
	}
if($_POST['name'] = "no") {
unlink($_POST['name']);
}
?>
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

A single equal sign is an assignment operator, two equal signs is used for comparison.

Code: Select all

<?
if($_POST['name'] == "yes") { 
...
?>
The way you have it, both if statements are returning true. You need to have your form in the loop (or it's only going to print once)... You need to have your write/delete logic at the top of the page so your changes are reflected in the results after the form is submitted. Think about where $_POST['name'] is supposed to be coming from -- is it even being set?
User avatar
panic!
Forum Regular
Posts: 516
Joined: Mon Jul 31, 2006 7:59 am
Location: Brighton, UK

Post by panic! »

I was reading through disparing that no one had picked that up, well done Aaron.
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

ok

Post by akimm »

With the fix of the == it still won't include the contents of the foreach, I am trying to figure out why it won't print the contents of those files.
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Post by akimm »

I have reworked this slightly, now it prints the files. It even prints the radios how i want it, but when i click yes or no it just submit it, and then when i go back to the page uri it the file still is there.

Because the way I have it written, I want the file unliked no matter what, if i approve it, then it will be written to guestbook.txt, but still it is unliked.

Also, the first entry printed, is one with all my questions, but they're unanswered, what i'm saying is glob finds two entrys, although there are only one so far, a test, the second printed works like a charm as far as printing is concerned.

http://www.akimm.com/guestbook1/approve.php

this is the file i have so far.

approve.php

Code: Select all

<?php
foreach (glob("*.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>
	<?php
	echo "</table>";
	}
?>
<input type="submit" value="submit for addition to guestbook" name="guestbook">
</form>
ad.php

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

Post by Luke »

Code: Select all

<?php
$fp = fopen('guestbook.txt', 'w');
if($_POST['name'] == "yes" && $fp) {
fwrite($fp, $files); // $files doesn't exist in this page... you created $files on the "approve" page, not this one... variables don't magically get transferred from file to file
fclose($fp);
unlink($_POST['name']); // $_POST['name'] doesn't contain anything... this name is nowhere in your form. You posted three variables to this page... try print_r($_POST) on this page, and you'll see it echo out 'yes', 'no' and 'guestbook' as keys, because that's what is in your form
	}
if($_POST['name'] == "no") { // Once again, you didn't post a "name" variable
unlink($_POST['name']);  // Once again, you didn't post a "name" variable
}
?>
Post Reply