Solved

How Do I Get Folder List

Posted on 2009-04-11
25
688 Views
Last Modified: 2013-12-12
Hello:
Using PHP, how can I get a folder list from my website. For example, I have a folders  "A", "B", "C" ... and these folders have subfolders under them.  I would like to present the top floder name ("A", "B" ...) to the user. Once the user clicks on the folder name, I would like to display all the subfolder under it.

This needs to be done so that the user can upload a file to the correct folder.

I realize that allowing the iploads are very risky, but this task will be available to only two user ids.

Thanks,
<<< Ali >>>
0
Comment
Question by:MohammadAliQureshi
  • 10
  • 10
  • 2
  • +1
25 Comments
 
LVL 15

Accepted Solution

by:
ncoo earned 300 total points
Comment Utility
Comment out the file bit to list only directories.

Set $dir to your default location.
<?php
 

if ($_GET['dir']) {

	$dir = $_GET['dir'].'/';

} else {

	$dir = "c:/";

}
 

if($handle = opendir($dir))

{

    while($file = readdir($handle))

    {

		if (is_dir($dir.$file)&&$file!='.'&&$file!='..') {

			echo '<a href="?dir='.urlencode($dir.$file).'">'.$file.'</a><br/>';

		} else if(is_file($dir.$file)) {

			echo $file.'<br/>';

		}

    }

    closedir($handle);

}
 

?>

Open in new window

0
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 200 total points
Comment Utility
You may be able to use scandir() to list files and folders.  (Good user notes on this page)
http://us3.php.net/manual/en/function.scandir.php

Also, glob() might be helpful.
http://us3.php.net/manual/en/function.glob.php

The process of choosing the target directory for the upload may present some challenges.  You might find that this is a good application for AJAX, since presumably the client would need to click through a selection of destination folders.  How to keep track of the selected folders on the server side - I would be interested to see your final code set.

An alternative might be to give each of the two users a folder, and let them be responsible for naming conventions that protect their files.

Can you tell us a little more about why you need to let the client choose a destination folder?  If we understand that, we may have some suggestions.

Thanks, ~Ray
0
 

Author Comment

by:MohammadAliQureshi
Comment Utility
Hello:

Thanks ncoo for your input ....
To answer Ray's question ....
The website is for educational institute. They have folders for different subjects and for different media. Let me explain. They have a folder called notes. Under this folder, they have subfolders for each subject (say physics). Now, If they want a notes about physics to be uploaded for students to see, they will choose notes --> physics.

Then, the user can click on notes tab and then physics tab to see all the pysics notes.

The same way they have audio lectures as well under audio folder Graphically, the folder are layed out as follows:
notes           Audio
   |                    |
   |_ Physics   |_ Physics
           |_ Files                |_ Audio files

The user has to select where the file belongs (Notes -> physics, notes -> Chemistry ...).

I hope that this long-winded expalnation is helpful.

Thanks,
<<< Ali >>>
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
@MohammadAliQureshi: This sounds almost like an intranet, of sorts.  If I were doing this, I would definitely look into using Google Docs!  They have done all the heavy lifting for us already.

Best of luck with it, ~Ray
0
 

Author Comment

by:MohammadAliQureshi
Comment Utility
Hello:

@Ray: The folders belong to an internet site.

Other alternative I was thinking is to use MySql database to store the documents. What do the experts and gurus of EE think about it?

Thanks,

<<< Ali >>>
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
"use MySql database to store the documents" - that depends on the number and size of the documents, as well as whether you need full-text indexing.  There are a lot of moving parts to an answer about this.  Here are some.  If the documents are very large, like photographic images or video, and have little or no indexable information they are poor candidates for a data base.  If they are text, they are much better candidates.  Large fields in a data base cause large issues with backups and with poorly thought-out queries like SELECT * FROM...  In those cases, you might store some of the identifying information in the DB and save a pointer to a directory in the file system (but not in the tree of WWW root).

If you need full text search capability and your server can handle the number and size of the documents, as well as the arrival rate of the queries, you may be better off using the DB to store the docs.

As with most things in SQL data bases, the key to performance is good indexing and well written queries.  Use EXPLAIN on every query to see what SQL is thinking about its processes.

HTH, ~Ray
0
 

Author Comment

by:MohammadAliQureshi
Comment Utility
Hello:

@Ray: Could you further explain what you meant by " ... (but not in the tree of WWW root)."

Thanks,
<<< Ali >>>
0
 
LVL 19

Expert Comment

by:NerdsOfTech
Comment Utility
Are you interested in a preloaded tree or an on-demand directory tree?
0
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 200 total points
Comment Utility
"but not in the tree of WWW root" - Yes, here is what I mean.

If you have a URL like www.mydomain.com, the web server looks in a "root" directory to find index.html or index.php (etc) and loads that has the home page.  Within that directory, you probably have other scripts to generate other web pages and you may have other directories like images/ or css/

Everything in the root directory and all the subdirectories can be accessed over the WWW with a URI -- essentially the domain name plus a path to the files and directories that are downstream from the home page.  On many servers you can list the images like this: www.mydomain.com/images/

Often this WWW root directory is called something like "public_html" and it resides in a higher-up directory, perhaps the directory of the client's account on the web server.

If you locate this "higher-up" directory and store your files in it, you can access these files from your PHP scripts with a statement like this:

include("../files/myfile.txt");

However there is no URI that can get to these files because the WWW server only allows access to files and directories downstream from the web root.  That adds a measure of security against accidental disclosure of the files.  Since only your WWW scripts can load the files and since your scripts can have knowledge of your client (via a login, etc) you have programmatic control over access to the files.

Does that make sense? ~Ray
0
 

Author Comment

by:MohammadAliQureshi
Comment Utility
@Ray --> Good explanation. Thank you ...

I checked and my website only has www.mydomain.com directory. It does not have any public_html". Also, I was able to get all the image files by entering www.mydomain.com/images (As mentioned in your email). Just wanted to see how the files will be displayed.

Now the question is Should I request my host to create public_html directory (Or create it my self) and move all the folder into that file? What about the source code? Should that be in public_html as well.

Sorry Ray, the above questions does not relate to the original question but are now become important since there is security involved and I completly missed this directory struture.

Again, I thank you for educating me and, hopefully, I will remember that for future.

P.S. I am working on displaying the folder page and ,once completed and tested, I will post the code.

Thanks,

<<< Ali >>>
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
Who is your host?  How do you look at your file structure -- do you have cPanel or some kind of FTP script available to you?
0
 

Author Comment

by:MohammadAliQureshi
Comment Utility
@Ray ...

Host = Ixwebhosting.com
FTP -> I use Coffee Cup Free FTP s/w ...

IXWebhostings also have login panel. I use it mostly for MySQL stuff or setting up email address kind of work ...

Thanks,
<<< Ali >>>
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 19

Expert Comment

by:NerdsOfTech
Comment Utility
when you are in FTP what happens if you go up a directory from your initial login point?

Thanks
0
 

Author Comment

by:MohammadAliQureshi
Comment Utility
@NerdsOfTech:

I FTP my files in a folder called domainname.com (There is where all of my folders are). On the top level, we have three folders
domainname.com (no WWW before the domain name!!)
logs
stats
Note that I am able to upload / download files from the top level as well. I hope this answers your question

<<< Ali >>>
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
Try adding a directory beside "logs" and "stats" - name it "base" and see if you can put files into that directory.  You should be able to FTP a file named "myfile.txt" into that directory and include it in your PHP script with something like this:

include("../base/myfile.txt");

Let us know how that worked out, ~Ray
0
 

Author Comment

by:MohammadAliQureshi
Comment Utility
@Ray: I was able to add the folder as suggested by you. I was also able to FTP a file to this folder.

Now, I will copy all of my folders under this folder. The only question is --> DO I MOVE THE php code under this folder as well or leave it under domainname.com folder?

<<< Thanks >>>
Ali
0
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 200 total points
Comment Utility
OK, we are making good progress!

Leave your PHP code in the "domainname.com" folder.

Put a file named 'test.php' in the newly created "base" folder with the information in the code snippet.

Put a file named "test.php" in the "domainname.com" folder with this information:

<?php
error_reporting(E_ALL);
require_once('../base/test.php');
echo "<br/>Hello From A Script Located INSIDE the WWW Root\n";
 ?>

Then use the WWW to go to your domain.com/test.php and see what you get.
<?php 

error_reporting(E_ALL);

echo "<br/>Hello From A Script Located Outside the WWW Root\n";

?>

Open in new window

0
 

Author Comment

by:MohammadAliQureshi
Comment Utility
@Ray:

www.domainname.com/testing.php echos the following:
Hello From A Script Located Outside the WWW Root
Hello From A Script Located INSIDE the WWW Root

This is what we expected. Correct?

Thanks,
<<< Ali >>>
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
Yes, that means the structure is correct.  Excellent news!
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
@MohammadAliQureshi: The security guard is telling me that he has to close for the night.  I will check back tomorrow.  Best regards, ~Ray
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
Well, the initial question was, "how can I get a folder list from my website" and I answered that!  But it seemed to take on a life of its own afterward.  
0
 
LVL 15

Expert Comment

by:ncoo
Comment Utility
I also answered the question (#a24125092)  "how can I get a folder list from my website" but didn't add further comments.
0
 

Author Comment

by:MohammadAliQureshi
Comment Utility
@angelIII:

Your are absolutely correct. I must be responsible for my open questions .... My appologies ....

Right after this, I will be awarding points and closing the question (I have another one ... Stay tuned ..)

Thanks,
<<< Ali >>>
0
 

Author Closing Comment

by:MohammadAliQureshi
Comment Utility
Hello:
Sorry for the delay ... Thanks for your help ...

<<< Ali >>>
0

Featured Post

Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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 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 …

771 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

10 Experts available now in Live!

Get 1:1 Help Now