Jump to content

listing files from directory


leequalls

Recommended Posts

I am using the following code to list mp3 files from a directory and find out if there is a file for it in mysql if so not to include it in the list. The code below is listing each file twice I only would like them to be listed once.

 

 

if($_POST['submit'] != 'select') {
echo'Choose an mp3 to add information.<p><select name="file">';
$dir = "/uploads/";

$result = mysql_query("SELECT * FROM dj_pool") or die(mysql_error());
$num_rows = mysql_num_rows($result);

// Open a known directory, and proceed to read its contents
if (is_dir($dir)) {
    if ($dh = opendir($dir)) {
        while (($file = readdir($dh)) !== false) {
            $info = explode(".", $file);
            if($info[1] == "mp3") {
            $i = 0; 
            while ($i < $num_rows)
              {
        $file2 = mysql_result($result,$i,mp3);
                if ($file != $file2) { echo "<option value='$info[0]'>$info[0]</option>"; }
            $i++; 
              }
                              }
        }closedir($dh);}
        
    }

print "</select><p><input type=submit value=select name=submit>";

 

Link to comment
Share on other sites

First off, you should use glob() to get the mp3 file list. Then do a query to get the ones not in the db. Then

 

Not tested, so there may be some syntax errors

<?php

$dir = "/uploads/";
$mp3FileList = glob("{$dir}*.mp3");

$query = "SELECT filename FROM dj_pool";
$result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_assoc($result))
{
    if(in_array($row['path'], $mp3FileList))
    {
        unset($mp3FileList[array_search($row['path'], $mp3FileList)]);
    }
}

print_r($mp3FileList);

?>

Link to comment
Share on other sites

ok so here is the out put

 

Array ( [0] => /home/user/public_html/en/djpanel/uploads/Dukes Nightmare - Is There Love In This House (Radio Edit).mp3 [1] => /home/user/public_html/en/djpanel/uploads/Dukes Nightmare - Losing You (Radio Edit).mp3 [2] => /home/user/public_html/en/djpanel/uploads/For You (Radio Edit).mp3 [3] => /home/user/public_html/en/djpanel/uploads/Gist The Essence f. Gasmine Sullivan - Holding You Down (Going In Circles) (Radio Edit).mp3 [4] => /home/user/public_html/en/djpanel/uploads/J-Storm f. StreetCorna - Freakshow (Radio Edit).mp3 [5] => /home/user/public_html/en/djpanel/uploads/SubSkript - Blaxploitation (Radio Edit).mp3 [6] => /home/user/public_html/en/djpanel/uploads/SubSkript - Lyracize (Radio Edit).mp3 [7] => /home/user/public_html/en/djpanel/uploads/SubSkript - Take It To The Top (Radio Edit).mp3 )

 

 

i need /home/user/public_html/en/djpanel/uploads/ to be removed and need to have each file in a drop down menu

 

 echo "<option value='$mp3File'>$mp3File</option>";

 

Link to comment
Share on other sites

<?php

$dir = "/uploads/";
$mp3FileList = glob("{$dir}*.mp3");

$query = "SELECT filename FROM dj_pool";
$result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_assoc($result))
{
    if(in_array($row['path'], $mp3FileList))
    {
        unset($mp3FileList[array_search($row['path'], $mp3FileList)]);
    }
}

echo '<select name="mp3">';
foreach ($mp3FileList as $file)
{
    $file = basename($file);
    echo '<option value="' . $file . '">' . $file . '</option>';
}
echo '</select>';

?>

That should print it as a dropdown with only the filename

Link to comment
Share on other sites

yes it is listing the files in the directory ok there are 2 db files listing 2 mp3 files the 2 files in the db are not being removed from the drop down menu.

 

output: (i have removed the .mp3 after the script was done)

 

<option value="Dukes Nightmare - Is There Love In This House (Radio Edit)">Dukes Nightmare - Is There Love In This House (Radio Edit)</option><option value="Dukes Nightmare - Losing You (Radio Edit)">Dukes Nightmare - Losing You (Radio Edit)</option><option value="For You (Radio Edit)">For You (Radio Edit)</option><option value="Gist The Essence f">Gist The Essence f</option><option value="J-Storm f">J-Storm f</option><option value="SubSkript  - Blaxploitation (Radio Edit)">SubSkript  - Blaxploitation (Radio Edit)</option><option value="SubSkript - Lyracize (Radio Edit)">SubSkript - Lyracize (Radio Edit)</option><option value="SubSkript - Take It To The Top (Radio Edit)">SubSkript - Take It To The Top (Radio Edit)</option>

 

SubSkript - Take It To The Top (Radio Edit)

SubSkript - Lyracize (Radio Edit)

 

should not be listed as they are in the mysql db

 

 

Link to comment
Share on other sites

alright, try this:


<?php

$dir = "/uploads/";
$mp3FileList = glob("{$dir}*.mp3");
array_walk($mp3FileList, 'basename');

$query = "SELECT filename FROM dj_pool";
$result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_assoc($result))
{
    if(in_array($row['path'], $mp3FileList))
    {
        unset($mp3FileList[array_search($row['path'], $mp3FileList)]);
    }
}

echo '<select name="mp3">';
foreach ($mp3FileList as $file)
{
    echo '<option value="' . $file . '">' . $file . '</option>';
}
echo '</select>';

?>

Link to comment
Share on other sites

new out put however the files are still listed

<option value="/home/party/public_html/en/djpanel/uploads/Dukes Nightmare - Is There Love In This House (Radio Edit)">/home/party/public_html/en/djpanel/uploads/Dukes Nightmare - Is There Love In This House (Radio Edit)</option><option value="/home/party/public_html/en/djpanel/uploads/Dukes Nightmare - Losing You (Radio Edit)">/home/party/public_html/en/djpanel/uploads/Dukes Nightmare - Losing You (Radio Edit)</option><option value="/home/party/public_html/en/djpanel/uploads/For You (Radio Edit)">/home/party/public_html/en/djpanel/uploads/For You (Radio Edit)</option><option value="/home/party/public_html/en/djpanel/uploads/Gist The Essence f">/home/party/public_html/en/djpanel/uploads/Gist The Essence f</option><option value="/home/party/public_html/en/djpanel/uploads/J-Storm f">/home/party/public_html/en/djpanel/uploads/J-Storm f</option><option value="/home/party/public_html/en/djpanel/uploads/SubSkript  - Blaxploitation (Radio Edit)">/home/party/public_html/en/djpanel/uploads/SubSkript  - Blaxploitation (Radio Edit)</option><option value="/home/party/public_html/en/djpanel/uploads/SubSkript - Lyracize (Radio Edit)">/home/party/public_html/en/djpanel/uploads/SubSkript - Lyracize (Radio Edit)</option><option value="/home/party/public_html/en/djpanel/uploads/SubSkript - Take It To The Top (Radio Edit)">/home/party/public_html/en/djpanel/uploads/SubSkript - Take It To The Top (Radio Edit)</option>

 

Link to comment
Share on other sites

<?php

$dir = "/uploads/";
$mp3FileList = glob("{$dir}*.mp3");
array_walk($mp3FileList, 'basename');

$query = "SELECT filename FROM dj_pool";
$result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_assoc($result))
{
    if(in_array($row['path'], $mp3FileList))
    {
        unset($mp3FileList[array_search($row['path'], $mp3FileList)]);
    }
    else { echo "File not Found in List: " . $row['path'] . "<br />"; }
}

echo '<select name="mp3">';
foreach ($mp3FileList as $file)
{
    echo '<option value="' . $file . '">' . $file . '</option>\n';
}
echo '</select>';

?>

Give that a try and see what the output is. There might be something wrong with $row['path'] or how its checking $mp3FileList.

Link to comment
Share on other sites

<select name="file">File not Found in List: <br />File not Found in List: <br /><option value="/home/user/public_html/en/djpanel/uploads/Dukes Nightmare - Is There Love In This House (Radio Edit)">/home/user/public_html/en/djpanel/uploads/Dukes Nightmare - Is There Love In This House (Radio Edit)</option><option value="/home/user/public_html/en/djpanel/uploads/Dukes Nightmare - Losing You (Radio Edit)">/home/user/public_html/en/djpanel/uploads/Dukes Nightmare - Losing You (Radio Edit)</option><option value="/home/user/public_html/en/djpanel/uploads/For You (Radio Edit)">/home/user/public_html/en/djpanel/uploads/For You (Radio Edit)</option><option value="/home/user/public_html/en/djpanel/uploads/Gist The Essence f">/home/user/public_html/en/djpanel/uploads/Gist The Essence f</option><option value="/home/user/public_html/en/djpanel/uploads/J-Storm f">/home/party/public_html/en/djpanel/uploads/J-Storm f</option><option value="/home/user/public_html/en/djpanel/uploads/SubSkript  - Blaxploitation (Radio Edit)">/home/user/public_html/en/djpanel/uploads/SubSkript  - Blaxploitation (Radio Edit)</option><option value="/home/user/public_html/en/djpanel/uploads/SubSkript - Lyracize (Radio Edit)">/home/user/public_html/en/djpanel/uploads/SubSkript - Lyracize (Radio Edit)</option><option value="/home/user/public_html/en/djpanel/uploads/SubSkript - Take It To The Top (Radio Edit)">/home/user/public_html/en/djpanel/uploads/SubSkript - Take It To The Top (Radio Edit)</option></select>

Link to comment
Share on other sites

i think the problem maybe that the file path is not included in the database file it only list the mp3 file.

 

Um, yeah. We cannot see into your database or filesystem and know what the values are. One of the previous scripts used basename() to strip off the path information from the values returned by glob() but apparently those values still do not match what is in your database. Do the values in the database include the ".mp3"? Please show what those values look like and we can help modify the glob() results accordingly.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.