I have a page that allows you to add images to the server.

As of course you are able to add them to the server you should be able to delete them.

Now the problem I have is that.

I cannot delete the folder if there are contents in it. (as it would be easier to delete all the contents in the folder along with the folder itself)

So I have to use UNLINK first and then RMDIR to delete the folder completely.

Now there are several files in that folder to be deleted and the name is stored in a database.

So the idea I had was to use the database to retrieve the name and then insert the name into a FILE EXISTS and then if that file exists use UNLINK and then once this has gone through 5 times (as maximum each person can have is 5 images) it then does RMDIR.

Now any help be greatly appericated!?!??! THANKYOU!!!

PS: Off to dinner so this might have been abit rushed, ill try fix it up later if it dont make sense!

What is the problem ? When the user uploads an image, I m sure you will be saving the path of the image. When the user clicks on delete, get the id of that image and unlink the file. What exactly is your problem ?

i think he is just after a loop/counter to make sure that 5 files have been removed

oh..hmm ! If the user has uploaded the images, there will be an entry for every uploaded images in the database. (ie., the path of the 5 images.) Get all the records for that particular user and then delete them. for example, if the user with userid 3 has uploaded 5 images. There will be 5 entries in the table image_upload(which contains the path of the images).

$q="select * from image_upload where userid='5'";
$result=mysql_query($q); //the query returns 5 records 
while($row=mysql_fetch_array($result)){
  $imagepath=$row['path']; //$imagepath will have the path of the uploaded image. 
unlink($imagepath);//delete the file
//then delete the directory. I hope you are saving the images into the directory which is named after userid.
}
rmdir(5); //remove the directory with userid 5. 
?>
Comments
*grabs nav33n by the ear* i (((DONNOT))) wear glasses anymore!

Ya thats something similar to what I have BUT.

I have the images stored in a common folder called PEOPLE and then subfolders called 1, 2, 3, etc.

Now how would i specify that imagename in that image path?

Dont forget that NOT all folders contain 5 images some 1, some 3, etc.

so what would you do with that then?

Thanks again nav, your awesome :)

Are you storing the path of the uploaded image in the table? If yes, you can do as I have mentioned above. If you aren't storing the path in the table, then you need to opendir , readdir and then unlink the files. See example 2 of readdir. Instead of echo-ing, you can unlink.

Ok I dont know how to explain exactly what I have done with the code as I dont have it here in front of me.

In short I have assigned a variable to the path.
Eg. Code is something like this off the top of my head:

$file = $people . '/' . $folder . '/' . $file[a]
$folder = $people . '/' . $folder
if(file_exists($file[a]) {
unlink($file[a]);
}

That array [a] is looped 5 times in a for loop. Then:

rmdir($folder);

So any ideas?

No. My question is, are you storing the path in the table when the user uploads an image ?

I am storing the name of the file, not the path in a table.

I dont see the need to store a path in a table?

If you had stored the path, it would have been easy to unlink that file. Just get the path from the table and unlink that 'path'.

hmm I really dont wana do that but ill have to rewrite my code then....

Ok if I do decide I am just goign to add a column to mySQL database named "path" then save that path which will be like "/cars/12/"
and ill still keep the column for the actual name of the object "amanda123.jpg"?

When you upload the file, you get the path. For, eg,
folder/test/image.jpg. Upload that to the table. You can keep the image name for your reference though.

Oh ya I remembered why I didnt make the paths in the first place!?!?!

Why should I make an extra column for nothing where the people folder is always PEOPLE and the 123 is always the PEOPLEID which is already a field and the IMAGE NAME has already a field. So then wouldnt it be easier calling those fields into one string?

Let me know what you think best and Ill see what I can come up with on the weekend.

ah! then thats not a problem. Just get the image name from the table and then concat it with $path="PEOPLE/".$peopleid."/".$imagename !

More problems cropped up after I tried this.

Nav, possible u can give me a small example that uses php to extract the image path (aka variables) used to make the delete path and used in an unlink function?

Thanks

Say for example, I upload 5 images(1,2,3,4,5 .jpg) to the database. So, there will be 5 entries in the table with the peopleid, imagename, etc.
So, If I want to delete an image, say, 4.jpg, I can do it this way.

$image_to_delete = "4.jpg";
$peopleid = my_user_id ;
$path_of_the_image = "people/".$peopleid."/".$image_to_delete;
if(file_exists($path_of_the_image)){
 unlink($path_of_the_image);
} else {
echo "File doesn't exist! ";
}

A simple example.

ok were getting somewhere now!

I can actually delete the object BUT not by calling the name from mysql.

Do you know if I am using the function "$test = mysql_fetch_array" how would I access a variable in that funciton (test["people"]that would give me the value stored in the table as 4.jpg? )

Because after using ur code it works but the problem is in the name I cant seem to retrieve names from the database, its being a real pain. =(

Thanks agai, nav

Do you know if I am using the function "$test = mysql_fetch_array" how would I access a variable in that funciton (test["people"]that would give me the value stored in the table as 4.jpg? )

Yep. When you fetch a record from mysql table using mysql_fetch_array, the record will be returned as an array and will be stored in whatever variable you use. $test = mysql_fetch_array($result) will make $test store the array returned by $result. So, you can access that particular array element by specifying the column name,like, $test.

I dont know whats wrong :(

Thats what I have been doing:

$banana =$test['food'];

That variable should contain the value for food, correct?

Regards, X

yep. print out the array values by using print_r($test). You will know what values does the array $test contains.

Still having a few issues, give some time to work on them and ill get back to nav. Thanks :)

I still cannot issolate the problem but just wana check my code, can anyone see any problems?

for($i= 1; $i<= 5; $i++) {
 $image = "image" . $i . "person";
 $data = $fetch_person[$$image];
 $file = $person . "/" . $number . "/" . $data;
 $folder = $person . "/" . $number;
 if(file_exists($file)) {
  unlink($file);
 }
rmdir($folder);
}

See any problems?
Not sure but:
Is it possible to refer to a cell using a $$image variable? (yes double $$)

Note:
The statement:

unlink("people/1/Tom.jpg");

Works fine when I use it as a replacement.

I think the error is somewhere in the actual ($file or $$image) if I had to lay money on anyways. Let me know.

Very much appericated.

Regards, X

Still testing and one error I got was permission denied?

Warning: unlink(people/8/) [function.unlink]: Permission denied

Back to testing...

Further testing has shown there might be an error with the

$data = $fetch_person[$$image];

Dosent seem to work with the $$

Back to testing...

Further testing has shown that I cannot seem to use a variable with the 'mysql_fetch_array' statement ($data = $fetch_person[$$image]; ) wether it is a $ or a $$ and I dont know why because I have used variables before...
Hmmm I just tried using arrays instead of passing the variable through as $$ and using arrays so then it is $ but neither worked.

So at a dead end once again so time for a long break...

Any Ideas, much appericated.

Regards, X

Further testing...

Ive made some progress but I am getting errors from my "file_exists" statement its saying its always true when it should be false at times, ideas?

Through various little ends, I solved it!

Funny thing is though I couldnt see any errors as such just abit of bad/lazy coding but no errors :(

Thanks all for the help.

This question has already been answered. Start a new discussion instead.