Jump to content

Beginner with beginner question re: listing specific file type


trepaning

Recommended Posts

Hi all, I am new to PHP, really enjoying what I am learning. I went through Kevin Skoglund's tutorial and created a CMS http://www.trepaning.com/cms/public  I am now doing other courses, but have a nagging loose end from the CMS I am curious to remedy.

 

http://www.trepaning.com/cms/public/index.php?subject=2 shows a list of folders and files from a directory scan, not something part of the actual tutorial (neither was how to get it all online). What I am looking to do is have no file type except MP3 be listed (an HTML and a PHP file are also being listed), and I would like the folder names to not be hotlinked. With proper folder structure, the extra file types would not be an issue because they would not be present, but I am curious because of this set up how it could be remedied to ignore all but MP3 and hotlink only the MP3s while also presenting the tree structure.

 

I am usually frst to just work these through, as I really enjoy figuring out solutions, but this time around, I would be more interested in whatever variety of solutions can be presented to learn more that way, as this is not really important enough for me to take time away from the studies currently underway. Anyway, there's my question, and if anyone wants to send a quick answer, that'd be great. Regardless, I am enjoying PHP and mySQL far more than I would have expected and am seriously upset I did not pursue them years ago. Thanks!

Edited by trepaning
Link to comment
Share on other sites

To only show mp3 files you can run an $dot = strrpos($file, ".") on the file name to get the last occurrence of a dot then substr($file, $dot+1, strlen($file)) and test if its equal to mp3. Alternatively, you could explode using the dot and get the last item in the array, and test if that's equal to mp3. The rest is self explanatory.

 

With regards to keeping the directory structure: you need to know if an mp3 file is in a directory before you can display it. You could do this by scanning everything and creating an array of the directories and any MP3's they have in them. Then remove any directories that reference an empty array.

 

i.e.

 

array(
   'dir1' => array(
      'something.mp3'
   ),
   'dir2' => array()
...

 

After you've built your array representing a directory structure as above, cycle through it and remove anything referencing an empty array such as 'dir2'. Without scanning the directory once, its impossible to know if it contains an MP3. 

 

To stop linking the directories, just remove the hyperlink after testing if its a directory or not. (http://php.net/is_dir).

 

Just a couple of ideas off the top of my head.

Link to comment
Share on other sites

 

glob() would be a easier and faster way to get a list of files

$files = glob('directory/*.mp3');
print_r($files);

 

That's the function I couldn't remember and ended up omitting from my post. I'd definitely recommend this over most solutions.

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.