Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Sep 13, 2006, 12:04 PM   #1
satans_banjo
macrumors regular
 
Join Date: Sep 2005
Location: SE London
Send a message via AIM to satans_banjo Send a message via MSN to satans_banjo Send a message via Yahoo to satans_banjo
PHP Directory Spider

Hi

I'm making a website for my band and I'm trying to make it so that my media page can list all of the files in a certain directory that have a certain extension, and link to them. Is there any way of doing that in PHP?

Thanks

Banjo

EDIT: The website in question is http://www.theautonomy.co.uk/

Last edited by satans_banjo; Sep 13, 2006 at 12:42 PM.
satans_banjo is offline   0 Reply With Quote
Old Sep 13, 2006, 02:15 PM   #2
stu02
macrumors newbie
 
Join Date: Aug 2006
Location: Edinburgh, Scotland
Heres some slightly adapted code I was using for basically the same purpose.

PHP Code:
function get_files($directory$file_type) {
    
    
$files = array();
    
$hidden = array();

    if (
is_dir($directory)) {
        if (
$directory_handle opendir($directory)) {
            while ((
$file readdir($directory_handle)) !== false) {
                if (
substr($file01) == '.') {
                    
$hidden[] = $file;
                }
                else {
                    if (
strstr($file$file_type)) {
                        
$files[] = $file;
                    }
                }
            }
            
closedir($directory_handle);
        }
    }
    else {
        echo 
"Specified directory doesn't exist.";
    }
    
    return 
$files;

It's not exactly perfect and I'm sure it can be improved in many ways but it'll get you started on what you want to achieve.

Something to take note off, is that the function determines the file type by searching the filename using the strstr() function which is case sensitive, so take care when your specifying the file type. Because its searching the filename for the file type you should probably always include the period when specifying the file type. For example you have a file named mympg.php and you said to look for .mpg files, the function won't return that file as being of the specified type.

Also it filters out hidden files and directories by looking for filenames that begin with a period. It's pretty basic and its only been tested on a server running Linux and I don't really know much about hidden files under different operating systems so use at your own risk.

Hope this helps.

Stu
stu02 is offline   0 Reply With Quote
Old Sep 13, 2006, 02:54 PM   #3
satans_banjo
Thread Starter
macrumors regular
 
Join Date: Sep 2005
Location: SE London
Send a message via AIM to satans_banjo Send a message via MSN to satans_banjo Send a message via Yahoo to satans_banjo
Thanks - I'm not an experienced user of PHP, so would you be able to elaborate on how I could use that? Right now I've got a 'controller' file containing that function, and an instance of that function containing the variables. I then include() that file on the page, but for output I just get the entire contents of the controller file. I've never used functions before in PHP - any chance you could help?

Thanks again for the help

EDIT: Okay, I think I've got it, just one thing - how do I define the 'file type' variable? When I use ".mp3" it doesn't show any results

Last edited by satans_banjo; Sep 13, 2006 at 03:00 PM.
satans_banjo is offline   0 Reply With Quote
Old Sep 13, 2006, 03:12 PM   #4
stu02
macrumors newbie
 
Join Date: Aug 2006
Location: Edinburgh, Scotland
Ok heres an example on how you would use the function.

PHP Code:
$files get_files('directory/path/''.mpg');

foreach (
$files as $file) {
    echo 
"<a href=\"directory/path/$file\">$file</a>";
    echo 
'<br />';

Which would produce something like this:

Code:
<a href="example/directory/file.mpg">file.mpg</a>
<a href="example/directory/file2.mpg">file2.mpg</a>
and so on depending on how many files there were.

You've got the controller part right but it would be easier just to call the function against a variable in the main file and include the file containing the code as you said but it doesn't really matter. If you keep it as you have it the now then all you have to do is get rid of the first line in the above code and put it in the controller file.

Hope this helps you further.

Stu
stu02 is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
[php] - php problems os x mountain lion 10.8.2 cakas6933 OS X 10.8 Mountain Lion 4 Jun 10, 2014 08:08 AM
copy a directory using bash - how to do it? error: is a directory (not copied). cupboy1 OS X 10.8 Mountain Lion 0 Mar 23, 2014 09:50 PM
Stupid Spider Questions Renzatic Community Discussion 56 Sep 23, 2013 06:44 PM
Listing directory for php gustavopi Mac Programming 2 Sep 23, 2013 02:31 PM
iPhone 5 to be Spider Designed? MacUserMichael iPhone 10 Jul 10, 2012 10:57 PM

Forum Jump

All times are GMT -5. The time now is 06:00 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC