使URL不可读

时间:2015-05-23 20:47:25

标签: php .htaccess

我正在编写一个脚本,您可以上传示例图片并下载..但是当我尝试下载文件时,它看起来像; http://localhost/uploads/file/filename.png

这对于黑客来说并不是很安全..他们可以放入image.png image1.png等等。所以我真正需要帮助的是,我怎样才能使链接不可读; http://localhost/asdajh1728dsa871nsada87或类似的那个或任何不可读的东西。

我只使用rawurlencode。

我挣扎了几天,非常感谢帮助。

如果可以通过.htaccess文件,也欢迎:)

2 个答案:

答案 0 :(得分:0)

可以使用fpassthru,此代码段将/images/$encoded_path的请求转换为show_image.php?img=$encoded_path,并让php返回数据并解码路径

PHP:

$decoded=some_decoding($_GET['img']);
// open the file in a binary mode
$name = './uploads/file/'.$decoded;
$fp = fopen($name, 'rb');

// send the right headers
header("Content-Type: image/png");
header("Content-Length: " . filesize($name));

// dump the picture and stop the script
fpassthru($fp);
exit;

和.htaccess

RewriteRule    ^images/([0-9a-z]+)/?$    show_image.php?img=$1    [NC,L]

答案 1 :(得分:0)

如果您只想“加扰”文件名,请使用带文件名和时间戳的md5。

$filename = bin2hex(openssl_random_pseudo_bytes(16));

那会给你一个随机的16 * 2长字符串。 只需使用随机文件名保存文件即可。 您可以更改16到32或64以创建更长的名称。