PHP将文件设置为只读/下载


PHP set file to read/download only

简而言之,我正在制作一个非常简单的文件上传脚本。我需要做的是,获取刚刚上传的文件,将其保存到指定的目录中,并使文件只读/不可执行。为什么?好吧,如果有人上传一个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()工作,或者将目录/文件设置为不执行?

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

我自己用.htaccess修复了它。

通过使用php_flag engine off关闭PHP处理,并使用ForceType text/plain强制服务器将.PHP文件当作文本文档来提供。