Link to home
Start Free TrialLog in
Avatar of rawcoder
rawcoder

asked on

How to list all files in a specific folder using php and hostgator server

I am building a joomla website and need to list all of the files in a specific folder off of the root "public_html" folder.  For example, if I need to list all of the files in, say, "public_html/testdir".  What path would I have to pass to opendir() for access.  The website is currently sitting on a hostgator server.
Avatar of Matt
Matt
Flag of United Kingdom of Great Britain and Northern Ireland image

Hi rawcoder,

Sorry, I'm not in a position to test the code below, however please give it a try and see if it achieves what you are looking to do

if ($handle = opendir($_SERVER['DOCUMENT_ROOT'] . '/testdir')) {
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..") {
            echo "$file\n";
        }
    }
    closedir($handle);
}

Open in new window

Avatar of rawcoder
rawcoder

ASKER

I tried that and received the following permission denied error:  "Warning: opendir(/home/hairupst/public_html) [function.opendir]: failed to open dir: Permission denied".  Is there anything special I need to do?  
What is the exact script that you are posting in your file?

ASKER CERTIFIED SOLUTION
Avatar of Carlos Llanos
Carlos Llanos
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Did any of the above help you?

To verify that it's a permission problem, you can alter the above scripts to drill further down into the directories.  I believe the images folder should be open to all users...

"/joomlafolder/images/"
Does the "public_html/testdir" folder exist?  If not create it.

Also, as NUKIT said please make sure you have permission to read the folder you are trying to access.