Click to See Complete Forum and Search --> : Copying images from one directory to another


SFADuncan
03-13-2005, 04:38 AM
Hi...

I want to go into a number of directories, get all the .jpgs, and copy them to another set of corresponding directories...

This is my pathetic attempt so far....

function copy_img_photo_img($wwwname, $photype, $phono, $pictitle, $picdesc)
{
include ("variables.php");

foreach (glob("/home/virtual/".$url."/var/www/html/".$wwwname."/photo/".$photype."/*.jpg") as $filename)

$new_place = "/home/virtual/".$url."/var/www/html/".$wwwname."/photo_img/".$photype"/");

copy($filename, $new_place);
}

Any ideas?

bokeh
03-13-2005, 07:14 PM
This works just fine for the transfer but you will have to add some output for the browser (and maybe some error checking).

<?php

$old_sub_dir = '/directory/containing/the/images';

$new_sub_dir = '/directory/waiting/for/the/images';

//get directory contents
$contents = array();

$dir = opendir($_SERVER['DOCUMENT_ROOT'] . $old_sub_dir);

while (false !== ($file = readdir($dir))) {
$contents[] = $file;
}
closedir($dir);

//get only jpeg contents
$jpeg_contents = array();

foreach($contents as $file){
if (eregi('.jpg{1}$', $file)){
$jpeg_contents[] = $file;
}
}

// copy each jpeg from directory 'a' to directory 'b'
foreach($jpeg_contents as $file){
copy($_SERVER['DOCUMENT_ROOT'] . $old_sub_dir . '/' . $file, $_SERVER['DOCUMENT_ROOT'] . $new_sub_dir . '/' . $file);
}

?>

SFADuncan
04-08-2005, 09:58 AM
Thanks... fantastic... very helpful, appreciate it