Page 1 of 5
Using unlink() with an if/else statement to delete files
Posted: Sun Sep 24, 2006 8:23 pm
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);
}
?>
Posted: Sun Sep 24, 2006 9:26 pm
by feyd
I'm not too sure what you're asking...
Posted: Sun Sep 24, 2006 9:47 pm
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
Posted: Sun Sep 24, 2006 10:09 pm
by Burrito
unlink() takes a string for the file name...you're passing it an array.
Posted: Sun Sep 24, 2006 10:14 pm
by akimm
Is there a function to delete a file, perhaps unset()?
Posted: Sun Sep 24, 2006 10:16 pm
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.
Posted: Sun Sep 24, 2006 10:42 pm
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);
}
?>
Posted: Sun Sep 24, 2006 10:51 pm
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.
Posted: Sun Sep 24, 2006 10:55 pm
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.
Posted: Sun Sep 24, 2006 11:21 pm
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']);
}
?>
Posted: Mon Sep 25, 2006 12:20 am
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?
Posted: Mon Sep 25, 2006 5:05 am
by panic!
I was reading through disparing that no one had picked that up, well done Aaron.
ok
Posted: Mon Sep 25, 2006 2:17 pm
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.
Posted: Mon Sep 25, 2006 2:42 pm
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']);
}
?>
Posted: Mon Sep 25, 2006 3:10 pm
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
}
?>