PHP 仅将文件设置为 read/download

PHP set file to read/download only

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

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

处理输出

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

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