[英]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.