使用fopen() PHP函数处理文件


handling files using fopen() php function

我得到一个页面的地址,并通过html形式将其发送到php页面,然后我想打开这个文件,并通过fopen()函数向它写入一些文本。
我的代码:

$open=fopen($subject,"a");  
//$subject is a variable that get the address of page that html for is in.  
// <input type=" text" name="subject" id="f-subject" value=" <?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" />
fwrite($open,"Hello World !");
fclose($open);

现在这些代码不工作,我得到错误的资源管理器。

警告:fopen(../../Comments/HTML-Learning/chapter1.html): failed to open stream: No such file or directory in ......

有什么建议吗?

您可以使用fopen($path,"flag");

使用相对路径或绝对路径

可以验证php $subjet处理程序php文件是否接收到数据,以及如何在该文件中调用处理程序函数或在另一个文件上,在这种情况下使用include或require方法来做到这一点?

所以在你的代码$open=fopen($subject,"a");中,$subject代表了一个文件本地化的路径,如"ftp","http","本地路径",你试图在$subject本地化上存储数据,你的文件本地化绝对路径必须是这样的:

$subjet = "/home/rasmus/fichero.txt";
$subjet = "http://www.example.com/";
$subjet = "ftp://user:password@example.com/fichero.txt";

如果你使用相对路径进行本地化你的文件本地化相对路径必须像这样:

$subjet = "/home/rasmus/../fichero.txt"; //One folder up of your scipt path example
$subjet = "/home/rasmus/../../fichero.txt"; //Two folders up of your script path example

如果您试图使用HTTP路径来存储文件,则需要在目标服务器HTTP地址中使用另一个处理程序

在https://www.php.net/manual/es/function.fopen.php阅读更多