Don’t know much about running a function on every item in an array, still pretty new to PHP, below $pic_loc is a data array from MySQL containing the path and filename to images matching whatever search criteria is preformed.
Tried to get the unlink function to run on everyone that it found. No errors return. I don’t think foreach is the proper way to go, can anyone point me in the right direction?
$pic_loc = $cell_pictures['pic_loc']; //gathers an array from the database of files locations
$pics_to_delete .= "../../".$pic_loc; //corrects the directory
foreach($pics_to_delete as $pics_being_deleted) //deletes all pictures found
{
unlink($pics_being_deleted);
}
EDIT:
I tinkered with it alot with both answers, I know I can delete files 2 tiers up because i can do it for individual files with no problem. This is what I’m currently doing, i got rid of the while() loop figuring maybe that was causing an issue for the foreach to function:
$data_array2 = mysql_query("SELECT * FROM pictures WHERE user_id = '".$id."'");
$cell_pictures = mysql_fetch_array($data_array2);
foreach($cell_pictures['pic_loc'] as $pics_being_deleted) //deletes all pictures found
{
unlink("../../".$pics_being_deleted);
}
here’s the error it returns now:
Warning: Invalid argument supplied for foreach() // on the line that the foreach is on
EDIT:
Ok that was screwy, BUT I GOT IT!!! you would think that the data coming from the query would’ve been an array, but no you actually had to set it as an array. This is how I did it:
$data_array2 = mysql_query("SELECT pic_loc FROM pictures WHERE user_id = '".$id."'");
while($cell_pictures = mysql_fetch_array($data_array2))
{
$the_pics = $cell_pictures['pic_loc'];
$pics_as_array = array($the_pics);
}
foreach($pics_as_array as $pics_being_deleted)
{
unlink("../../".$pics_being_deleted);
}
This works excellent. Thanks a whole bunch for taking the time to read this!
-Mike
If $cell_pictures[‘pic_loc’] is an array of files to be deleted then