SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2004
    Location
    Belgium
    Posts
    84
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    cleaning out a directory

    Another easy one, but I'm learning:

    This is my array:
    PHP Code:
    print_r ($the_array); 
    and it outputs:
    Array ( [0] => bya_img.inc.php [1] => captions.inc.php [2] => pic1.jpg [3] => pic2.jpg [4] => pic3.jpg [5] => pic4.jpg [6] => pic5.jpg [7] => pic6.jpg [8] => pic7.jpg [9] => Thumbs.db )

    How can i clean out the array so there's only the images (jpg)?

    thanks

  2. #2
    SitePoint Guru gavwvin's Avatar
    Join Date
    Nov 2004
    Location
    Cornwall, UK
    Posts
    686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    foreach($the_array as $key=>$file)
    {
      
    //checks $file that it ends in .jpg, if it doesn't unset that key
      
    if(!preg_match("#.jpg$#i",$file))
      {
         unset(
    $the_array($key));
      }

    I haven't tested that but I think it should work. That might leave gaps in the array key numbering, so if you want to re-assign the numbers you could use sort($the_array)

  3. #3
    SitePoint Addict Quaint's Avatar
    Join Date
    May 2004
    Location
    Netherlands
    Posts
    219
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't think you should use the above, as it deletes the files in your directory, instead of remove the entries from your array! Be cautious with it!

    Instead of
    PHP Code:
    unset($the_array($key)); 
    (which deletes the file)

    Try writing a line of code that removes the KEY from the ARRAY, don't have much time, but that should be easy to find!

    Quaint Tech
    - Blog on web development and web technology.

  4. #4
    Now with customized title Jump's Avatar
    Join Date
    Sep 2002
    Location
    The Restaurant at The End of The Universe
    Posts
    1,423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This should work.
    PHP Code:
     $test_array = Array ('bya_img.inc.php''captions.inc.php''pic1.jpg''pic2.jpg''pic3.jpg''pic4.jpg''pic5.jpg''pic6.jpg''pic7.jpg''Thumbs.db');
      
      foreach(
    $test_array as $file)
      {
          if(
    strpos($file'.jpg'))
          {
              
    $new_array[] = $file;
          }
      }
      
      
    print_r($new_array); 
    Last edited by Jump; Nov 28, 2004 at 13:21.

  5. #5
    SitePoint Addict Quaint's Avatar
    Join Date
    May 2004
    Location
    Netherlands
    Posts
    219
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yup I'd go that way.. Only drawback is that filenames must not have .jpg in their name (for instance: get.jpg.php) but that of course is NOT logical and if you use this in a controlled environment it won't be a problem...

    Quaint Tech
    - Blog on web development and web technology.

  6. #6
    If it aint Dutch it aint much Kilroy's Avatar
    Join Date
    Oct 2003
    Location
    The Netherlands
    Posts
    406
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    foreach($array as $k => $v){
      echo 
    '$v';

    That should do the trick If you want spaces in between, or linebreaks, just add those right after the echo '$v' part.

    Edit: I assumed you wanted to print this... but if you want to only clean it then the above won't work, sorry.

  7. #7
    Now with customized title Jump's Avatar
    Join Date
    Sep 2002
    Location
    The Restaurant at The End of The Universe
    Posts
    1,423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What I would do ultimately is have a function that worked with any extension.

    PHP Code:
     $test_array = Array ('bya_img.inc.php''captions.inc.php''pic1.jpg''pic2.jpg''pic3.jpg''pic4.jpg''pic5.jpg''pic6.jpg''pic7.jpg''Thumbs.db');
      
      function 
    grab_images($array$ext)
      {
          foreach(
    $array as $file)
          {
              if(
    strpos($file$ext))
              {
                  
    $new_array[] = $file;
              }
          }
          Return 
    $new_array;
      }
      
    $test_array grab_images($test_array'.jpg'); 

  8. #8
    SitePoint Guru gavwvin's Avatar
    Join Date
    Nov 2004
    Location
    Cornwall, UK
    Posts
    686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't think you should use the above, as it deletes the files in your directory, instead of remove the entries from your array!
    unset() only unsets an variable, unlink() would remove the file

    http://www.php.net/unset
    http://www.php.net/unlink

  9. #9
    SitePoint Addict Quaint's Avatar
    Join Date
    May 2004
    Location
    Netherlands
    Posts
    219
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you are right, sorry

    Quaint Tech
    - Blog on web development and web technology.

  10. #10
    SitePoint Zealot Koobi's Avatar
    Join Date
    Nov 2003
    Location
    Sri Lanka
    Posts
    156
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Edit:


    Code was edited to type cast $arg to an array



    The thing is, you could have a png or a gif file with a JPG extension...
    Using getimagesize(), PHP will lookup the IMAGETYPE constant so that you can be sure that the file is a JPG:

    PHP Code:
    <?php
        
    function arrCleaner($arg = array())
        {
            
    $arg = (array) $arg;
            foreach(
    $arg as $eachKey => $eachVal)
            {
                
    $type getimagesize($eachVal);
                switch(
    $type[2])
                {
                    case 
    2:
                            
    /* If it's a JPG, do nothing... */
                        
    break;

                    default:
                            
    /* Else, unset that value */
                            
    unset($arg[$eachKey]);
                        break;
                }
            }
            return 
    $arg;
        }
    ?>

    Usage:
    PHP Code:
    /* Assume that $myArr contains your list of files */
    print_r(@arrCleaner($myArr)); 


    Lookup: http://www.php.net/getimagesize/ if the code doesn't make sense to you
    Or let me know if you have problems with this function


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
  •