Jump to content

Recommended Posts

How do I read a list of files in a directory with php?

 

example if I want to see all the excel, pdfs, etc in http://testsite.com/downloads/

 

I am creating a backend for a dealer downloads page. I want to be able to select the file and store the url to it in a database. Then, display the download files on our downloads page.

 

Thanks,

 

Jake

Link to comment
https://forums.phpfreaks.com/topic/139733-selecting-files-in-a-directory/
Share on other sites

Is it possible to list files in a directory across the web rather than on the local server. I am having trouble getting the code below to work.

 

code:

<?php
/*
ABOUT:
This snippet will list all the files in the directory of your
choice, truncate the file extension, capitalize the first letter and
put it all in a drop down menu. The script will not list subdirectories so 
all you see are the files in your directory.

Feel free to use or modify to your liking.

USAGE:
Change the $dirpath variable the directory you want to list.

AUTHOR:
Written by shockingbird
Visit www.glomer.net for more information
tate at tatenations dot com for any questions
*/
echo "<form>";
//Looks into the directory and returns the files, no subdirectories
echo "<select name='yourfiles'>";
//The path to the style directory
$dirpath = "http://mysite.com/downloads/";
$dh = opendir($dirpath);
while (false !== ($file = readdir($dh))) {
//Don't list subdirectories
if (!is_dir("$dirpath/$file")) {
//Truncate the file extension and capitalize the first letter
echo "<option value='$file'>" . htmlspecialchars(ucfirst(preg_replace('/\..*$/', '', $file))) . '</option>';
}
}
closedir($dh); 
//Close Select
echo "</select>";
echo "</form>";
?>

 

error:

PHP Warning: opendir(http://mysite.com/downloads/) [function.opendir]: failed to open dir: not implemented in C:\Inetpub\Websites\mysite.com\scan.php on line 25 PHP Warning: readdir(): supplied argument is not a valid Directory resource in C:\Inetpub\Websites\mysite.com\scan.php on line 26 PHP Warning: closedir(): supplied argument is not a valid Directory resource in C:\Inetpub\Websites\mysite.com\scan.php on line 33

 

By setup, do you mean permissions in iis?

 

Almost every website hides their directories cause the world does not need to see it. However, if their webserver is setup to show directories you will get an html page list of the directories.

 

If they do not have that, you would have to have FTP permission to the site to get all the filenames etc. If they do not have it in a list somewhere, you cannot get their directory listing.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.