...

View Full Version : how to list all the files in current directory



ketanco
01-29-2012, 06:32 PM
i uploaded pictures to a directory and in that directory i will make an index.php file and with this file i want to be able to list all the pictures in this folder clickable so that my friend who lives far from me can click each of them and look at it. how can i do that?

jmj001
01-29-2012, 07:14 PM
$folderPath = 'whereismyfiles';
$fList = array();
if ($handle = opendir($folderPath)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && substr($file,0,2) != "._" && $file != "..") {
$fList[] = $file;
}
}
closedir($handle);
}
// this is the files list
sort($fList);
print_r($fList);

ketanco
01-29-2012, 08:29 PM
$folderPath = 'whereismyfiles';
$fList = array();
if ($handle = opendir($folderPath)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && substr($file,0,2) != "._" && $file != "..") {
$fList[] = $file;
}
}
closedir($handle);
}
// this is the files list
sort($fList);
print_r($fList);
thanks, for the 'whereismyfiles' i typed '../mycurrentfoldername' and it listed the file names but how can i make each item in the list a clickable item so that when clicked the picture will open?

jmj001
01-29-2012, 09:09 PM
// loop through the files and read the functions
$j = count($fList);
for($i = 0; $i < $j; $i++){
echo '<a href="">'.$fList[$i].'</a><br>';
}


you will need to figure out the href=""

weir-07
01-29-2012, 09:16 PM
Here's a solution which will just iterate the images for you and make them clickable.


<? function getFileExtension($filename)
{
$path_info = pathinfo($filename);
return @$path_info['extension'];
}

function getFileList($dir)
{
// array to hold return value
$retval = array();

// add trailing slash if missing
if(substr($dir, -1) != "/") $dir .= "/";

// open pointer to directory and read list of files
$d = @dir($dir) or die("getFileList: Failed opening directory $dir for reading");
while(false !== ($entry = $d->read())) {
// skip hidden files
if($entry[0] == ".") continue;
if(is_dir("$dir$entry")) {
$retval[] = array(
"name" => "$dir$entry/",
"type" => filetype("$dir$entry"),
"size" => 0,
"lastmod" => filemtime("$dir$entry")
);
} elseif(is_readable("$dir$entry")) {
$retval[] = array(
"name" => "$dir$entry",
"type" => getFileExtension("$dir$entry"),
"size" => filesize("$dir$entry"),
"lastmod" => filemtime("$dir$entry")
);
}
}
$d->close();

return $retval;
}
$dirlist = getFileList(".");
?>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Pictures</title>
</head>
<blockquote><br />
// <b>Pictures</b> //<br /><br /><table border="1">
<tr><th width="200">Name</th><th>Size</th><th>Last Mod.</th></tr>
<?PHP
// output file list as table rows
foreach($dirlist as $file) {
if(($file['type'] != 'jpg')||($file['type'] != 'jpeg')||($file['type'] != 'gif')||($file['type'] != 'png')) continue;
echo "<tr>\n";
echo "<td><a href=\"{$file['name']}\">",basename($file['name']),"</a></td>\n";
echo "<td>{$file['size']}</td>\n";
echo "<td>",date('r', $file['lastmod']),"</td>\n";
echo "</tr>\n";
}
?>
</table>
<body>
</body>
</html>
:)

ketanco
01-29-2012, 10:21 PM
// loop through the files and read the functions
$j = count($fList);
for($i = 0; $i < $j; $i++){
echo '<a href="">'.$fList[$i].'</a><br>';
}


you will need to figure out the href=""

ok now it lists the files as clickable links but when i click does not open the pictures. how can i open that picture when i click its link?

weir-07
01-29-2012, 10:25 PM
The code I posted - copied and pasted into an index file in your pictures folder will do the trick without any fuss. :)

ketanco
01-29-2012, 10:39 PM
The code I posted - copied and pasted into an index file in your pictures folder will do the trick without any fuss. :)
actually i tried yours too but it only listed the titles of a table and that was it. i copied and pasted your code as it is. what might be wrong?

weir-07
01-29-2012, 10:45 PM
Woops should have tested that!

Remove the line;

if(($file['type'] != 'jpg')||($file['type'] != 'jpeg')||($file['type'] != 'gif')||($file['type'] != 'png')) continue;

ketanco
01-29-2012, 10:56 PM
Woops should have tested that!

Remove the line;

if(($file['type'] != 'jpg')||($file['type'] != 'jpeg')||($file['type'] != 'gif')||($file['type'] != 'png')) continue;
woow very nice thanks a lot!!! it even zooms in once i open picture....

ketanco
01-30-2012, 12:06 AM
Woops should have tested that!

Remove the line;

if(($file['type'] != 'jpg')||($file['type'] != 'jpeg')||($file['type'] != 'gif')||($file['type'] != 'png')) continue;


one more thing... what if i wanted the pictures to display without clicking, as soon as i open the webpage? what part of the code would i change and how? (i know for many pictures it would be a very very slow opening webpage but thats another thing...)

weir-07
01-30-2012, 12:09 AM
Make a html document.
Name it index.
Then insert your pictures into that.

You can google how to do that, I'm sure :)

ketanco
01-30-2012, 01:45 AM
ok i know how to do that but for that i would have to type all file names one by one... with your method, i dont... what if i want to share 400 photos with my friend... thats whyi asked that :)

BluePanther
01-30-2012, 10:14 PM
Coming back to this, just change the a tags to img tags, with the src as $file['name'] OR change the inside of the a tag an img tag, again with $file['name'] as the src.

kbluhm
01-30-2012, 11:28 PM
I can't believe no one has suggested using glob() yet:

$files = glob( './files/*.{jpg,JPG,jpeg,JPEG,gif,GIF,png,PNG}', GLOB_BRACE );

foreach ( $files as $file )
{
echo '<a href="' . $file . '">', basename( $file ), '</a><br />';
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum