[英]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 move all image files from a directory to a sub-directory using php
我试图将所有图像从我的 /webfiles 目录移动到我的 /webfiles/images 目录。 我设法使用以下代码对单个图像执行此操作:
$imgfiles = glob("webfiles/28.png");
rename($imgfiles[0], "webfiles/images/28.png");
但是,我有多个图像,名称未知,因此无法按上述方式指定。
// 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.