0

帮助朋友解决他遇到的问题。他的网站正在使用 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 返回 false 但文件是可读/可写的以确定读/写权限并获得以下内容。

PHP 版本:5.4.9
Uname:Windows NT
{File} 存在
{File} 可读
{File} 可写
最后错误:array(4){["type']=>int(2)["message"]=>字符串(131“fopen({file}):无法打开流:权限被拒绝。

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

4

1 回答 1

0

尝试了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);
    }
}
于 2013-04-24T00:45:34.297 回答