Hello,

I have looked through the search results and have not found the answer to my question. I'm trying to find a way for users to see all the files inside a directory without using ftp client. Not sure if its possible. I'm not a web-developer.

Here's what I'm looking for:

1. User/Admin uploads file to folder using an ftp client. (ex. I upload a file to www.abc.com/download folder using ftp client)
2. User/Admin goes to abc.com/download and is able to see the uploaded file and other uploaded files inside the "download" folder

So basically, I was wondering if Drupal can automatically "create" a page that shows all the files inside that directory so users can easily pull files off there instead of having to type the exact filename to pull it out.

Alternatively, I was wondering if it was possible to make e.g. the download page to be "Drupal free" so when a user visits abc.com/download, the user sees the directory mode thingy.

Thank you in advance for all your help!

Comments

haeger’s picture

I'm curious about this too. It's quite similar to what I want to do.

daviestown’s picture

Yes. Bump. =)

daviestown’s picture

Anyone know a solution?

davedelong’s picture

The following code will list all the files (not directories) in directory of the current script. If you want it to list a different directory, you would need to change the $folder variable. Is that what you're looking for? Example

//get the directory of the current file:
$folder = dirname(__FILE__);
//initialize an empty array:
$files = array();
//if it's a folder:
if (is_dir($folder)) {
	//if we can open it:
	if ($handle = opendir($folder)) {
		//change directories into it:
		chdir($folder);
		//loop through all the files:
		while(($file = readdir($handle)) != false) {
			//if current file isn't a directory...
			if (!is_dir($file)) {
				//then add it to our array of found files:
				$files[] = $file;
			}
		}
	}
}
//output our list of files:
foreach ($files as $idx => $file) {
	echo '<a href="'.$file.'">'.$file.'</a><br>';
}

FYI, this a very crude way of doing it.

daviestown’s picture

Yes, I think this is what I am looking for. THANK YOU SO MUCH!! I was just wondering where I would paste this script?

If my download directory is www.abc.com/download/

Do I create a php file named index.php and place it there? Would Drupal override the php file and say "www.abc.com/download/ does not exist"?

Thanks again for your help =)

davedelong’s picture

Here's the way I would probably do it:
1. Make sure that you have the PHP Filter turned on. This allows you to create Drupal Pages that execute PHP snippets when run.
2. Figure out what the path to your site root is. This is easily done by creating a php file (temp.php) in your site root and putting this code in it:
echo dirname(__FILE__);
3. Create a new Drupal page, and use the following code (almost like the code above, just modified a bit):

$root = '/whatever/the/root/is/from/step/2';
//this is the location of your uploads folder (so at www. example.com/downloads)
$dir = '/downloads';
$folder = $root . $dir;
$files = array();
if (is_dir($folder)) {
	if ($handle = opendir($folder)) {
		while(($file = readdir($handle)) != false) {
			if (!is_dir($file)) {
				$files[] = $file;
			}
		}
		closedir($handle);
	}
}

echo '<h1>Listing files in ' . $dir . $file . '</h1>';
echo '<ul>';
foreach ($files as $idx => $file) {
	echo '<li><a href="'.$dir .'/'. $file.'">'.$file.'</a></li>';
}
echo '</ul>';

4. Save your page

This will give you a page like this one. So it works, but you don't get any snazzy features with it like download counting or whatnot.

Cheers,

Dave

daviestown’s picture

Awesome. You're my website lifesaver!! lol.

daviestown’s picture

My php is enabled. I created a new page, pasted the code, entered my $root and $dir... and then posted the page.

There are files in the /x directory (the directory that I set it to). However, what I see is only this line:

Listing files in /x

Not sure why that is. Any ideas? Thanks in advance.

davedelong’s picture

Is the folder world readable?

daviestown’s picture

The CHMOD is .. 755

davedelong’s picture

daviestown contacted me off list, and after about 15 seconds we noticed that he was missing the initial slash on his root url. Once that was put in, everything was fixed.