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

  • Home
  • SEARCH
  • 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 6658431
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:54:48+00:00 2026-05-26T01:54:48+00:00

I’m currently building a very low level CMS for friend’s artist web page that

  • 0

I’m currently building a very low level CMS for friend’s artist web page that will allow her to upload, edit, and delete images along with designating categories for them and posting news posts about shows and so on.

I’m sure there is a very easy solution to this problem of mine but my inexperience in programming has me left at a loss; so here goes.

The Problem

The problem occurs on a page where the user can delete an image that has been uploaded. Here is the snippet of code where the problem occurs:

    // Assign selection to variables in memory...
    $img_id = $data["img_name"];

    // First, collect the file path to the image being deleted...
    $rs = mysql_query("SELECT img_path FROM img_uploads WHERE img_id = '$img_id'") or die(mysql_error());
    list($img_path) = mysql_fetch_row($rs);

    // Then delete that row from the DB...
    mysql_query("DELETE FROM img_uploads WHERE img_id = '$img_id'") or die(mysql_error());

    // Now, using the file path collected earlier, delete that file from the server.
    unlink($img_path);

    // Quickly make sure that the file has been deleted by checking if it exists... if it still exists return error.
    if(file_exists($filename)) {
        $err[] = "ERROR - There was an error deleting the file! Please try again.";
        $_SESSION["errors"] = $err;
        header("Location: img_del.php?doDel=failed");
        exit();
    }

    // Scan the directory now that a file has been deleted to see if the dir is empty. If so, delete it. (No use in having empty folders!)
    $file_types = array("gif","jpg","png"); // file types to scan for...
    $path_parts = pathinfo($img_path); // get the directory from the file path...
    $dir = $path_parts["dirname"] . "/"; // assign it to a new variable...
    $handle = opendir($dir);
    $scan = scandir($dir); // now, scan that directory...
    $image_found = FALSE;
    for($i=0; $i<count($scan); $i++) { 
        if ($scan[$i] != '.' && $scan[$i] != '..' && in_array(end(explode('.', $scan[$i])), $file_types)) { 
            $image_found = TRUE;
        }
    }
    closedir($handle);
    if(!$image_found) {
        rmdir($dir);
    }

I first delete the DB row containing image info, then delete the file from the server. this works fine, however, I also want to check if the directory is left empty after deleting that file. I check if the directory is empty using a loop and if no file is found, I run mkdir(). For some reason it keeps returning an error saying that the directory is not empty

I’ve searched the web and this site for a solution but I’ve yet to find one. I’m sure it’s out there but I’m having trouble finding it which why I came here. What should I do?

Thanks in advance for any help submitted!

NOTE
I have also checked for hidden files and folders but no luck…

Here is a link to an image that pretty much sums up my problem in a nutshell

  • 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-05-26T01:54:48+00:00Added an answer on May 26, 2026 at 1:54 am

    Are you sure PHP has permission to delete the file? Since you say you’ve checked for hidden files, this seems to be the only remaining option. CHMOD 0777 when in doubt (I’d never recommend this usually, but if you’re deleting it anyway…), and make sure the folder has the proper owner to let php delete it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
Basically, what I'm trying to create is a page of div tags, each has
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I want use html5's new tag to play a wav file (currently only supported

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.