Page 1 of 1

download all files in a directory

Posted: Tue Jul 04, 2006 2:47 pm
by darren
hawleyjr | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


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.

Code: Select all

<?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>
hawleyjr | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Tue Jul 04, 2006 5:28 pm
by Burrito
so you want to download all of the files in the directory in one shot? Your best bet would be to zip them all up (take a look at redmonkey's zip class) then grab the zip file.