Solved

Read file names in directory and compare to table

Posted on 2013-01-24
3
217 Views
Last Modified: 2013-02-03
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.
0
Comment
Question by:tjyoung
3 Comments
 
LVL 42

Accepted Solution

by:
sedgwick earned 250 total points
ID: 38813732
check this code:

$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
$filenames= array();


if ($handle = opendir('/path/to/audio_files')) {
  while (false !== ($entry = readdir($handle))) {
        if ($entry != "." && $entry != "..") {
array_push($filenames, $entry);            
        }
    }
    closedir($handle);
}

$arrNames = join(',',$filenames);
$extensions = "mp3, wav"

$result = mysql_query("SELECT * FROM audio_table WHERE filename IN ($arrNames) and   SUBSTRING_INDEX(filename,'.',-1) in ($extensions) ");

if (!$result) {
    echo 'Could not run query: ' . mysql_error();
    exit;
}

echo 'Connected successfully';
mysql_close($link);

while($row = mysql_fetch_array($result))
  {
  echo $row['filename'];
  echo "<br />";
  }

Open in new window

0
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
ID: 38814128
When you refer to having a "table" I would understand that you mean you have a PHP array. You can use these functions to make the comparison.
http://php.net/manual/en/function.scandir.php
http://php.net/manual/en/function.array-intersect.php
0
 
LVL 9

Expert Comment

by:crazedsanity
ID: 38818822
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).
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Php pie charts 3 26
only allow numbers with preg match 4 26
myqsl update statement on phpMyAdmin 8 22
PHP preg_replace code convert to Delphi 14 35
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now