Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 上载文件夹安全解决方案_Php_Security_Upload_Chmod - Fatal编程技术网

Php 上载文件夹安全解决方案

Php 上载文件夹安全解决方案,php,security,upload,chmod,Php,Security,Upload,Chmod,我们都知道chmod777并不是让我们的上传脚本工作的最佳方式。所以我在想,当使用它时,什么是最好的解决方案 我认为当用户上传文件“picture.jpg”时,我可以将其重命名为“md5(username-picture-1.jpg)”,并在网页上显示为 然后在这个files.php文件上 <?php $file = md5($_GET['img']); header('Content-Type: image/jpeg'); header('Content-Len

我们都知道chmod777并不是让我们的上传脚本工作的最佳方式。所以我在想,当使用它时,什么是最好的解决方案

我认为当用户上传文件“picture.jpg”时,我可以将其重命名为“md5(username-picture-1.jpg)”,并在网页上显示为

然后在这个files.php文件上

<?php
    $file = md5($_GET['img']);

    header('Content-Type: image/jpeg');
    header('Content-Length: ' . filesize($file));
    echo file_get_contents($file);
?>

这样用户就不会知道他上传的文件名,也就是说,他将无法执行它。我说得对吗


很明显,我会有一些其他检查来处理上传,比如mime类型和其他东西。我只是想知道这是否是阻止用户执行他上传的文件的一种方法。

您只需将一个.htaccess文件添加到包含上传文件的文件夹中,即可防止执行,但我认为,在将上传的文件永久存储在文件系统之前,最好先检查其mime类型/文件扩展名,这会让你很恼火,因为这样会弄清楚请求的文件是什么。但大多数代码都是开源的,所以你不能靠默默无闻来保证安全性。像Facebook这样的人不这么做还有另一个原因。您已经将您的网站转换为每页1个PHP执行,每幅图像1个PHP执行(+1个页面)。在一个可能是交通量30倍的画廊里。您不需要
chmod 777
无论如何,该文件应该只由Web服务器运行时的用户执行(不应该是root用户)。这怎么办?我没有ssh访问权限!你是说像order deny allow deny from all这样的东西吗?不-你想阻止对完整目录的访问-因此你必须在你的/upload文件夹中放置一个.htaccess文件,使用
deny from all
,因为它是内容。这样做,如果你使用src=“file.php”,我仍然能够在页面上显示图像解决方案-yesLFI攻击仍然存在问题。