Check image exists with patteren in filename

Discussion in 'PHP' started by Python, Feb 8, 2007.

  1. #1
    I have a directory with images in it. Their filenes are in the format:

    The x above represents an ID which is a unique number. The 0's are basically the date using the date() function.

    I want a scrript which will search to see if there is a file which its ID is for example 6 regardless of its date.

    So if there is a file called 6-0000000000.jpg then it will return its whole filename. If not then it will return false.

    How can I do this?

    Thanks
     
    Python, Feb 8, 2007 IP
  2. Icheb

    Icheb Peon

    Messages:
    1,092
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Look through php.net for file handling. You will find a function to open a directory and loop through the contents. With that, you just check if the first character is a 6.
     
    Icheb, Feb 8, 2007 IP
  3. technoguy

    technoguy Notable Member

    Messages:
    4,369
    Likes Received:
    306
    Best Answers:
    0
    Trophy Points:
    205
    #3
    
    <?php 
    $maindir = "." ; 
    $mydir = opendir($maindir) ; 
    $x='6';
    while($fn = readdir($mydir)) 
    { 
    if(substr($fn,1,1)=='$x')
    {
    echo $fn;
    } 
    else
    {
    return 0;
    }
    } 
    closedir($mydir); 
    ?>
    
    PHP:
     
    technoguy, Feb 8, 2007 IP
  4. Icheb

    Icheb Peon

    Messages:
    1,092
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #4
    People will never learn how to code on their own if you just hold their hands. Your code has a mistake in it anyway.
     
    Icheb, Feb 8, 2007 IP
  5. exam

    exam Peon

    Messages:
    2,434
    Likes Received:
    120
    Best Answers:
    0
    Trophy Points:
    0
    #5
    @technoguy: look up substr in the docs :)
     
    exam, Feb 8, 2007 IP
  6. exam

    exam Peon

    Messages:
    2,434
    Likes Received:
    120
    Best Answers:
    0
    Trophy Points:
    0
    #6
    To be more helpful, I'll give you an example.

    
    1. Open the dir
    2. Loop through all the files in the dir
    3. On each file, check if it's a jpg file by comparing substr ($filename, -4, 4) to ".jpg"
    4. If it's a jpg file, use explode to separate the two parts (before and after the dash).  Now the first part is your ID (It could be longer than one char, that's why we use explode)
    5. If that first part is the ID you were looking for, then do what you want to do with the filename (output it I presume)
    
    Code (markup):
     
    exam, Feb 8, 2007 IP
  7. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #7
    also no need to return anything .....

    @the op, do you wanna learn or just want the code ?
     
    krakjoe, Feb 8, 2007 IP
  8. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #8
    2 actually. Variables won't be parsed between single quotes. EDIT: 3, look up readdir() as well.

    And give this a try. (untested)
    
    
    function image_exists($imageid, $imagedir = './images/')
    {
         $files = glob($imagedir . $imageid . '-*.jpg');
    
         if (sizeof($files) > 0)
         {
              return basename($files[0]);
         }
         
         return false;
    }
    
    
    PHP:
     
    nico_swd, Feb 8, 2007 IP
  9. Icheb

    Icheb Peon

    Messages:
    1,092
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #9
    That was actually the first I noticed and I didn't bother looking any further. :p
     
    Icheb, Feb 8, 2007 IP
  10. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #10
    Lol. :p


    And actually, substr() isn't even needed. Remember that strings can be used as array too. So you'd just have to do $fn[0] to check the first character.
     
    nico_swd, Feb 8, 2007 IP
  11. Python

    Python Well-Known Member

    Messages:
    680
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    120
    #11
    Thanks for all your help guys :D
     
    Python, Feb 8, 2007 IP