Looping througharray

I have the following array


<pre>Array ( ...     [file] => Array         (             [0] => 1370545724-reverand_mcelrath.png             [1] => 1370545724-steven_currey.png             [2] => 1370545724-rudy_barlaan.png         )  ) &#8216;</pre>

and id simply like to loop through it, rename it, and move it to new directory, is this ok so far?


foreach ($file as $image=>$key)
{
//rename it and move it to a new directory
rename('images/uploads/'.$image, 'images/2/'.$key.'.jpg');
}

Another problem I just noticed is thow do I change the format of whats in the array to a jpg?

Try this:


  $imgs = array
  (
    // ... [file] => 
    '1370545724-reverand_mcelrath.png',             
    '1370545724-steven_currey.png',             
    '1370545724-rudy_barlaan.png',         
  );
  echo '<pre> $imgs => '; print_r($imgs); echo '</pre>';

  echo '<dl>';
  foreach ($imgs as $key => $img):
    //rename it and move it to a new directory
    $old = 'images/uploads/'  .$img;
    $tmp = 'images/2/'        .$img;
    $new = str_replace('.png', '.jpg', $tmp); 
    if( file_exists( $old )):
      rename( $old, $new);
    else:
        echo '<dt>  Missing $image: </dt>';
        echo '<dd>  $img => ', $img ,'</dd>';
        echo '<dd>  $old => ', $old ,'</dd>';
        echo '<dd>  $tmp => ', $tmp ,'</dd>';
        echo '<dd>  $new => ', $new ,'</dd>';
        echo '<dd> &nbsp;</dd>';
    endif;
  endforeach;
  echo '</dl>';


ouput


$imgs =&gt; Array
(
    [0] =&gt; 1370545724-reverand_mcelrath.png
    [1] =&gt; 1370545724-steven_currey.png
    [2] =&gt; 1370545724-rudy_barlaan.png
)

Missing $image:
    $img =&gt; 1370545724-reverand_mcelrath.png
    $old =&gt; images/uploads/1370545724-reverand_mcelrath.png
    $tmp =&gt; images/2/1370545724-reverand_mcelrath.png
    $new =&gt; images/2/1370545724-reverand_mcelrath.jpg
     
Missing $image:
    $img =&gt; 1370545724-steven_currey.png
    $old =&gt; images/uploads/1370545724-steven_currey.png
    $tmp =&gt; images/2/1370545724-steven_currey.png
    $new =&gt; images/2/1370545724-steven_currey.jpg
     
Missing $image:
    $img =&gt; 1370545724-rudy_barlaan.png
    $old =&gt; images/uploads/1370545724-rudy_barlaan.png
    $tmp =&gt; images/2/1370545724-rudy_barlaan.png
    $new =&gt; images/2/1370545724-rudy_barlaan.jpg

thanks

I do have 1 more question though…
Since the image may be in some other format (the extension may change)
Is there a way to account for that?

The example above simply changes the file extension - it doesn’t actually change the FILE.

Are you wanting to physically convert the image files from whatever they are to JPEGs?

Yes there are lots :slight_smile: :slight_smile:

…and a quick Google search revealed:



echo '<br />$filename: ',
  $filename = 'lukeurtnowski-Google-Search-php-find-file-extension.png';

echo '<br />', __LINE__, ' : ',  
  $ext = substr(strrchr($filename, '.'), 1);

echo '<br />', __LINE__, ' : ',  
  $ext = substr($filename, strrpos($filename, '.') + 1);

echo '<br />', __LINE__, ' : ',  
  $ext = preg_replace('/^.*\\.([^.]+)$/D', '$1', $filename);

echo '<br />', __LINE__, ' : ',  
  $ext = pathinfo($filename, PATHINFO_EXTENSION);

echo '<br />', __LINE__, ' : ',  
  pathinfo($filename)['extension']; 
  $file = new SplFileInfo($filename); 

echo '<br />', __LINE__, ' : ',  
  $ext  = $file->getExtension();  

echo '<br />', __LINE__, ' : ',  
  $ext  = (new SplFileInfo($filename))->getExtension();  

echo '<br />', __LINE__, ' : ',  
  $ext = end( @explode('.', $filename));


// Output:

$filename: lukeurtnowski-Google-Search-php-find-file-extension.png
21 : png
24 : png
27 : png
30 : png
33 : png
37 : png
40 : png
43 : png