如何从文件夹下载所有图像

时间:2012-12-06 08:15:27

标签: php html apache

我正在通过Apache服务器显示图像。 当我点击每个图像时,它应该打开download.php?fiel=file_name.jpg并将图像下载到系统。

这是图像显示的代码

  <?php
 $files = glob("Image/"."*.*");
 for ($i=0; $i<count($files); $i++)
{
$num = $files[$i];
echo $num."<br>";
echo '<img src="'.$num.'" alt="Here should be image" width="256" height="192" >'."<br/>";
}
?>

我使用以下代码下载Image (即download.php文件)。

 <?php
 $file = $_GET['file'];
  header("Content-type: image");
 header("Content-disposition: attachment; filename= ".$file."");
 readfile($file);
 ?> 

如何实现这一目标?

4 个答案:

答案 0 :(得分:1)

你的意思是这样的? :

<?php
$files = glob("Image/"."*.*");
$countFiles = count($files); //Don't do calculations in your loop, this is really slow

for ($i=0; $i<$countFiles; $i++)
{
    $num = $files[$i];

    echo '<a href="download.php?file=' . $files[$i] . '"><img src="'.$num.'" alt="Here should be image" width="256" height="192" ></a><br/>';
}
?>

答案 1 :(得分:0)

foreach (glob("Image/*.*") as $file) {
  echo "<a href='download.php?file=$file'>$file<br>\n";
  echo "<img src='$file' alt='Here should be image' width='256' height='192'/><br>\n</a>";
}

答案 2 :(得分:0)

如果您想将所有图像下载到一个文件夹中,我建议您使用zip或类似工具存档图像。你可以动态地在php中压缩文件。要迭代目录中的所有文件,请查看RecursiveDirectoryIterator

foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir)) as $file) {
    var_dump($file);
}

答案 3 :(得分:-2)

试试这个

if(isset($_GET['file'])){
    //Please give the Path like this
    $file = 'images/'.$_GET['file'];

if (file_exists($file)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($file));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    readfile($file);
    exit;
}

}

相关问题