簡體   English   中英

如何使用php將所有圖像文件從目錄移動到子目錄

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

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM