php 如何正确使用php fopen()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2644861/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use the php fopen() correctly
提问by Hymansta
I am learning php, trying to use the fopen()function.
The php file I am coding is in this directory /domains/xxxxx.com.au/public_html/phpfile.phpWhat path do I specify for the file to be opened, the example I am looking at is based on a server on a pc where this is the file path $filename = "c:/newfile.txt";not an online server.
我正在学习php,尝试使用该fopen()功能。我正在编码的 php 文件在这个目录中/domains/xxxxx.com.au/public_html/phpfile.php我指定要打开的文件的路径是什么,我正在查看的示例基于 PC 上的服务器,其中这是文件路径$filename = "c:/newfile.txt";而不是在线服务器。
UPDATE!
更新!
This is the whole script, I have the file location correct, now the4 script is returning "couldnt create the file" does this have something to do with ther permission of the folder location of the file?
这是整个脚本,我的文件位置正确,现在 4 脚本返回“无法创建文件”这与文件文件夹位置的权限有关吗?
<?php
$filename = "/domains/xxxxxxxx.com.au/public_html/newfile.txt";
$newfile = @fopen($filename, "w+") or die ("couldnt create the file");
fclose($newfile);
$msg = "<p>File Created</p>";
?>
<HTML>
<HEAD>
</HEAD>
<BODY>
<? echo "$msg" ?>
</BODY>
</HTML>
采纳答案by Simone Margaritelli
Assuming that your php file is inside the public_html too, you could use :
假设您的 php 文件也在 public_html 中,您可以使用:
$fp = fopen( "newfile.txt", "rt" );
Or, giving the full path :
或者,给出完整路径:
$fp = fopen( "/domains/xxxxx.com.au/public_html/newfile.txt", "rt" );
This will open it if it already exists.
如果它已经存在,这将打开它。
Refer to thisfor further details of opening flags.
请参阅此开旗的进一步细节。
UPDATE: You can even use the is_writable/is_readablefunction to check file access before trying to open it.
更新:您甚至可以在尝试打开文件之前使用is_writable/ is_readable函数来检查文件访问。
回答by David Baucum
Read http://us2.php.net/manual/en/function.fopen.phpExample #1 is relevant for a Unix system.
阅读http://us2.php.net/manual/en/function.fopen.phpExample #1 与 Unix 系统相关。

