【问题标题】:PHP set file to read/download onlyPHP 将文件设置为仅读取/下载
【发布时间】: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 标记(例如 &lt;?php error_reporting(E_ALL); ini_set('display_errors', 1);)之后将错误报告添加到文件顶部,然后是其余代码,以查看它是否产生任何结果。
  • PHP 或服务器用户(PHP 运行的地方,而不是您的管理员登录用户)是否有权编辑该目录?
  • 即使 PHP 文件是只读且不可执行的,它仍然会作为 PHP 代码处理。保存文件时需要将“php”扩展名改为“phps”。
  • @Fred-ii- 没有错误,如果我上传一个,它仍然允许我执行一个 php 脚本,而不是使它成为只读的。
  • @RobertDundon 拥有完全权限。

标签: php post permissions chmod


【解决方案1】:

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

【讨论】:

  • .htacces 喜欢:RewriteEngine On RewriteCond "%{SCRIPT_FILENAME}" "(.*)\/my_special_folder\/(.*)\.php" RewriteRule "^(.*)\/(.*)\.php$" my_php_file_viewer.php?my_file=$2 和 my_php_file_viewer.php 喜欢:echo file_get_contents($_GET['my_file'].'.php'); `
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 2013-07-02
    • 1970-01-01
    • 1970-01-01
    • 2021-12-13
    • 2014-08-02
    • 2012-04-17
    • 1970-01-01
    • 2011-10-15
    • 1970-01-01
    相关资源
    最近更新 更多