Link to home
Start Free TrialLog in
Avatar of tjyoung
tjyoung

asked on

Read file names in directory and compare to table

Hi,
In my application a user may upload a series of audio files and I need to ensure before they are put to use all of them are actually in the directory and are named appropriately. The user may have additional files in the directory which doesn't matter. I just need to ensure the required ones are there. Also the user may upload one of 2 different formats ie: .wav or .mp3

So if I had a table with the required names in my db ie: 'audio_table'
In that table I have the following names of audio files:
hello
goodbye
thanks

Any idea of the code required to check each filename in the directory and compare it to the filenames in the table, see if there is a match and see that the file extension in the directory is either .mp3 or .wav

Kind of like:
foreach audio file in the db table, read directory file names, find if match, check if .wav or .mp3
if all there return true
if missing return missing filenames array so I can echo back missing files

Thanks for any help.
ASKER CERTIFIED SOLUTION
Avatar of Meir Rivkin
Meir Rivkin
Flag of Israel image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
It might be useful to store a hash (e.g. MD5) of the file in the database to ensure it hasn't been tampered with.  Once you've performed the steps Ray_Paseur gave, I'd add a "foreach" loop to test the file for their MD5 value, and do something intelligent if there's a mismatch (e.g. delete the file).