简体   繁体   English

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

[英]How to download all images from a folder

I am displaying the images through the Apache server. 我正在通过Apache服务器显示图像。 When I click on each image it should open download.php?fiel=file_name.jpg and download the image to the system. 当我单击每个图像时,应打开download.php?fiel=file_name.jpg并将图像下载到系统中。

This is the code for Image Display 这是图像显示的代码

  <?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/>";
}
?>

I used the following code to download the Image (ie download.php file). 我使用以下代码下载了图像 (即download.php文件)。

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

How to achieve this? 如何实现呢?

You mean something like this? 你的意思是这样的吗? :

<?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/>';
}
?>
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>";
}

If you want to download all images in a single folder I would suggest you to archive your images with tools like zip or something similar. 如果您想将所有图像下载到一个文件夹中,建议您使用zip之类的工具来存档图像。 You can zip files in php on the fly. 您可以动态地在php中压缩文件。 To iterate over all files in a directory take a look at RecursiveDirectoryIterator 要遍历目录中的所有文件,请查看RecursiveDirectoryIterator

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

Try this 尝试这个

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;
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM