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?
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:
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.
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.
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?