被重命名()和unlink()异步函数? [英] Are rename() and unlink() asynchronous functions?

查看:150
本文介绍了被重命名()和unlink()异步函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有充分的理由相信,这两个功能重命名()和unlink()是异步的,其中,从我的理解,意味着当函数被调用,它们下面的code是它完成其程序之前继续在文件系统中。这是互联网的应用程序,我会在​​下面解释一个问题,因为后来$ C $,c取决于这些变化已经是一成不变的。那么,有没有一种方法,使两者同步,使code读者冻结当它击中这些功能,直到它的所有任务都充分体现了对文件系统进行?

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?

下面是code在删除 - image.php,这是由AJAX从另一个管理员-images.php(后者将不会显示)被称为:

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";
} ?>`

在此脚本返回其完成文本,管理,images.php触发一个新功能,它填充的图像表从这些重命名,并修剪文件。有时,它会显示旧名称和已假设要删除的文件,和一个简单的页面刷新摆脱他们。这似乎表明,上述PHP脚本通过所有code运行,并吐出呼应文字的mainfile其完成文件系统操作(这一切其他code是长期和复杂的面前,并希望不必要手头的讨论)。

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).

您会发现,我已经尝试了睡眠()函数来停止PHP脚本希望给它时间来完成。这是一个ineligent,以及做事的方式有问题,因为我已经把大量的时间,以确保它的作品每一次,但我不希望用户等待更长的时间比她/他有。

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.

推荐答案

我想他们都是不可以异步的,因为它们返回的结果告诉,如果操作成功与否。

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

我相信会发生问题,因为当您运行 SCANDIR 作出修改后,可以使用缓存的数据,从内存,而不是重新扫描文件系统。

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.

这篇关于被重命名()和unlink()异步函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆