如何使用php使用for循环从目录中读取文件

时间:2018-01-08 12:21:09

标签: php

我想使用我的images-folder侧面的循环作为图像名称来读取图像         将从1.jpg到100.jpg,在这里我使用数组来获取图像

$files = array("images-folder/1.jpg","images-folder/2.jpg"); /*Image array*/ 

我在我的图片文件夹中读取.jpg扩展图片(图片 -          夹)          并将它们下载到zip文件中。到目前为止它正在使用这些行          代码          $ files = array(“images-folder / 1.jpg”,“images-folder / 2.jpg”); / 图像          阵列 /

create new zip opbject
  $zip = new ZipArchive();

     $tmp_file = tempnam('.',''); /*create a temp file & open it*/
     $zip->open($tmp_file, ZipArchive::CREATE);

     # loop through each file
     foreach($files as $file){

       # download file
         $download_file = file_get_contents($file);        
            $zip->addFromString(basename($file),$download_file);/*add it to the zip*/
     }

        # close zip
        $zip->close();/*send the file to the browser as a download*/

        header('Content-disposition: attachment; filename=download.zip');
        header('Content-type: application/zip');
        readfile($tmp_file);
        echo($file);

1 个答案:

答案 0 :(得分:0)

您可以使用以下文件名创建数组:

$files = array();

for ($i = 1; $i <= 100; ++$i) {
   $files[] = "images-folder/$i.jpg";
}

只需遍历所有数字并将它们存储在数组中。