Ojay Posted March 6, 2007 Share Posted March 6, 2007 Hey everyone... this is my first post, WOOOOO! It's good to be here! I am here because my basic knowledge of PHP is restricting me... All i want to create is a script that displays a directory of mp3's as dynamically generated links. I can build a form to upload the mp3's to a directory, i just can't find/figure out a script that will display them. If any one knows a tutorial, script or is willing to show me how to achieve such a thing i will drink a beer and toast it in honour of their name. thanks for any help! xxx Quote Link to comment Share on other sites More sharing options...
s0c0 Posted March 6, 2007 Share Posted March 6, 2007 One way to do what you are talking about is store a file link to the mp3 in a mysql database. Then you query the database and display the rows returned on your site as links. This is fairly simple and is something I have done in the past. The other option, which I have NOT done, is to use some of the directory functions. In particular you'd be interested in the scandir() function. Check out the manual here: http://us2.php.net/dir. I'm guessing the scandir function returns an array of all files in a given directory and then you can use a foreach loop to iterate through them. This is probably the easiest of the two solutions, but I'm not sure which is the best. Quote Link to comment Share on other sites More sharing options...
s0c0 Posted March 6, 2007 Share Posted March 6, 2007 Not to hijack your thread, but if any more experience PHP programmers read this which solution do you think I provided is the "better" of the two (or other solutions as well). Basically I'm asking: Does a query / echo the returned rows from a db, result in faster response time and less load on the server, than the scandir() method? Please advise. Quote Link to comment Share on other sites More sharing options...
HalfBrian Posted March 6, 2007 Share Posted March 6, 2007 I made this script a while ago for my friend, it worked for him. It should only show .mp3 files (the sample also shows .zip files) http://www.realmb.com/dir/ (the files are in dir.zip) Quote Link to comment Share on other sites More sharing options...
chronister Posted March 6, 2007 Share Posted March 6, 2007 here is some code I use to dynamically show all files in a directory (I did not write this, and don't remember where I got it). You should be able to modify it to show only .mp3 files pretty easy. <?php $cwd=getcwd(); $path = "$cwd"; $dir_handle = @opendir($path) or die("Unable to open $path"); while ($file = readdir($dir_handle)) { if($file!="." && $file!=".." && $file !="index.php") echo "<li><a href='$file'>$file</a></li>"; } //closing the directory closedir($dir_handle); ?> Quote Link to comment Share on other sites More sharing options...
bob_the _builder Posted March 6, 2007 Share Posted March 6, 2007 Hi, If you are using a form to upload the MP3 I would store the filename + other data in mysql database, this will be the better all round option. Easyer and also searches etc can be implimented easily. Good Luck Quote Link to comment Share on other sites More sharing options...
legohead6 Posted March 6, 2007 Share Posted March 6, 2007 here is a script i have for a fileing service, it links to all the files in a specific folder.. i dunno how much of the code is good for you, but there is alot of good stuff in here... if(isset($_POST['submit2'])){ $first=$_POST['new']; $end=$_POST['end']; $name="$first.$end"; $types = array('gif', 'jpeg', 'jpg', 'pjpeg','bmp','txt','doc','php','html','mpg','css','mpeg','png','xls'); $parts = explode('.', $name); $key = count($parts) - 1; $ext = strtolower($parts[$key]); if (in_array($ext, $types)) { $page = fopen("userfolders/$folder/$name", 'x+'); $content=" "; fwrite($page, $content); $cpage="INSERT INTO files VALUES('','$name','$end','$user','0')"; $result = mysql_query($cpage) or die ("Error in query: $cpage. ".mysql_error()); }else{ echo "Sorry Your File is not supported, Please The administrator"; } if(isset($_POST['upload'])){ $dest="userfolders/$folder/".$_FILES['ph']['name']; copy($_FILES['ph']['tmp_name'],"$dest"); $filer=$_FILES['ph']['name']; $type=$_FILES['ph']['type']; $cpage="INSERT INTO files VALUES('','$filer','$type','$user','0')"; $result = mysql_query($cpage) or die ("Error in query: $cpage. ".mysql_error()); print "File has been successfully uploaded!<br>"; } } $dir = "userfolders/$folder/"; $dh = opendir($dir); while (false !== ($filename = readdir($dh))) { $files[] = $filename; } $delete = array(); echo "<table align=center cellspacing=0 cellpadding=5>"; $bg = '#eeeeee'; echo "<tr><td>File</td><td>Download</td><td>Delete</td><td>Public</td></tr><form method=POST>"; $re = array_search('..', $files); $rem = array_search('.', $files); unset($files[$rem], $files[$re]); $totalp = count($files); foreach($files as $id => $file){ $file1 = str_replace(" ", "_", "$file"); $file2 = str_replace(".", ".", "$file1"); $nfile = base64_encode("$file1"); echo '<tr>'; echo "<td align=left><a href=page.php?f=$nfile>$file2</a></td><td><a href=download.php?f=$nfile><b><font color=black>Download</font></b></a></td><td><input type=checkbox name=delete[] value=$file></td><td><a href=public.php?f=$nfile>Make Public</a></td></tr>"; echo '</tr>'; } echo "<tr><td>Total Files: $totalp</td><td></td><td><input type=submit name=subm value=Delete></td></tr></table></form>"; if(isset($_POST['subm'])){ $delete=$_POST['delete']; foreach($delete as $rfiles){ @unlink("userfolders/$folder/$rfiles") or die ("The file doesnt exist!"); $rem="DELETE FROM files WHERE Name='$rfiles'"; $result = mysql_query($rem) or die ("Error in query: $rem. ".mysql_error()); } } } Quote Link to comment Share on other sites More sharing options...
Ojay Posted March 6, 2007 Author Share Posted March 6, 2007 OH MY LORD!!!! Thanks everyone... there's more than enough for me to be getting my teeth into Although there's going to be a lot of toasting... i may end up rather wonkey Thanks again, i'll be back once i've figured smthg out... cheers! Quote Link to comment 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.