• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 703
  • Last Modified:

How Do I Get Folder List

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
MohammadAliQureshi
Asked:
MohammadAliQureshi
  • 10
  • 10
  • 2
  • +1
4 Solutions
 
ncooCommented:
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
 
Ray PaseurCommented:
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
 
MohammadAliQureshiAuthor Commented:
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Ray PaseurCommented:
@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
 
MohammadAliQureshiAuthor Commented:
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
 
Ray PaseurCommented:
"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
 
MohammadAliQureshiAuthor Commented:
Hello:

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

Thanks,
<<< Ali >>>
0
 
NerdsOfTechTechnology ScientistCommented:
Are you interested in a preloaded tree or an on-demand directory tree?
0
 
Ray PaseurCommented:
"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
 
MohammadAliQureshiAuthor Commented:
@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
 
Ray PaseurCommented:
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
 
MohammadAliQureshiAuthor Commented:
@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
 
NerdsOfTechTechnology ScientistCommented:
when you are in FTP what happens if you go up a directory from your initial login point?

Thanks
0
 
MohammadAliQureshiAuthor Commented:
@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
 
Ray PaseurCommented:
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
 
MohammadAliQureshiAuthor Commented:
@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
 
Ray PaseurCommented:
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
 
MohammadAliQureshiAuthor Commented:
@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
 
Ray PaseurCommented:
Yes, that means the structure is correct.  Excellent news!
0
 
Ray PaseurCommented:
@MohammadAliQureshi: The security guard is telling me that he has to close for the night.  I will check back tomorrow.  Best regards, ~Ray
0
 
Ray PaseurCommented:
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
 
ncooCommented:
I also answered the question (#a24125092)  "how can I get a folder list from my website" but didn't add further comments.
0
 
MohammadAliQureshiAuthor Commented:
@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
 
MohammadAliQureshiAuthor Commented:
Hello:
Sorry for the delay ... Thanks for your help ...

<<< Ali >>>
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 10
  • 10
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now