PHP设置文件只能读/下载

时间:2015-03-24 18:13:51

标签: php post permissions chmod

为了简短起见,我制作了一个非常简单的文件上传脚本。我需要做的是获取刚刚上传的文件,将其保存到指定的目录,并使文件只读/不可执行。为什么?好吧,如果有人要上传一个PHP脚本来向某人展示,那么该脚本就不会被允许运行,相反,它只会显示该文件的实际文本。

我一直试图用chmod()来做这件事,但没有任何效果。

$target_directory = "./" . $_POST["directory"] . "/"; // The target directory to put the file.

$file_name = $_FILES["file"]["name"]; // The name of the file being uploaded (Example: file.ext)

$target_file = $target_directory . $file_name; // The more compiled target file/location. (Example: /folder/file.ext)

if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) {
    echo "The file was uploaded. at <a href='" .$target_file ."'>".$target_file ."</a>";
    chmod($target_file, 0644);

我需要编写什么样的代码才能使chmod()工作,或者将目录/文件设置为不执行?

2 个答案:

答案 0 :(得分:0)

我自己使用.htaccess修复了它。

使用php_flag engine off关闭PHP处理,并ForceType text/plain强制服务器提供.php文件,就像它是文本文档一样。

答案 1 :(得分:-1)

您可以在.htaccess中创建重定向并使用echo file_get_contents();

处理输出