【发布时间】:2015-03-24 18:13:50
【问题描述】:
为了简短起见,我正在制作一个非常简单的文件上传脚本。我需要做的是获取刚刚上传的文件,将其保存到指定的目录,并使文件只读/不可执行。为什么?好吧,如果有人要上传一个 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() 工作,或者将目录/文件设置为不执行?
【问题讨论】:
-
需要查看完整的上传代码以及上传文件的存储位置。否则,请在打开 PHP 标记(例如
<?php error_reporting(E_ALL); ini_set('display_errors', 1);)之后将错误报告添加到文件顶部,然后是其余代码,以查看它是否产生任何结果。 -
PHP 或服务器用户(PHP 运行的地方,而不是您的管理员登录用户)是否有权编辑该目录?
-
即使 PHP 文件是只读且不可执行的,它仍然会作为 PHP 代码处理。保存文件时需要将“php”扩展名改为“phps”。
-
@Fred-ii- 没有错误,如果我上传一个,它仍然允许我执行一个 php 脚本,而不是使它成为只读的。
-
@RobertDundon 拥有完全权限。
标签: php post permissions chmod