【问题标题】:PHP fopen doesn't work although file checks as writable尽管文件检查为可写,但 PHP fopen 不起作用
【发布时间】:2013-04-14 07:08:43
【问题描述】:

帮助朋友解决他遇到的问题。他的站点使用 fopen 函数打开文件并保存更改,但 fopen 失败并出现“Permission Denied”错误。该文件位于远程服务器上,权限似乎正确。

他使用的代码是……

if (isset($_POST['save'])) {
$filepath = "string.txt";
    if (file_exists($filepath)) {
        $file = fopen($filepath, "w+");
        fwrite($file, $_POST['save']);
        fclose($file);
    }
}

我在这里使用了 VolkerK 的代码php fopen returns false but file is readable/writable 来确定读/写权限并获得以下内容。

PHP 版本:5.4.9
名称:Windows NT
{文件} 存在
{File} 可读
{File} 可写
最后一个错误:array(4){["type']=>int(2)["message"]=>string(131 "fopen({file}):failed to open stream: Permission denied.

起初我认为这是文件权限问题,但我认为如果 PHP 将文件视为可写,这应该不是问题,我是否正确阅读?

【问题讨论】:

  • 在win7上进行了快速测试,这对我有用。该文件使用 $_POST 变量的内容写入。尝试使用新的 *.txt 文件。
  • 你说文件在远程服务器上,可能allow_url_fopen 可能是假的——虽然我不确定这会是问题,但它是值得一看。除此之外,代码按原样运行,因此我们需要远程服务器上的更多上下文和正在使用的$filepath
  • 如果是远程文件,你使用的是什么协议?
  • 这是http。也许我应该尝试 ftp?

标签: php fopen


【解决方案1】:

尝试了 FTP 并到达那里。不过,必须正确设置 FTP 连接的上下文选项。

感谢您为我指明正确的方向。

if (isset($_POST['save'])) {
//Setup FTP connection
$ftp = "ftp://user:password@example.com/filepath/filename.txt";
//Set FTP Options
$stream_options = array('ftp' => array('overwrite' => true));
$stream_context = stream_context_create($stream_options);
//Open file and write changes
if (file_exists($ftp)) {
    $file = fopen($ftp, "w", 0 , $stream_context);
    fwrite($file, $_POST['save']);
    fclose($file);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-06-02
    • 2018-11-15
    • 2011-04-09
    • 1970-01-01
    • 2019-04-09
    • 1970-01-01
    • 2014-07-05
    • 1970-01-01
    相关资源
    最近更新 更多