[Solved] Deleting MP3'S

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Addos
Forum Contributor
Posts: 305
Joined: Mon Jan 17, 2005 4:13 pm

[Solved] Deleting MP3'S

Post by Addos »

Hi, I hope it's okay to ask the following. I'm looking for a simply script that will show a list of mps.3 in a folder so that I can select to delete a specific mp3. Can anyon point me to one? I have been reading up but a lot are very complex for my level of PHP.
Thanks
Last edited by Addos on Wed Apr 02, 2008 11:52 am, edited 1 time in total.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Deleting MP3'S

Post by Chris Corbyn »

So maybe we need to start at the beginning ;) Can you write code to list the MP3's even if it doesn't "do" anything?
Addos
Forum Contributor
Posts: 305
Joined: Mon Jan 17, 2005 4:13 pm

Re: Deleting MP3'S

Post by Addos »

Yep! And this works okay for me. Thanks

Code: Select all

<?
    // Define the full path to the folder whose contents you want to list
    $path = "../../tutti_audio/";
    // Open the directory
    $dir_handle = @opendir($path) or die("Error opening $path");
    // Loop through the files
    while ($file = readdir($dir_handle)) {
      if($file == "." || $file == ".." || $file == "index.php" ) { continue; }
    
      echo "<a href=\"$file\">$file</a><br />";
    }
    // Close
    closedir($dir_handle);
?>
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Deleting MP3'S

Post by John Cartwright »

Code: Select all

echo '<a href="?delete='. urlencode($file) .'">Delete '.$file .'</a><br />";
 
Then someone on the page to detect whether a delete has been requested,

Code: Select all

 
 
$dir = $_SERVER['DOCUMENT_ROOT'] . '/files/mp3/';
$file = isset($_GET['delete']) ? basename(urldecode($_GET['delete'])) : false;
 
if ($file && file_exists($dir . $file)) {
   unlink($dir.$file);
}
A couple things to consider in the example I've provided,

#1 urlencode() the links filename since it may contain some invalid url characters (and urldecode when using it)
#2 check that the requested file exists in the correct folder using file_exists()
#3 apply basename() on the requested file to avoid users trying to pass invalid filepaths

I'm sure theres a couple things I forgot to consider
Addos
Forum Contributor
Posts: 305
Joined: Mon Jan 17, 2005 4:13 pm

Re: Deleting MP3'S

Post by Addos »

Thanks for all the help. Another little bit learned thank you!
B
Post Reply