DEV Community

Discussion on: How to copy an image from one folder to another in php?

Collapse
 
lito profile image
Lito
<?php
$old = $_SERVER['DOCUMENT_ROOT'].'/old/images';
$new = $_SERVER['DOCUMENT_ROOT'].'/new/images';

$dh = opendir($old);

while (($file = readdir($dh)) !== false) {
    if (preg_match('/\.jpg$/', $file)) {
        copy($old.'/'.$file, $new.'/'.$file);
    }
}

closedir($dh);
Enter fullscreen mode Exit fullscreen mode