PHP fopen不工作,虽然文件检查为可写


PHP fopen doesn't work although file checks as writable

帮助朋友解决他遇到的问题。他的网站正在使用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
名称:Windows NT
{File}
存在{文件}是可读的
{文件}是可写的
最后一个错误:array(4){["type "]=> int(2)["message"]=>string(131 "fopen({file}):failed to open stream: Permission denied.

起初我认为这是一个文件权限问题,但我认为如果文件被PHP视为可写,这应该不是问题,我读对了吗?

尝试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);
    }
}