虽然文件检查为可写,但PHP fopen不起作用(PHP fopen doesn't work although file checks as writable)

编程入门 行业动态 更新时间:2024-10-27 07:31:23
虽然文件检查为可写,但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 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); } }

更多推荐

本文发布于:2023-08-05 02:19:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1426159.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不起作用   文件   fopen   PHP   writable

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!