简体   繁体   English

如何使用php将所有图像文件从目录移动到子目录

[英]How can i move all image files from a directory to a sub-directory using php

I am attempting to move all images from my /webfiles directory to my /webfiles/images directory.我试图将所有图像从我的 /webfiles 目录移动到我的 /webfiles/images 目录。 I have managed to do it to a single image using the below code:我设法使用以下代码对单个图像执行此操作:

$imgfiles = glob("webfiles/28.png");
rename($imgfiles[0], "webfiles/images/28.png");

However i have multiple images and the names will be unknown so cannot specify as per the above.但是,我有多个图像,名称未知,因此无法按上述方式指定。

// Get array of all source files
$files = scandir("source");
// Identify directories
$source = "webfiles/";
$destination = "webfiles/images/";
// Cycle through all source files
foreach ($files as $file) {
    if (in_array($file, array(".",".."))) continue;
    // If we copied this successfully, mark it for deletion
    if (copy($source.$file, $destination.$file)) {
        $delete[] = $source.$file;
    }
}

// Delete all successfully-copied files

foreach($delete as $file) {
    unlink($file);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在子目录中使用子目录名称的前缀重命名所有文件,然后将文件移动到新目录? - How can I rename all files in sub-directories with prefix of sub-directory name and then move files to new directory? 如何打印目录和子目录的列表以及子目录中的文件 - How can I print the list of directory and sub-directory along with files inside sub-directory 如何使用php在父目录中创建子目录? - How to create a Sub-directory in parent directory using php? 扫描目录和子目录中的文件,并使用php将其路径存储在数组中 - Scan files in a directory and sub-directory and store their path in array using php 如何使用 .htaccess 将子目录重定向到以参数为子目录的根目录 - How to redirect sub-directory to root directory with parameter as sub-directory using .htaccess 如何将所有子目录页面重定向到父目录? - How to redirect all sub-directory pages to parent directory? 删除所有文件,包括3个月以上的子目录中的文件PHP - Delete all files, including those within a sub-directory older than 3 months PHP 我将所有文件和文件夹从 wordpress 根目录移动到 wordpress 子目录 - I move all the files and folder from the wordpress root directory to wordpress sub directory 我可以使用PHP将文件移动到服务器上的私有目录吗? - Can I move files to the private directory on a server using PHP? PHP Laravel - 从子目录返回视图 - PHP Laravel - Returning view from a sub-directory
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM