Scaning a directory then compare 1 file name with 1 row name

Discussion in 'PHP' started by PET, Oct 18, 2008.

  1. #1
    Hello there,

    I have a directory with about 1800 images. The images names are something like:

    a_traitor_among_us.jpg
    aldori_legacy_defender.jpg
    adal.jpg
    caretaker_heartwing.jpg

    Then I have a table in a MySQL database.
    I have to scan the directory and get the each name. Then I have to query the database.... and:

    A Traitor Among Us - Is the name in the cell. I have to automaticly search the directory and asociate a_traitor_among_us.jpg to that row.

    I can make an array with the file names. Then I can do a Foreach "row" and somehow see if that name is in that array with the file names. The thing is that the names are not exactly as the names in the database.

    Again:

    A Traitor Among Us (database name)
    a_traitor_among_us.jpg (file name)

    got it?

    So, sugestions?
     
    PET, Oct 18, 2008 IP
  2. Shoro

    Shoro Peon

    Messages:
    143
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #2
    
    if ($dh = opendir($directory)) {
        while (($file = readdir($dh)) !== false) {
    	    if ($file != "." && $file != "..") {
                if ($pos = strrpos($file,'.')) {
                    $name = substr($file,0,strrpos($file,'.'));
                }
                else {
                    $name = $file;
                }
                $name = strtoupper(str_replace('_',' ',$name));
                $sql = "UPDATE tablename SET filename = '$file' WHERE UPPER(name) = '$name'";
                mysql_query($sql);
            }
        }
        closedir($dh);
    }
    PHP:
     
    Shoro, Oct 18, 2008 IP
  3. PET

    PET Member

    Messages:
    86
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    43
    #3
    That's for the code.

    Question...

    What happens if the file name dosn't have a database entry. Let's just say it's an old forghotten file.

    Also, I have to get rid of the extension...

    And another problem is that some of the names have an ' in the name. Something like A'dal. The file name si Adal.jpg Guess I will have to manualy update them.

    Anyway, thanks for the code, I'm going to test it now.
     
    PET, Oct 18, 2008 IP
  4. PET

    PET Member

    Messages:
    86
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    43
    #4
    Later Edit:

    Wow. It works. Thanks. I would have never tought about that. Making the name UPPERCASE then check it up with the other uppercase.

    Now I have like 257 un-updated images because they had ' , " or - in their names.
     
    PET, Oct 18, 2008 IP
  5. Kyosys

    Kyosys Peon

    Messages:
    226
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #5
    well you can't expect the php script to know where to place ' or ,

    You could make an array of exceptions and loop it for that, but this solution seems pretty stupid altogether... why don't you just make a field for the filename?
     
    Kyosys, Oct 19, 2008 IP