循环并重命名php中的所有子文件夹 [英] loop and rename all subfolders in php

查看:111
本文介绍了循环并重命名php中的所有子文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难找到有关遍历文件夹中所有子文件夹并使用PHP重命名它们的任何信息。我正在重命名它们,以使子文件夹的数量不受限制。

I'm struggling to find any information on looping through all subfolders within a folder and renaming them using PHP. I am renaming them so that I do not reach any limits on numbers of subfolders.

目前,我的用户文件夹结构如下:

At the moment my user folder structure is as follows :

images/logos/1216/logo.jpg
images/logos/11437/logo.jpg
images/logos/234438/logo.jpg

所以我想遍历徽标中的所有文件夹,并按以下方式重命名它们:

So I want to loop through all folders within the 'logos' and rename them as follows :

images/users/1/1216/logos/logo.jpg
images/users/11/11437/logos/logo.jpg
images/users/234/234438/logos/logo.jpg

为了计算新的子文件夹名称,我将使用现有的用户ID(即11437并除以1000)。

To calculate the new subfolder name, i'm going to take the existing user id (i.e. 11437 and divide by 1000).

实际的问题是,如何循环通过所有子文件夹,以及重命名文件夹结构的最佳方法是什么。

The actual issue is how, do I loop through all the subfolders and what is the best way to rename the folder structure.

推荐答案

如果我对第一行是正确的在这里,您将获得类似于第二行的数组外观。首先进行测试,然后备份文件。

If I'm correct about the first line of code here you sholud get an array lookin like the second line. Test that first and make a backup of your files.

这应该列出所有用户ID或目录并循环浏览。

如果复制成功(true),它将取消旧文件的链接,否则返回错误消息。

This should list all userid's or directories and loop through them.
If the copy is success (true) it unlinks the old file, else returns a error message.


这是未经测试的代码



//$arr = array_filter(glob('images/logos/*'), 'is_dir');
$arr = array("images/logos/1216","images/logos/11437","images/logos/234438");
//var_dump($arr);

foreach($arr as $dir){
    $UID = str_replace("images/logos/", "", $dir);
    $newpath = "images/users/" . floor($UID/1000) . "/" . $UID . "/logos/logo.jpg";
    $oldpath = $dir . "/logo.jpg";
    if(copy($oldpath, $newpath)){
        unlink($oldpath);
    }else{
        echo "error with file " . $oldpath . "<br>\n";
        $error = true;
    }
}

if(!$error) unlink("images/logos");

编辑;只是注意到最近我使用Excel太多了。更改了&到。

EDIT2;现在在我自己的页面上测试了代码,似乎glob返回了路径。包含获取用户ID的代码。

EDIT; Just noticed I had used Excel too much lately. Changed the & to .
EDIT2; tested the code on my own page now and it seems the glob returns the path. Included code to get the UserID.

这篇关于循环并重命名php中的所有子文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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