[英]How to download multiple images through an unique php file?
I need a php file with the ability to download multiple images per se from a folder on my server, with an unique ID per image. 我需要一个能够从服务器上的文件夹本身下载多个图像的php文件,每个图像具有唯一的ID。
eg 例如
I have an image on my server: /home/example/public_html/multipleimage/2.png 我的服务器上有一个图像:/home/example/public_html/multipleimage/2.png
And I want to download that image through the unique php file: http://example.com/file.php 我想通过唯一的php文件下载该图像: http : //example.com/file.php
I would type the following url: http://example.com/file.php?id=2 And the browser would have to return the image. 我将键入以下URL: http : //example.com/file.php?id=2并且浏览器将必须返回图像。
But... how should be the php file? 但是... php文件应该如何? Can it be done without going through a database?
无需通过数据库就能完成吗?
Thanks. 谢谢。
<?php
header("Content-Type: image/png");
readfile(
sprintf(
'/home/example/public_html/multipleimage/%d.png',
$_GET['id']
)
);
See the following entries in the PHP Manual: 请参阅PHP手册中的以下条目:
header
- Send a raw HTTP header header
-发送原始HTTP标头 readfile
- Reads a file and writes it to the output buffer. readfile
读取文件并将其写入输出缓冲区。 sprintf
- Returns a string produced according to the formatting string format. sprintf
返回根据格式字符串格式产生的字符串。 $_GET
- associative array of variables passed to the current script via the URL parameters $_GET
通过URL参数传递给当前脚本的变量的关联数组 Note that I am using sprintf
with %d
, so it will silently convert any non-numeric id value to 0, so a malicious input like ../../etc/passwd
will try to read 0.png
. 请注意,我正在将
sprintf
与%d
配合使用,因此它将无提示地将任何非数字id值转换为0,因此, ../../etc/passwd
类的恶意输入将尝试读取0.png
。 If you want to use anything else but numbers, you need to sanitize the input to prevent directory traversal attacks and null byte poisoning (before PHP 5.3.4) : 如果要使用除数字以外的其他任何东西,则需要清理输入,以防止目录遍历攻击和空字节中毒(在PHP 5.3.4之前) :
I'm still not sure I know what you want but here it goes: 我仍然不确定我是否知道你想要什么,但是事情就这样了:
<?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.