Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7796441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T23:22:25+00:00 2026-06-01T23:22:25+00:00

Don’t know much about running a function on every item in an array, still

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T23:22:26+00:00Added an answer on June 1, 2026 at 11:22 pm

    If $cell_pictures[‘pic_loc’] is an array of files to be deleted then

    foreach($cell_pictures['pic_loc'] as $pics_being_deleted) //deletes all pictures found
    {
    
    unlink("../../".$pics_being_deleted);
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I don't have much PHP experience and I want to know how to best
Don't know much about encryption... Say I'm preparing a SAML request to submit to
Don't really know how to formulate the title, but it should be pretty obvious
I don't know if this is a well known 'thing' or something new in
I don't want to know a way to preload images, I found much on
Don't know a whole lot about streams. Why does the first version work using
Don't know why, but sometimes LocationManager is still working also after closing application. I
Don't freak out about the length of my post, it's pretty simple, I just
Don't know how to google for such, but is there a way to query
Don't know why but font is not displaying.Please help. CSS(in css folder): style.css: @font-face

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.