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 6962567
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:40:38+00:00 2026-05-27T15:40:38+00:00

I have strong reason to believe that both functions rename() and unlink() are asynchronous,

  • 0

I have strong reason to believe that both functions rename() and unlink() are asynchronous, which, from my understanding, means that when the functions are called, the code below them are continued before it finishes its procedures on the filesystem. This is a problem for the internet app I’ll explain below, because later code depends on these changes to already be set in stone. So, is there a way to make both synchronous, so that the code reader freezes when it hits these functions, until all of its tasks are fully carried out on the filesystem?

Here is the code in delete-image.php, which is called by ajax from another admin-images.php(the latter will not be shown):

`

    foreach ($dirScan as $key => $value) {
        $fileParts = explode('.', $dirScan[$key]);
        if (isset($fileParts[1])) {
            if ((!($fileParts[1] == "gif") && !($fileParts[1] == "jpg")) && (!($fileParts[1] == "png") && !($fileParts[1] == "jpg"))) {
                unset($dirScan[$key]);
            }
        } else {
            unset($dirScan[$key]);
        }
    }
    $dirScan = array_values($dirScan);

    // for thumbnail
    $file = 'galleries/' . $currentGal . '/' . $currentDir . "/" . $dirScan[$imageNum - 1];
    unlink($file);      
    for ($i = ($imageNum - 1) + 1; $i < count($dirScan); $i++) {
        $thisFile = 'galleries/' . $currentGal . '/' . $currentDir . '/' . $dirScan[$i];
        $thisSplitFileName = explode('.', $dirScan[$i]);
        $newName = 'galleries/' . $currentGal . '/' . $currentDir . "/" . ($thisSplitFileName[0] - 1)  . "." . $thisSplitFileName[1];
        rename($thisFile, $newName);
    }

    // for large image
    $fileParts = explode('.', $dirScan[$imageNum - 1]);
    $file = 'galleries/' . $currentGal . '/' . $currentDir . "/large/" . $fileParts[0] . "Large." . $fileParts[1];
    unlink($file);
    for ($i = ($imageNum - 1) + 1; $i < count($dirScan); $i++) {
        $thisSplitFileName = explode('.', $dirScan[$i]);
        $thisFile = 'galleries/' . $currentGal . '/' . $currentDir . '/large/' . $thisSplitFileName[0] . "Large." . $thisSplitFileName[1];
        $newName = 'galleries/' . $currentGal . '/' . $currentDir . "/large/" . ($thisSplitFileName[0] - 1)  . "Large." . $thisSplitFileName[1];
        rename($thisFile, $newName);
    }

    sleep(1);
    echo 'deleted ' . $dirScan[$imageNum - 1] . " successfully!";

} else {
    echo "please set the post data";
} ?>`

After this script returns its completed text, admin-images.php triggers a new function which populates an image table from these renamed and trimmed files. Sometimes it displays old names and files that were suppose to be deleted, and a simple page refresh gets rid of them. This seems to suggest that the above php script is running through all the code and spitting out echoed text to the mainfile before it completes its file-system manipulation (All of this other code is long and complicated, and hopefully unnecessary for the discussion at hand).

You’ll notice, I’ve tried a sleep() function to halt the php script to hopefully give it time to finish. This is an ineligent, and problematic way of doing things, because I have to put a large amount of time to insure it works every-time, but I don’t want the user to wait longer than she / he has to.

  • 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-27T15:40:39+00:00Added an answer on May 27, 2026 at 3:40 pm

    I suppose they are not asynchronous, because they return a result telling if the operation was successful or not.

    I believe the problem happens because when you run scandir after making the modifications, it may be using “cached” data, from memory, instead of re-scanning the file system.

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

Sidebar

Related Questions

I have a Rails method that just returns a string, and for some reason,
This is an SDL problem, however I have the strong feeling that the problem
I have a form for editing a ticket that requires a Reason for Edit.
I have validated the JSON response from my C# Webmethod, so I don't believe
I have a script set up that for reasons of necessity gets both the
I have an update query that shouldbe working but for some reason it doesnt
I have a code to concatenate strings. However, for some reason, the final string
Is it really important to know algorithms to build mobile applications? I have strong
Using t-sql, how can i find if all SQL Logins have strong passwords on
I have a strong database background but very little practical experience with ms sql

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.