Solved

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

Posted on 2011-09-20
6
304 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:NUKIT
ID: 36569953
What is the exact script that you are posting in your file?

0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 13

Accepted Solution

by:
NUKIT 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:NUKIT
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Read about how to approach blogging and about ways to do it right. Stand out from the crowd and let your knowledge be consumed by a large audience. This article aims to explain how your blog should look like,  the most important things to do while b…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now