Solved

PHP - copy/duplicate multiple image files in same directory?

Posted on 2014-04-08
5
1,010 Views
Last Modified: 2014-04-09
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_762_1_med.jpg
front_line_plus_for_cats_762_1_sld.jpg
front_line_plus_for_cats_762_1_sml.jpg
front_line_plus_for_cats_762_1_swp.jpg
front_line_plus_for_cats_762_1_tny.jpg

front_line_plus_for_cats_762_2_med.jpg
front_line_plus_for_cats_762_2_sld.jpg
front_line_plus_for_cats_762_2_sml.jpg
front_line_plus_for_cats_762_2_swp.jpg
front_line_plus_for_cats_762_2_tny.jpg

to

front_line_plus_for_cats_1094_1_med.jpg
front_line_plus_for_cats_1094_1_sld.jpg
front_line_plus_for_cats_1094_1_sml.jpg
front_line_plus_for_cats_1094_1_swp.jpg
front_line_plus_for_cats_1094_1_tny.jpg

front_line_plus_for_cats_1094_2_med.jpg
front_line_plus_for_cats_1094_2_sld.jpg
front_line_plus_for_cats_1094_2_sml.jpg
front_line_plus_for_cats_1094_2_swp.jpg
front_line_plus_for_cats_1094_2_tny.jpg


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);
							  }
		}


?>

Open in new window

0
Comment
Question by:sabecs
  • 3
  • 2
5 Comments
 
LVL 52

Expert Comment

by:Julian Hansen
ID: 39988127
Firstly why will your current code not work?

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.
<?php
$input = array (
  'front_line_plus_for_cats_762_1_med.jpg',
  'front_line_plus_for_cats_762_1_sld.jpg',
  'front_line_plus_for_cats_762_1_sml.jpg',
  'front_line_plus_for_cats_762_1_swp.jpg',
  'front_line_plus_for_cats_762_1_tny.jpg',
  'front_line_plus_for_cats_762_2_med.jpg',
  'front_line_plus_for_cats_762_2_sld.jpg',
  'front_line_plus_for_cats_762_2_sml.jpg',
  'front_line_plus_for_cats_762_2_swp.jpg',
  'front_line_plus_for_cats_762_2_tny.jpg'
);
$id = 762;
$new_record_id = 1094;
for ($i = 0; $i < count($input); $i++) {  
  $image_{$i} = $input[$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'];
    $rootd = "";
    echo "copy( " . $rootd . $image_{$i} . "," . $rootd.$newfile . ")<br/>";
  }
}
?>

Open in new window

0
 

Author Comment

by:sabecs
ID: 39989676
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_762_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.

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

Open in new window


Also, would it be possible to use a wild card and copy say "front_line_plus_for_cats_762_2*" ?
0
 
LVL 52

Accepted Solution

by:
Julian Hansen earned 500 total points
ID: 39990195
In that case I would use the preg_match function to get the filename prefix
<?php
$input = array (
  'front_line_plus_for_cats_762_1.jpg',
  'front_line_plus_for_cats_762_2.jpg',
);
$id = 762;
$new_record_id = 1094;
for ($i = 0; $i < count($input); $i++) {  
  $image = $input[$i];  
  if (preg_match("/(.*)_(\d+)_(\d+).jpg/", $image, $result)) {
     $target = $result[1] . '_' . $new_record_id . '_' . $result[3]. '.jpg';
     $source = $result[1] . '_' . $result[2] . '_' . $result[3] . 'jpg';
     echo "copy($source, $target);<br/> ";
     $target = $result[1] . '_' . $new_record_id . '_' . $result[3]. '_sml.jpg';
     $source = $result[1] . '_' . $result[2] . '_' . $result[3 ] . '_sml.jpg';
     echo "copy($source, $target);<br/> ";
     $target = $result[1] . '_' . $new_record_id . '_' . $result[3]. '_sld.jpg';
     $source = $result[1] . '_' . $result[2] . '_' . $result[3 ] . '_sld.jpg';
     echo "copy($source, $target);<br/> ";
     $target = $result[1] . '_' . $new_record_id . '_' . $result[3]. '_med.jpg';
     $source = $result[1] . '_' . $result[2] . '_' . $result[3 ] . '_med.jpg';
     echo "copy($source, $target);<br/> ";
     $target = $result[1] . '_' . $new_record_id . '_' . $result[3]. '_swp.jpg';
     $source = $result[1] . '_' . $result[2] . '_' . $result[3 ] . '_swp.jpg';
     echo "copy($source, $target);<br/> ";
     $target = $result[1] . '_' . $new_record_id . '_' . $result[3]. '_tny.jpg';
     $source = $result[1] . '_' . $result[2] . '_' . $result[3 ] . '_tny.jpg';
     echo "copy($source, $target);<br/> ";
  }
}
?>

Open in new window

Still not clear on why you need to create the variable $image_{$i} instead of just $image as shown in the code above?
0
 

Author Closing Comment

by:sabecs
ID: 39990433
Thanks Julian, that is perfect..
 I will also just use $image instead of $image_{$i}
0
 
LVL 52

Expert Comment

by:Julian Hansen
ID: 39990767
You are welcome - thanks for the points.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
php ssh2_scp_send 1 48
mysqli 3 21
mysql update statement effect only some rows 4 26
PHP get array item with custom id 4 22
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now