Jump to content

problems deleting multiple files


Ninjakreborn

Recommended Posts

I have a script here, that does multiple things, all issues are worked out except one, it only deletes 1 file, and not the rest, I don't have any idea what I am doing wrong.  First I have a page that calls the information from a database, and displays it based on what I want.
Administration page
[code]<?php  // Connects to database
$connect = mysql_connect("localhost", "######", "######");
$select = mysql_select_db("funnyemailforwards");
$errorhandler = "";
$management = true;
if (!$connect || !$select) {
$errorhandler = "The database could not connect, or was not selected<br />";
$management = false;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Approval</title>
</head>
<body>
<h3>Welcome Bobby</h3>
<p>These are recently pending database entries, please accept/decline.  Please note declined entries are automatically permanently deleted from the database. These are all updated together, upon submit.  The ones that are approved are set to approved status, the ones that are declined, are immediately deleted from the database, and the file is removed from it's location on the server.  If you check some of the choice, and leave some of the choice alone, they will still be here later, for instance if there are 3 items listed.  If you approve 1, decline 1, and leave the other alone for now, the approved one is updated, the declined one is deleted, and the one left alone, is not touched, and left there, for you to update later.</p>

<?php // script to display information
echo "<form name=\"dataupdating\" action=\"approvalprocessor.php\" method=\"post\">";
$result = mysql_query("SELECT * FROM fileinfo WHERE approval=0 ORDER BY entrydate ASC");
while ($row = mysql_fetch_array($result))
{
extract($row);
$entrydate = "m/d/y";
$website = "http://www.funnyemailforwards.com/apex/";
$site = $website . $funnyurl;
echo "<ul>";
echo "<li><a href=" . '"' . $site . '"' . 'target="_blank">' . $nameoffunny . "</a></li>";
echo "<li>" . $date . "</li>";
echo "<li><input type=\"radio\" name=\"approval[$id]\" value=\"1\">Approve</li>";
echo "<li><input type=\"radio\" name=\"approval[$id]\" value=\"0\">Decline</li>";
echo "</ul>"; 
$manager = true;
}
if ($manager === true) {
echo "<input name=\"update\" type=\"submit\" value=\"Update Database\">";
echo "</form>";
}
?>



</body>
</html>[/code]

Processor Page
[code]<?php
include 'general.inc.php';
?>

<?php
if (isset($_POST['update'])) {

      foreach ($_POST['approval'] as $key => $value) {
    if ($value) {
mysql_query('UPDATE fileinfo SET approval=1 WHERE id='.$key);
echo "Update Successful, update";
    } else {
mysql_query('DELETE FROM fileinfo WHERE id='.$key);
echo "Update Successful, delete from db";
$funnyurl = "../apex/" . $funnyurl;
unlink($funnyurl); // url is set to exactly like this
// http://www.funnyemailforwards.com/apex/uploads/filename.ext
// Follows a path exactly to the file.
    }
}// closes foreach
}// closes isset control structure
?>[/code]

Include for the processor page
[code]<?php  // Connects to database
$connect = mysql_connect("localhost", "#####", "#####");
$select = mysql_select_db("funnyemailforwards");
$errorhandler = "";
$management = true;
if (!$connect || !$select) {
$errorhandler = "The database could not connect, or was not selected<br />";
$management = false;
}
?>
<?php
$result = mysql_query("SELECT * FROM fileinfo WHERE approval=0 ORDER BY entrydate ASC");

$row = mysql_fetch_array($result);
extract($row);
?>[/code]
What is currently happening that I don't want it to, is I finally got it deleting the proper file, if there is one thing to delete, but if I try to delete 2 or more items from the database, it tells me the following.
something along the lines of
Update Successful, delete from dbUpdate Successful, delete from db
Warning: unlink(../apex/../apex/uploads/websitereport.txt): No such file or directory in /home/all/funnyemailforwards/public_html/administration/approvalprocessor.php on line 16
I don't understand why, I think it deletes a file, then tries to delete the same file a second time, I know I have a slight miscalculation somewhere but can't figure out where any advice?
Link to comment
Share on other sites


[b]You're not updating the value of $funnyurl with anything meaningful![/b]  (Haven't I mentioned this about three or four times in previous posts?)

You're just prepending "../apex/" to it.

So it'll look like...:

[code]
filename
../apex/filename
../apex/../apex/filename
../apex/../apex/../apex/filename
(...etc)
[/code]
Link to comment
Share on other sites

I am look, here The funny email like this
$funnyurl = "db variable"
I extracted, w hen you extract you get access to db rows, by variable name.
When I access $funnyurl, I pull what is in the db, I have in the database the uploads/filename.ext
For every one
then I append what I need to
for instance accessing the file I use
$site = "http://www.funnyemailforwards.com/apex/";
Then
$fullurl = $site . $funnyurl;
If I echo that the url from funnyurl is pulled from the database, based on that item
and it is put together with site, and it looks like this
output
http://www.funnyemailforwards.com/apex/uploads/filename.ext
For this example, I have the $funnyurl, it has acecss to the db like usual
the other thing is set
like this
$funnyurl = "../apex/" . $funnyurl;
What that does is it points out of the directory my script is in, into apex and the file location, I know it's right there because if I use just 1 time, if I just delete 1 entry, it deletes it properly, the problem is, it won't do more than 1 at a time, how do I set it up to do all the entries.
I know that the $funnyurl is being pulled from the db, I just modify it with whatever additional information per script I need to gain file access.
See what I mean, any advice.
Or did you mean something else when you said that, I know you said it 4 times, but I already had it set, unless you are referring to something I am not understanding.
Link to comment
Share on other sites

[code]
<?php
$result = mysql_query("SELECT * FROM fileinfo WHERE approval=0 ORDER BY entrydate ASC");

$row = mysql_fetch_array($result);
extract($row);
?>
[/code]

Here you're only loading one row of the resultant dataset.  If you want to return more than one row, you're going to have to loop through the result until it returns false; usually this is implemented with a while loop.

[code]
<?php
    $result = mysql_query("SELECT * FROM fileinfo WHERE approval=0 ORDER BY entrydate ASC");
    if ($result && mysql_num_rows($result)) {
        while ($row = mysql_fetch_assoc($result)) {
            extract($row);
            // ... do stuff
        }
    }
?>
[/code]

Secondly, you're only prepending the $funnyurl variable within the foreach loop with "../apex/".  Look at the loop.  First it starts out with the single value you pulled from the database (I'll call it "filename.ext").  Before the variable is used for anything, you prepend it with "../apex/" ($funnyurl = "../apex/" . $funnyurl;)  Now it's "../apex/filename.ext".  Here the code probably works correctly once.  Then the loop repeats for as many $_POST['approval'] variables are set, and enters that portion of code for as many as are set to "0".  So for each repetition of that part of the loop, $funnyurl is going to be "../apex/../apex/filename.ext", "../apex/../apex/../apex/filename.ext", etc, etc.  That's why you're getting your error:  "unlink(../apex/../apex/uploads/websitereport.txt)"

Do you print out your variables in an effort to debug while you're coding?  It's helpful to see what's really going on while you're developing code.  If you put in some "echo" statements in the loops, you can see if the variables actually contain what you want them to contain at the appropriate points in the script.

I'm assuming that each entry as a seperate file in "uploads", right?  Do you store the filename in the database entry, or have you assigned it a lawful filename based on the id, i.e., "submission_00001.txt"?
Link to comment
Share on other sites

In the db it's named whatever the file was named off there computer, I am working in features into my script later that strip the names down, and make the modified for the server, and make it to where they can't rename files duplicate names, some other small things, after I finish the admin script, and get everything else situated.  But for now it's giving me t his problem, there is an id for each entry yes, the funnyurl field in the db has the following
uploads/filename.ext
It has that exactly with each filename,a nd extension, that way I can modify it with php to match the sitaution, some situations I had to have absolute url, some relative, so I cna change that based on the script, then the url links when  the thing is called is called with whatever they name the file
like www.funnyemailforwards.com/file/whatever
The script searches the table fileinfo under the column funnyname, for any match that everything after file/
Then it pulls the information from the db based on the name, getting id and everything, then breaks apart the url pulled from the db, based on extension and routes it to the proper viewing method, at that time, well you get it, but at this section I am feeding it what I need for it to reach the file, I will try this, I just need it to do this, for each occurence, and when I tried a foreach statement, of some sort, it started spassing, or actually that was a while loop, and the error kept going, over and over again, until it almost crashed my browser.
Link to comment
Share on other sites

It is still not working, I did that, and it's still only deleting one file, and not the rest, I did some minor changes, but I am still reaching my limits, I know a lot about php, more than I did, but not everything, and this is somethign i have no knowledge on, I am trying to create foreach, or while loops but nothing is working.
Link to comment
Share on other sites

I done the below code for you, But i have noticed that all your code is from donations from other users and in my option lots and lots of users have always helped you and you dont seem to go and study the code that everyone provides you and thats bad.

Anyway hope you all the best.





Array deleting example ok.

<?php

// The name of the folder that the files are.

$folder="delete_images/";

// Array format off file names

$test=array("a.txt","b.txt","c.txt");

//loop throw the array to delete the files.

for($i=0; $i<count($test); $i++) {

//declair the varable to delete files and . caternate the folder name.

$delete=$folder.$test[$i];

//unlink the files delete them all from the array.

unlink($delete);

//echo message recomend redirect here.

echo"files deleted";

exit;

}
?>


database deleting example ok.

<?php

// connect to database.

$db=mysql_connect("localhost","name","password");
mysql_select_db("what_ever,$db);

//query database
$query="select * form files";

$result=mysql_query($query);

//while loop
while($record=mysql_fetch_assoc($result) {

// The name of the folder that the files are.

$folder="delete_images/";

//loop throw the array to delete the files.

for($i=0; $i<count($record['$files_to_delete']); $i++) {

//declair the varable to delete files and . caternate the folder name.

$delete=$folder.$test[$i];

//unlink the files delete them all from the array.

unlink($delete);

//echo message recomend  redirect here.

echo"files deleted";

exit;
}
}
?>
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.