我需要在目录中找到一个文件并将其复制到另一个目录

发布于 2024-08-29 11:56:31 字数 93 浏览 1 评论 0原文

我只有文件名,没有扩展名(.txt、.eps 等) 该目录有几个子文件夹。因此,该文件可以位于任何地方。

如何查找不带扩展名的文件名并将其复制到不同的目录?

I merely have the file name, without extension (.txt, .eps, etc.)
The directory has several subfolders. So, the file could be anywhere.

How can I seek the filename, without the extension, and copy it to different directory?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

迷路的信 2024-09-05 11:56:31

http://www.pgregg.com/projects/php/preg_find/ preg_find.php.txt 似乎正是您所需要的,可以找到该文件。然后只需使用普通的 php copy() 命令 http://php.net/manual/ en/function.copy.php 复制它。

http://www.pgregg.com/projects/php/preg_find/preg_find.php.txt seems to be exactly what you need, to find the file. then just use the normal php copy() command http://php.net/manual/en/function.copy.php to copy it.

疾风者 2024-09-05 11:56:31

看看这个 http://php.net/manual/en/function.copy .php

至于查找文件名,可以使用数据库来记录文件的位置吗?并使用该日志来查找您的文件

have a look at this http://php.net/manual/en/function.copy.php

as for seeking filenames, could use a database to log where the files are? and use that log to find your files

冬天的雪花 2024-09-05 11:56:31

我发现 scandir() 是此类操作最快的方法

function findRecursive($folder, $file) {
    foreach (scandir($folder) as $filename) {
        $path = $folder . '/' . $filename;
        # $filename starts with desired string
        if (strpos($filename, $file) === 0) {
            return $path;
        }
        # search sub-directories
        if (is_dir($path)) {
            $result = findRecursive($path);
            if ($result !== NULL) {
                return $result;
            }
        }
    }
}

:文件,您可以使用 copy()

copy(findRecursive($folder, $partOfFilename), $targetFile);

I found that scandir() is the fastest method for such operations:

function findRecursive($folder, $file) {
    foreach (scandir($folder) as $filename) {
        $path = $folder . '/' . $filename;
        # $filename starts with desired string
        if (strpos($filename, $file) === 0) {
            return $path;
        }
        # search sub-directories
        if (is_dir($path)) {
            $result = findRecursive($path);
            if ($result !== NULL) {
                return $result;
            }
        }
    }
}

For copying the file, you can use copy():

copy(findRecursive($folder, $partOfFilename), $targetFile);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文