You are here: download all files in a directory « All PHP « IT news, forums, messages
download all files in a directory

Posted by darren on 07/04/06 19:50

Hello Folks,

I have the following code that displays files and directoies within a
directory and display the thumbnail exif data if it exists. And
everybody was happy with that for a few hours.

Now I have been asked to add a button that downloads the files in the
directory and I can't quite get my head round it.

Could someone point me in the right direction please.


<?php
//////////////////////////////////////////////////////////////////////////////////
//this is version 01072006 index sitewide
///////////////////////////////////////////////////////
//the directories and files must only contain a dash - or minus sign -
or hyphen - once.////
//////////////////////////////////////////////////////////////////////////////////
?>
<html>
<head>
<title>Richards Contractors Client Portal</title>
</head>
<body>
<h2>Richards Contractors Client Portal</h2>
<form>
<input type="button" value="Back" onclick="history.back()">
<input type="button" onClick="parent.location='http://....'"
value="Home">
</form>
<?php
/////////////////THE DIRECTORY list/////////////////////////////
// TASK: display a directory listing with thumbnails for images and
human-readable filesizes

// handy humansize function:
// input is number of bytes, output is a "human-readable" filesize
string
function humansize($size) {

// Setup some common file size measurements.
$kb = 1024; // Kilobyte
$mb = 1024 * $kb; // Megabyte
$gb = 1024 * $mb; // Gigabyte
$tb = 1024 * $gb; // Terabyte

if($size < $kb) return $size."B";
else if($size < $mb) return round($size/$kb,0)."KB";
else if($size < $gb) return round($size/$mb,0)."MB";
else if($size < $tb) return round($size/$gb,0)."GB";
else return round($size/$tb,2)."TB";
}

// get local directory path
$path= dirname($_SERVER['SCRIPT_FILENAME']);

?>

<h3><!-- Files in <?php //print $path; ?>: --></h3>

<ul>

<?php
//intinialise loop variable
$l = 0;
//initiate the arrays
$unprefexedfiles = array();
$unprefexeddirs = array();
$multifiles = array();
$multi = array();

$d = dir($path);
$icon = '';

while (false !== ($entry = $d->read())) {
if (substr($entry, 0, 1)=='.' ) continue;

// get size
$size = filesize($path.'/'.$entry);
$humansize = humansize($size);

// find filename extension
$dotpos = strrpos($entry, '.');

if ($dotpos) {
$ext = substr($entry, $dotpos+1);
if ($ext === 'jpg' || $ext === 'php' || $ext === 'JPG') {
//if then file has the extensions above then do something or nothing
// $icon = "<img src='$entry' style='width: 48px; height:
auto; vertical-align: text-top;' alt='icon' title='$entry' />";
//print "<li><a href='$entry'>$entry</a> ($humansize)
$icon</li>\n";
//echo $entry;
}
else
{
/////////////////////////PUT the files in order
//get the position of the - dash
$pos = strrpos($entry, "-");
//if dash is position 2 get the first bit
if($pos == 4){
//get the first part of the name
$firstfiles = substr($entry,0,4);
//turn first into an interger
settype($firstfiles, "integer");
//add entry into an array
//add data to array
$multifiles[$firstfiles] = $entry;
//print out the results

}
else
{
//add the unprefexfiles to array
$unprefexedfiles[$l] = $entry;
//print the files that aren't to be ordered
//print "<li><a href='$entry'>$entry</a> ($humansize)
$icon</li>\n";
}
//////////////////////////////////////////////////////////////////////
}
}
else
{
//get the position of the - dash
$pos = strrpos($entry, "-");
//if dash is position 2 get the first bit
if($pos == 4){
//get the first part of the name
$first = substr($entry,0,4);
//turn first into an interger
settype($first, "integer");
//add entry into an array
//add data to array
$multi[$first] = $entry;
//print out the results
//print "<li><a href='$entry'>$str</a> $first (DIR)</li>\n";
}
else
{
//capitalise directories
//$str = strtoupper($entry);
//print the other directories
//print "<li><a href='$entry'>$str</a> (DIR)</li>\n";
//$icon= '';
//add the unprefixed directories to an array
$unprefexeddirs[$l] = $entry;
}

}
}
//print_r($unprefexedfiles);
//print the unprefexed directories
foreach($unprefexeddirs as $number => $dir){
$str = strtoupper($dir);
print "<li><a href='$dir'>$str</a> (DIR)</li>\n";
}
//print the unprefexed files
foreach($unprefexedfiles as $number => $dir){
$str = $dir;
print "<li><a href='$dir'>$str</a></li>\n";
}
//echo $multi[9];
//echo $multi[2];
//echo $multi[20];
//sort and print the prefixed files
sort($multifiles);
foreach($multifiles as $number => $dir){
$str = $dir;
print "<li><a href='$dir'>$str</a></li>\n";
}
//sort and print the prefixed directories
sort($multi);
foreach($multi as $number => $dir){
$str = strtoupper($dir);
print "<li><a href='$dir'>$str</a> (DIR)</li>\n";
}
$l++;
$d->close();
////////////////////////END OF DIRECTORY
list//////////////////////////////
?>
</ul>
<table>
<tr><td>
<?php // define directory path
$dir = ".";
// iterate through files
// look for JPEGs
if (is_dir($dir)) { if ($dh = opendir($dir)) { while (($file =
readdir($dh)) !== false) { if (preg_match("/.JPG/", $file)) {
// read EXIF headers
$exif = exif_read_data($file, 0, true); echo "<tr>";
// get thumbnail
// link to full image
echo "<td valign=top><a href=$dir/$file><img
src=thumbnail.php?file=$file></a><td>";
echo "<td valign=top><font size=-1>";
// get file name
echo "File: <b>" . $exif['FILE']['FileName'] . "</b><br/>";
// get timestamp
echo "Timestamp: " . $exif['IFD0']['DateTime'] . "<br/>";
// get image dimensions
echo "Dimensions: " . $exif['COMPUTED']['Height'] . " x " .
$exif['COMPUTED']['Height'] . " <br/>";
// get camera make and model
echo "Camera: " . $exif['IFD0']['Model'];
echo "</font></td>";
echo "</tr>";
}
}
closedir($dh);
}
}
?>
</td>
<td>&nbsp;</td>
</tr>
</table>

</body>
</html>

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация