帮助朋友解决他遇到的问题。 他的网站使用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}是可写的 上一个错误:array(4){[“type'] => int(2)[”message“] => string(131”fopen({file}):无法打开流:权限被拒绝。
起初我认为这是一个文件权限问题,但我认为如果PHP将文件视为可写,这应该不是问题,我是否正确阅读?
Helping a friend with an issue he's having. His site is using a fopen function to open a file and save the changes to it but the fopen is failing with a "Permission Denied" error. The file is on a remote server and permissions seem to be correct.
The code he is using is...
if (isset($_POST['save'])) { $filepath = "string.txt"; if (file_exists($filepath)) { $file = fopen($filepath, "w+"); fwrite($file, $_POST['save']); fclose($file); } }I used VolkerK's code here php fopen returns false but file is readable/writable to ascertain the read/write permissions and get the following.
PHP Version: 5.4.9 Uname: Windows NT {File} exists {File} is readable {File} is writable Last error: array(4){["type']=>int(2)["message"]=>string(131 "fopen({file}):failed to open stream: Permission denied.
At first I thought it was a file permissions problem but I think if the file is being seen by PHP as writable this should not be the issue, do I read that correctly?
最满意答案
试过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); } }Tried FTP and got there. Had to properly set context options for FTP connection though.
Thanks for pointing me in the right direction.
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); } }更多推荐
发布评论