To copy an image from one folder to another folder you have to read the image file that you want to copy. Then create another file with the same name as the original file in the directory to where you want to copy.
Copy() function:
The copy() function in PHP is used to copy a file from source to target or destination directory. It makes a copy of the source file to the destination file and if the destination file already exists, it gets overwritten. The copy() function returns true on success and false on failure.
Read more at kodlogs.
Code:
<?php
$old_sub_dir = '/directory/containing/the/images';
$new_sub_dir = '/directory/waiting/for/the/images';
//get the content from directory
$contents = array();
$dir = opendir($_SERVER['DOCUMENT_ROOT'] . $old_sub_dir);
while (false !== ($file = readdir($dir)))
{
$contents[] = $file;
}
closedir($dir);
//get only jpeg images from folder
$jpeg_contents = array();
foreach($contents as $file) {
if (eregi('.jpg{1}$', $file)) {
$jpeg_contents[] = $file;
}
}
// copy each jpeg from directory 'source' to directory 'destination'
foreach($jpeg_contents as $file) {
copy($_SERVER['DOCUMENT_ROOT'] . $old_sub_dir . '/' . $file, $_SERVER['DOCUMENT_ROOT'] . $new_sub_dir . '/' . $file);
}
?>
Top comments (1)