sabecs
asked on
PHP - copy/duplicate multiple image files in same directory?
Hi,
I am trying to copy/duplicate some image files in the same directory.
The code below works fine for copying say
'front_line_plus_for_cats_ 762_1.jpg' to 'front_line_plus_for_cats_ 1094_1.jpg '
and
'front_line_plus_for_cats_ 762_2.jpg' to 'front_line_plus_for_cats_ 1094_2.jpg '
but how can I modify it so it also copies
front_line_plus_for_cats_7 62_1_med.j pg
front_line_plus_for_cats_7 62_1_sld.j pg
front_line_plus_for_cats_7 62_1_sml.j pg
front_line_plus_for_cats_7 62_1_swp.j pg
front_line_plus_for_cats_7 62_1_tny.j pg
front_line_plus_for_cats_7 62_2_med.j pg
front_line_plus_for_cats_7 62_2_sld.j pg
front_line_plus_for_cats_7 62_2_sml.j pg
front_line_plus_for_cats_7 62_2_swp.j pg
front_line_plus_for_cats_7 62_2_tny.j pg
to
front_line_plus_for_cats_1 094_1_med. jpg
front_line_plus_for_cats_1 094_1_sld. jpg
front_line_plus_for_cats_1 094_1_sml. jpg
front_line_plus_for_cats_1 094_1_swp. jpg
front_line_plus_for_cats_1 094_1_tny. jpg
front_line_plus_for_cats_1 094_2_med. jpg
front_line_plus_for_cats_1 094_2_sld. jpg
front_line_plus_for_cats_1 094_2_sml. jpg
front_line_plus_for_cats_1 094_2_swp. jpg
front_line_plus_for_cats_1 094_2_tny. jpg
Thanks
I am trying to copy/duplicate some image files in the same directory.
The code below works fine for copying say
'front_line_plus_for_cats_
and
'front_line_plus_for_cats_
but how can I modify it so it also copies
front_line_plus_for_cats_7
front_line_plus_for_cats_7
front_line_plus_for_cats_7
front_line_plus_for_cats_7
front_line_plus_for_cats_7
front_line_plus_for_cats_7
front_line_plus_for_cats_7
front_line_plus_for_cats_7
front_line_plus_for_cats_7
front_line_plus_for_cats_7
to
front_line_plus_for_cats_1
front_line_plus_for_cats_1
front_line_plus_for_cats_1
front_line_plus_for_cats_1
front_line_plus_for_cats_1
front_line_plus_for_cats_1
front_line_plus_for_cats_1
front_line_plus_for_cats_1
front_line_plus_for_cats_1
front_line_plus_for_cats_1
Thanks
<?php
for ($i = 1; $i <= 5; $i++) {
$image_{$i} = $row_query_images['image'.$i];
//if a picture exits lets copy it
if(stristr($image_{$i}, 'nopic') === FALSE) {
$newfile = str_replace ( $id, $new_record_id, $image_{$i});
$rootd = $_SERVER['DOCUMENT_ROOT'];
copy( $rootd.$image_{$i}, $rootd.$newfile);
}
}
?>
ASKER
Thanks for your help.
$image_{$i} gives me the values of
'front_line_plus_for_cats_ 762_1.jpg' , 'front_line_plus_for_cats_ 762_2.jpg' , 'front_line_plus_for_cats_ 762_3.jpg' ...
which comes from my MySQL table.
but my uploads folder has different versions/sizes of the front_line_plus_for_cats_7 62_1.jpg
so I need to also move/copy the _med, _sld, _sml _swp _tny version of files etc
so I was just after a way of simply adding these to my current code?
I am currently out of the office but I might try something similar to below when I get back.
Also, would it be possible to use a wild card and copy say "front_line_plus_for_cats_ 762_2*" ?
$image_{$i} gives me the values of
'front_line_plus_for_cats_
which comes from my MySQL table.
but my uploads folder has different versions/sizes of the front_line_plus_for_cats_7
so I need to also move/copy the _med, _sld, _sml _swp _tny version of files etc
so I was just after a way of simply adding these to my current code?
I am currently out of the office but I might try something similar to below when I get back.
copy( $rootd.$image_{$i}, $rootd.$newfile); //copy main file
copy( str_replace(".jpg","_sml.jpg",$rootd.$image_{$i}), str_replace(".jpg","_sml.jpg",$rootd.$newfile)); //copy _sml file
copy( str_replace(".jpg","_sld.jpg",$rootd.$image_{$i}), str_replace(".jpg","_sld.jpg",$rootd.$newfile)); //copy _sld file
copy( str_replace(".jpg","_med.jpg",$rootd.$image_{$i}), str_replace(".jpg","_med.jpg",$rootd.$newfile)); //copy _med file
copy( str_replace(".jpg","_swp.jpg",$rootd.$image_{$i}), str_replace(".jpg","_swp.jpg",$rootd.$newfile)); //copy _swp file
copy( str_replace(".jpg","_tny.jpg",$rootd.$image_{$i}), str_replace(".jpg","_tny.jpg",$rootd.$newfile)); //copy _tny file
Also, would it be possible to use a wild card and copy say "front_line_plus_for_cats_
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks Julian, that is perfect..
I will also just use $image instead of $image_{$i}
I will also just use $image instead of $image_{$i}
You are welcome - thanks for the points.
As I understand it you are simply replacing the numeric value in the string with a new numeric value so the str_replace code should work.
The following code seems to do what you require.
Open in new window