2015-05-23 63 views
1

我正在制作一个脚本,您可以上传示例图片并下载它。但是,当我尝试下载文件时,它看起来像; http://localhost/uploads/file/filename.png使URL无法读取

这是不是真的很安全我想黑客..他们只能放在image.png image1.png等..所以我确切需要帮助是,我怎么能使一个链接不可读取像; http://localhost/asdajh1728dsa871nsada87或类似的那个,或任何不可读的东西。

我只是使用rawurlencode。

我挣扎了好几天,真的很感激。

如果通过的.htaccess是可能的文件其也欢迎:)

+0

您是否试图阻止黑客猜测您的图片文件?或者你是否试图阻止黑客将不好的东西放入你的图像文件中? – Tim3880

+0

如何使用随机图像名称,而不是使用数据库来存储它.. – Shubanker

+0

我试图防止黑客访问我的上传文件夹的内容..你可以猜想像/uploads/filename1.png,如果它存在比你可以看到它,但我不想有这样的东西..只是一个安全的网址,所以他们不能看到文件夹/文件结构。 –

回答

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] 
+0

但是,如果我还有其他文件扩展名?请尽快回复:) –

+0

我还没有介绍如何对图像进行编码和解码,我将这项工作留给了你:-),如果你愿意,你可以在编码中包含扩展名。 –

0

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

$filename = bin2hex(openssl_random_pseudo_bytes(16)); 

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

+0

感谢您的快速回复,我会尝试应用它:) –