PDA

View Full Version : Show folder files and content with automatic list??


lorne17
08-08-2008, 08:52 PM
Hello there,

I am trying to get a page where our clients can login and view the password protected folder (the one they login to) and download files to their own machines.

I have successfully completed this task. BUT, I don't like the way the Folder index page looks. Is there a way to create an index page that will automatically list all files and content in the folder the client just logged into?

Also once logged in, can I get a File Upload form so the client can upload their own files to that folder?

Take a look at the attached photos. What I'm getting is the typical white background, blue text and file list/info. But I don't want that. I want it to match our site and look good. Like the second picture that shows the "Client Login" page. A similar list like that where clients can login and download files. Can anyone help? I am using Dreamweaver cs3.

Thanks,

Lorne

edbr
08-09-2008, 12:21 AM
yes with php. i gave a snippet a while ago here but lost it. i havent used this but try this. drop it in a class and you can style as you want<?

/**
* Change the path to your folder.
*
* This must be the full path from the root of your
* web space. If you're not sure what it is, ask your host.
*
* Name this file index.php and place in the directory.
*/

// Define the full path to your folder from root
$path = "full path to folder";

// Open the folder
$dir_handle = @opendir($path) or die("Unable to open $path");

// Loop through the files
while ($file = readdir($dir_handle)) {

if($file == "." || $file == ".." || $file == "index.php" )

continue;
echo "<a href=\"$file\">$file</a><br>&gt;";

}

// Close
closedir($dir_handle);

?>

lorne17
08-11-2008, 10:42 PM
I'm really new with PHP. Is there anything special I need to do to input this into my page? I inserted this code into my index.php page which is located in the folder that I want to list the contents. When I went to preview it, it said "Unable to open /www/htdocs/client login files/test/"

Why is this? I can't figure out what I'm doing wrong? Is it due to the spaces in the "client login files" for the folder name? I tried the 20% inbetween for spaces...but still no dice.

I found this coding from another forum, and this didn't work either. What am I missing? Thanks,

Lorne

<?php

function getDirectoryListing($dirname, $sortorder = "a", $show_subdirs = 0, $show_subdirfiles = 0, $exts = "", $ext_save = 1) {
// This function will return an array with filenames based on the criteria you can set in the variables
// @sortorder : a for ascending (the standard) or d for descending (you can use the "r" for reverse as well, works the same)
// @show_subdirs : 0 for NO, 1 for YES - meaning it will show the names of subdirectories if there are any
// Logically subdirnames will not be checked for the required extentions
// @show_subdirfiles : 0 for NO, 1 for YES - meaning it will show files from the subdirs
// Files from subdirs will be prefixed with the subdir name and checked for the required extentions.
// @exts can be either a string or an array, if not passed to the function, then the default will be a check for common image files
// If exts is set to "all" then all extentions are allowed
// @ext_save : 1 for YES, 0 for NO - meaning it will filter out system files or not (such as .htaccess)

if (!$exts || empty($exts) || $exts == "") {
$exts = array("jpg", "gif", "jpeg", "png");
}
if ($handle = opendir($dirname)) {
$filelist = array();
while (false !== ($file = readdir($handle))) {

// Filter out higher directory references
if ($file != "." && $file != "..") {
// Only look at directories or files, filter out symbolic links
if ( filetype ($dirname."/".$file) != "link") {
// If it's a file, check against valid extentions and add to the list
if ( filetype ($dirname."/".$file) == "file" ) {
if (checkFileExtention($file, $exts, $ext_save)) {
$filelist[] = $file;
}
}
// If it's a directory and either subdirs should be listed or files from subdirs add relevant names to the list
else if ( filetype ($dirname."/".$file) == "dir" && ($show_subdirs == 1 || $show_subdirfiles == 1)) {
if ($show_subdirs == 1) {
$filelist[] = $file;
}
if ($show_subdirfiles == 1) {
$subdirname = $file;
$subdirfilelist = getDirectoryListing($dirname."/".$subdirname."/", $sortorder, $show_subdirs, $show_subdirfiles, $exts, $ext_save);
for ($i = 0 ; $i < count($subdirfilelist) ; $i++) {
$subdirfilelist[$i] = $subdirname."/".$subdirfilelist[$i];
}
$filelist = array_merge($filelist, $subdirfilelist);
}

}

}
}
}
closedir($handle);

// Sort the results
if (count($filelist) > 1) {
natcasesort($filelist);
if ($sortorder == "d" || $sortorder == "r" ) {
$filelist = array_reverse($filelist, TRUE);
}
}
return $filelist;
}
else {
return false;
}
}

function checkFileExtention($filename, $exts, $ext_save = 1) {
$passed = FALSE;
if ($ext_save == 1) {
if (preg_match("/^\./", $filename)) {
return $passed;
}
}
if ($exts == "all") {
$passed = TRUE;
return $passed;
}
if (is_string($exts)) {
if (eregi("\.". $exts ."$", $filename)) {
$passed = TRUE;
return $passed;
}
} else if (is_array($exts)) {
foreach ($exts as $theExt) {
if (eregi("\.". $theExt ."$", $filename)) {
$passed = TRUE;
return $passed;
}
}
}
return $passed;
}
?>

edbr
08-12-2008, 01:20 AM
i have not tried it , as i said i lost my original snippet but i posted it here somewhere, it does work I know. ill try this one later

edbr
08-12-2008, 04:38 AM
k thats a function and you have to call it, however its not what you want i think
try this
<?php
if (http://www.php.net/if) ($handle = opendir (http://www.php.net/opendir)('.')) {
while (http://www.php.net/while) (false (http://www.php.net/false) !== ($file (http://www.php.net/file) = readdir (http://www.php.net/readdir)($handle)))
{
if (http://www.php.net/if) ($file (http://www.php.net/file) != "." && $file (http://www.php.net/file) != "..")
{
$thelist .= '<a href="'.$file (http://www.php.net/file).'">'.$file (http://www.php.net/file).'</a>';
}
}
closedir (http://www.php.net/closedir)($handle);
}
?>
<p>List of files:</p>
<p><?=$thelist?></p>

edbr
08-12-2008, 04:44 AM
and if you changed
$thelist .= '<a href="'.$file.'">'.$file.'</a>;
to
$thelist .= '<a href="'.$file.'">'.$file.'</a>'.'<br>';
it will list files in a new line

edbr
08-12-2008, 05:34 AM
k getting into this now :)
playing with code example from w3 schools this lets you list files in a specific folder, in this case images so change to suit, with links to the files

<?php
//Open images directory
$dir = opendir("images");

//List files in images directory
while (($file = readdir($dir)) !== false)
{
if ($file != "." && $file != "..")
{
echo '<a href="'.$file.'">' . $file . '</a>'."<br />";
}
}
closedir($dir);
?>

lorne17
08-13-2008, 05:53 PM
Ok what am I missing here...I tried your last post:

<?php
//Open images directory
$dir = opendir("test");

//List files in images directory
while (($file = readdir($dir)) !== false)
{
if ($file != "." && $file != "..")
{
echo '<a href="'.$file.'">' . $file . '</a>'."<br />";
}
}
closedir($dir);
?>

I changed the images folder to "test" because that's what my folder I want listed is called.

Am I missing something? The attached image shows what the page shows when the link to index.php was clicked.

Thanks for your help

Lorne

lorne17
08-13-2008, 06:16 PM
Ok so I tried this code and it worked!!

<?php
if ($handle = opendir('.')) {
while (false !== ($file = readdir($handle)))
{
if ($file != "." && $file != "..")
{
$thelist .= '<a href="'.$file.'">'.$file.'</a>'.'<br>';
}
}
closedir($handle);
}
?>
<p style="font-weight: bold; font-size: 10pt">List of files:</p>
<p style="font-weight: bold; font-size: 10pt; color: #560D1C"><?=$thelist?></p>

The only thing now is, am I able to automatically hide specific files from the list?? I.E. .htaccess, index.php??

Just show all other files? Thanks

Lorne

lorne17
08-13-2008, 06:43 PM
Sorry still playing with this.

So two things I'd like to do with this...
1. hide specific files
2. upload files to the same folder.

Regarding the upload files, i've been trying to play with it on W3Schools, but can't quite seem to get it to work. Any thoughts??

Thanks,

Lorne

lorne17
08-13-2008, 10:08 PM
Sorry to keep posting,
I got the hide file extensions set...

<?php
if ($handle = opendir('.')) {
$ignore_files = array('.', '..', '.htaccess', '.htpasswd', 'index.php');
while (false !== ($file = readdir($handle)))
{
if (!in_array($file, $ignore_files))
{
$thelist .= '<a href="'.$file.'">'.$file.'</a>'.'<br>';
}
}
closedir($handle);
}
?>
<p style="font-weight: bold; font-size: 10pt">List of files:</p>
<p style="font-weight: bold; font-size: 10pt; color: #560D1C"><?=$thelist?></p>

now I just need to know how to upload to the same folder the index.php is located. This way when I put the index.php file into another directory, It will automatically upload files to that new directory, and avoid having to point it to the directory. make sense??

Thanks,
lorne

edbr
08-14-2008, 01:01 AM
no not really. you are asking how to upload files to a folder?

lorne17
08-14-2008, 01:09 AM
Yea sorry, it is confusing, I'm trying to put an "upload form" in my html coding and when I use PHP to tell it where to upload I get stuck.

All I want to figure out is how to create an upload form in DreamWeaver and when the user uploads say an .jpg. It is loaded into the same folder the current webpage is (the current page would be index.php). So therefore the next time index.php is loaded then the new .jpg will be in the list that we set with the code above because the new .jpg was uploaded to the same folder.

Now does that make sense?

edbr
08-14-2008, 01:14 AM
think so , you need an 'upload script' right?
Check the video tuts on this site first , i dont know if davidj has done a video tut but i suspect he has. If not start a new post asking about uploading files

randyb
08-11-2011, 03:14 AM
Hello, first, thank for the code. I am new in PHP and jsut wonder if possible to add "Sort by Filename" for this code? THANK YOU!

<?php
if ($handle = opendir('.')) {
$ignore_files = array('.', '..', '.htaccess', '.htpasswd', 'index.php');
while (false !== ($file = readdir($handle)))
{
if (!in_array($file, $ignore_files))
{
$thelist .= '<a href="'.$file.'">'.$file.'</a>'.'<br>';
}
}
closedir($handle);
}
?>

edbr
08-11-2011, 04:38 AM
if you add this as index.php it will list files in alphabetical order

<?php
if ($handle = opendir('.')) {
while (false !== ($file = readdir($handle)))
{
if ($file != "." && $file != "..")
{
$thelist .= '<a href="'.$file.'">'.$file.'</a>'."<br>";
}
}
closedir($handle);
}
?>
<P>List of files:</p>
<P><?php echo $thelist;?></p>

randyb
08-11-2011, 12:18 PM
hi, from your last posting, I tried and it works but not sorting the full like if I am in windows explorer-name sorted. then I also lost the ignore files? thanks.

edbr
08-12-2011, 01:12 AM
ike if I am in windows explorer-name sorted.
not sure what you want then
I also lost the ignore files
that can be added

edbr
08-12-2011, 03:14 AM
do you mean like this?

<?php
if ($handle = opendir('.')) {
$ignore_files = array('.', '..', '.htaccess', '.htpasswd', 'index.php');
while (false !== ($file = readdir($handle)))
{
if (!in_array($file, $ignore_files))
{
$thelist .= '<a href="'.$file.'">'.$file.'</a>'.'<br>';
}
}
closedir($handle);
}
?>
<P>List of files:</p>
<P><?php echo $thelist;?></p>
<P><?php echo asort($thelist);?></p>