leequalls Posted August 6, 2010 Share Posted August 6, 2010 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>"; Quote Link to comment https://forums.phpfreaks.com/topic/210022-listing-files-from-directory/ Share on other sites More sharing options...
Psycho Posted August 7, 2010 Share Posted August 7, 2010 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/210022-listing-files-from-directory/#findComment-1096140 Share on other sites More sharing options...
leequalls Posted August 7, 2010 Author Share Posted August 7, 2010 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>"; Quote Link to comment https://forums.phpfreaks.com/topic/210022-listing-files-from-directory/#findComment-1096144 Share on other sites More sharing options...
Tazerenix Posted August 7, 2010 Share Posted August 7, 2010 <?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 Quote Link to comment https://forums.phpfreaks.com/topic/210022-listing-files-from-directory/#findComment-1096149 Share on other sites More sharing options...
leequalls Posted August 7, 2010 Author Share Posted August 7, 2010 it worked you rock thanks Quote Link to comment https://forums.phpfreaks.com/topic/210022-listing-files-from-directory/#findComment-1096152 Share on other sites More sharing options...
leequalls Posted August 7, 2010 Author Share Posted August 7, 2010 forgot one thing i do not want the file to be removed from the directory. I just don't want it to be listed in the drop down menu. Quote Link to comment https://forums.phpfreaks.com/topic/210022-listing-files-from-directory/#findComment-1096166 Share on other sites More sharing options...
Tazerenix Posted August 7, 2010 Share Posted August 7, 2010 is it being removed from the directory when you run this script? it shouldn't be Quote Link to comment https://forums.phpfreaks.com/topic/210022-listing-files-from-directory/#findComment-1096169 Share on other sites More sharing options...
leequalls Posted August 7, 2010 Author Share Posted August 7, 2010 actually it is not being removed from the directory however it is not being removed from the list either. sorry I mixed up unset with unlink Quote Link to comment https://forums.phpfreaks.com/topic/210022-listing-files-from-directory/#findComment-1096170 Share on other sites More sharing options...
Tazerenix Posted August 7, 2010 Share Posted August 7, 2010 So there are non .mp3 files in the list? what is the contents of the dj_pool.path field in the mysql? Quote Link to comment https://forums.phpfreaks.com/topic/210022-listing-files-from-directory/#findComment-1096172 Share on other sites More sharing options...
leequalls Posted August 7, 2010 Author Share Posted August 7, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/210022-listing-files-from-directory/#findComment-1096173 Share on other sites More sharing options...
Tazerenix Posted August 7, 2010 Share Posted August 7, 2010 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>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/210022-listing-files-from-directory/#findComment-1096174 Share on other sites More sharing options...
leequalls Posted August 7, 2010 Author Share Posted August 7, 2010 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> Quote Link to comment https://forums.phpfreaks.com/topic/210022-listing-files-from-directory/#findComment-1096175 Share on other sites More sharing options...
Tazerenix Posted August 7, 2010 Share Posted August 7, 2010 <?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. Quote Link to comment https://forums.phpfreaks.com/topic/210022-listing-files-from-directory/#findComment-1096176 Share on other sites More sharing options...
leequalls Posted August 7, 2010 Author Share Posted August 7, 2010 should path be a value? Quote Link to comment https://forums.phpfreaks.com/topic/210022-listing-files-from-directory/#findComment-1096179 Share on other sites More sharing options...
leequalls Posted August 7, 2010 Author Share Posted August 7, 2010 <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> Quote Link to comment https://forums.phpfreaks.com/topic/210022-listing-files-from-directory/#findComment-1096180 Share on other sites More sharing options...
Tazerenix Posted August 7, 2010 Share Posted August 7, 2010 $row['path'] should be the name of the file you wish to exclude from the list Quote Link to comment https://forums.phpfreaks.com/topic/210022-listing-files-from-directory/#findComment-1096189 Share on other sites More sharing options...
leequalls Posted August 7, 2010 Author Share Posted August 7, 2010 ok i am saying does path need to be changed or keep it as path Quote Link to comment https://forums.phpfreaks.com/topic/210022-listing-files-from-directory/#findComment-1096194 Share on other sites More sharing options...
Tazerenix Posted August 7, 2010 Share Posted August 7, 2010 that doesn't matter, as long as path in your mysql entry contains the name of the file which you wish to leave out of the list. Quote Link to comment https://forums.phpfreaks.com/topic/210022-listing-files-from-directory/#findComment-1096195 Share on other sites More sharing options...
leequalls Posted August 7, 2010 Author Share Posted August 7, 2010 is there another way to rout this its not working with the in_array function Quote Link to comment https://forums.phpfreaks.com/topic/210022-listing-files-from-directory/#findComment-1096197 Share on other sites More sharing options...
Tazerenix Posted August 7, 2010 Share Posted August 7, 2010 oh right, change $row['path'] to $row['filepath'] Quote Link to comment https://forums.phpfreaks.com/topic/210022-listing-files-from-directory/#findComment-1096198 Share on other sites More sharing options...
leequalls Posted August 7, 2010 Author Share Posted August 7, 2010 i think the problem maybe that the file path is not included in the database file it only list the mp3 file. Quote Link to comment https://forums.phpfreaks.com/topic/210022-listing-files-from-directory/#findComment-1096287 Share on other sites More sharing options...
Psycho Posted August 7, 2010 Share Posted August 7, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/210022-listing-files-from-directory/#findComment-1096336 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.