fopen不能与r php有所不同 [英] fopen doesn´t work with something different than r php

查看:87
本文介绍了fopen不能与r php有所不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对fopen函数和打开模式参数有疑问.

I have a problem with the fopen function and the opening mode argument.

function writeOnFile($url, $text)
{
    $fp = fopen($url, "r");
    echo "<br>read: ".fgets($fp);
    //fwrite($fp, $text);
    fclose($fp);
}

如果我使用"r"作为打开模式,则回显行有效……但是,如果我更改此参数为其他任何参数(使用相同的url文件),它将停止工作,并且仅显示"read:",而没有其他显示. 我尝试使用"w +","r +","a +" ...,但是没人能用.
我想读取的是一个txt文件,我更改了该文件的权限,现在是777 ...

If I use "r" as the opening mode the echo line works... but if I change this argument for any other (using the same url file) it stops working and only see "read:" and nothing else. I tried with "w+" "r+" "a+"... but no one works.
What I am trying to read is a txt file and I changed the permissions of the file and now are 777...

我在做什么错了?

推荐答案

给出变量命名,$url建议您尝试写入http://example.com/....这是不可能的.您无法写入" URL,因为PHP绝对知道远程服务器需要什么协议.例如.出于某些奇迹,PHP决定允许该URL通过,并使用http POST ...,但是服务器期望使用http PUT.糟糕,这行不通.

Given your variable naming, $url suggests you're trying to write to a http://example.com/.... This is not possible. You cannot "write" to a url, because PHP has absolutely NO idea what protocol the remote server is expecting. E.g. PHP by some miracle decides to let this URL through, and uses http POST... but the server is expecting an http PUT. Ooops, that won't work.

同样,永远不要假设对外部资源的操作成功了.始终承担失败,并将成功视为惊喜:

As well, never EVER assume an operation on an external resource succeeded. Always assume failure, and treat success as a pleasant surprise:

function writeOnFile($url, $text)
   if (!is_writeable($url)) {
      die("$url is not writeable");
   }
   $fp = fopen($url, "w");
   if (!$fp) {
      die("Unable to open $url for writing");
   }
   etc...
}

这篇关于fopen不能与r php有所不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆