SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Wizard lukeurtnowski's Avatar
    Join Date
    Mar 2003
    Location
    Coronado
    Posts
    1,582
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    looping througharray

    I have the following array
    HTML Code:
    <pre>Array ( ...     [file] =&gt; Array         (             [0] =&gt; 1370545724-reverand_mcelrath.png             [1] =&gt; 1370545724-steven_currey.png             [2] =&gt; 1370545724-rudy_barlaan.png         )  ) ‘</pre>
    and id simply like to loop through it, rename it, and move it to new directory, is this ok so far?
    PHP Code:
    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?
    "Oh, and Jenkins--apparently your mother died this morning."

  2. #2
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,578
    Mentioned
    62 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by lukeurtnowski View Post
    I have the following array
    HTML Code:
    <pre>Array ( ...     [file] => Array         (             [0] => 1370545724-reverand_mcelrath.png             [1] => 1370545724-steven_currey.png             [2] => 1370545724-rudy_barlaan.png         )  ) ‘</pre>
    and id simply like to loop through it, rename it, and move it to new directory, is this ok so far?
    PHP Code:
    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:

    PHP Code:
      $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 => Array
    (
    [0] => 1370545724-reverand_mcelrath.png
    [1] => 1370545724-steven_currey.png
    [2] => 1370545724-rudy_barlaan.png
    )

    Missing $image:
    $img => 1370545724-reverand_mcelrath.png
    $old => images/uploads/1370545724-reverand_mcelrath.png
    $tmp => images/2/1370545724-reverand_mcelrath.png
    $new => images/2/1370545724-reverand_mcelrath.jpg

    Missing $image:
    $img => 1370545724-steven_currey.png
    $old => images/uploads/1370545724-steven_currey.png
    $tmp => images/2/1370545724-steven_currey.png
    $new => images/2/1370545724-steven_currey.jpg

    Missing $image:
    $img => 1370545724-rudy_barlaan.png
    $old => images/uploads/1370545724-rudy_barlaan.png
    $tmp => images/2/1370545724-rudy_barlaan.png
    $new => images/2/1370545724-rudy_barlaan.jpg

  3. #3
    SitePoint Wizard lukeurtnowski's Avatar
    Join Date
    Mar 2003
    Location
    Coronado
    Posts
    1,582
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    thanks
    "Oh, and Jenkins--apparently your mother died this morning."

  4. #4
    SitePoint Wizard lukeurtnowski's Avatar
    Join Date
    Mar 2003
    Location
    Coronado
    Posts
    1,582
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    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?
    "Oh, and Jenkins--apparently your mother died this morning."

  5. #5
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,609
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    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?
    Ian Anderson
    www.siteguru.co.uk

  6. #6
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,578
    Mentioned
    62 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by lukeurtnowski View Post
    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?
    Yes there are lots

    ...and a quick Google search revealed:
    PHP Code:

    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($filenamestrrpos($filename'.') + 1);

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

    echo 
    '<br />'__LINE__' : ',  
      
    $ext pathinfo($filenamePATHINFO_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
    Last edited by John_Betong; Jun 7, 2013 at 18:58. Reason: spelling: not my fortay


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •