douhuan6157 2014-11-27 23:30
浏览 256
已采纳

使用php readfile读取多个图像文件

I am trying to read multiple image files from a folder (.htaccess protected) and display in a HTML page using php readfile().

The problem is I can see only the first image is read and the next is not shown in the browser. The code is as below

<?php
$image1 = 'files/com_download\256\50\wwwes\icon\android\icon-36-ldpi.png';
$image2 = 'files/com_download\256\50\wwwes\icon\android\icon-48-mdpi.png';

$imginfo = getimagesize($image1);
header("Content-type: ".$imginfo['mime']);
readfile($image1);

$imginfo = getimagesize($image2);
header("Content-type: ".$imginfo['mime']);
readfile($image2);
?>

I could see the first image 'icon-36-ldpi.png' successfully read and displayed in the browser and the second image is not read and not displayed in the browser.

Am I missing something? Any advice please.

Sorry if I am doing stupid but the requirement is to read multiple image files and render in the browser like a grid view. I cannot use img tag because of security reasons.

  • 写回答

1条回答 默认 最新

  • duanhe4267 2014-11-27 23:38
    关注

    You can't dump both images out at once. Why not make two images in your html so the browser makes two calls to your script. Then use a GET param to pass the filename you want to display.

    ---Edit---

    Important Security Note

    There is an attack vector which you open up when doing soething like this. Someone could easily view your source html and change the parameter to get your image script to output any file they want. They could even use "../../" to go up directories and search for well known files that exist. e.g. "../../../wp_config.php". Now the attacker has your wordpress database credentials. The correct way to prevent against this is to always validate the input parameter properly. For example, only output if the file name ends with ".jpg"

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?