繁体   English   中英

如何通过唯一的php文件下载多个图像?

[英]How to download multiple images through an unique php file?

我需要一个能够从服务器上的文件夹本身下载多个图像的php文件,每个图像具有唯一的ID。

例如

我的服务器上有一个图像:/home/example/public_html/multipleimage/2.png

我想通过唯一的php文件下载该图像: http : //example.com/file.php

我将键入以下URL: http : //example.com/file.php?id=2并且浏览器将必须返回图像。

但是... php文件应该如何? 无需通过数据库就能完成吗?

谢谢。

<?php
header("Content-Type: image/png");
readfile(
    sprintf(
        '/home/example/public_html/multipleimage/%d.png', 
        $_GET['id']
    )
);

请参阅PHP手册中的以下条目:

请注意,我正在将sprintf%d配合使用,因此它将无提示地将任何非数字id值转换为0,因此, ../../etc/passwd类的恶意输入将尝试读取0.png 如果要使用除数字以外的其他任何东西,则需要清理输入,以防止目录遍历攻击空字节中毒(在PHP 5.3.4之前)

我仍然不确定我是否知道你想要什么,但是事情就这样了:

<?php
if (array_key_exists('id', $_GET) && is_numeric($_GET['id'])) {
    header("Content-Type: image/png");
    echo file_get_contents("/home/example/public_html/multipleimage/".$_GET['id'].".png");
}

?>

暂无
暂无

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

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