Solved

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

Posted on 2011-09-20
6
306 Views
Last Modified: 2012-05-12
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.
0
Comment
Question by:rawcoder
  • 3
  • 2
6 Comments
 
LVL 4

Expert Comment

by:MattJellings
ID: 36569535
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

0
 

Author Comment

by:rawcoder
ID: 36569649
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?  
0
 
LVL 13

Expert Comment

by:Andrew Derse
ID: 36569953
What is the exact script that you are posting in your file?

0
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
LVL 13

Accepted Solution

by:
Andrew Derse earned 500 total points
ID: 36570002
I believe you would need to write this:

 
<?php 

if ($handle = opendir($_SERVER['DOCUMENT_ROOT']."/joomlafolder/")) {
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..") {
            echo "$file <br />";
        }
    }
    closedir($handle);
}

?>

Open in new window


joomlafolder being the directory that your site is installed in.  

If that still doesn't work, then you don't have full permissions to that directory for an anon user...
0
 
LVL 13

Expert Comment

by:Andrew Derse
ID: 36573305
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/"
0
 
LVL 4

Expert Comment

by:MattJellings
ID: 36579151
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.
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article discusses how to create an extensible mechanism for linked drop downs.
FAQ pages provide a simple way for you to supply and for customers to find answers to the most common questions about your company. Here are six reasons why your company website should have a FAQ page
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question