如何正确使用php fopen()

时间:2010-04-15 11:40:58

标签: php

我正在学习php,试图使用fopen()函数。 我编码的php文件位于此目录/domains/xxxxx.com.au/public_html/phpfile.php 我要为要打开的文件指定什么路径,我正在查看的示例基于PC上的服务器,其中这是文件路径$filename = "c:/newfile.txt";而不是在线服务器。

UPDATE!

这是整个脚本,我的文件位置正确,现在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>

2 个答案:

答案 0 :(得分:4)

假设你的php文件也在public_html中,你可以使用:

$fp = fopen( "newfile.txt", "rt" );

或者,给出完整的路径:

$fp = fopen( "/domains/xxxxx.com.au/public_html/newfile.txt", "rt" );

如果它已经存在,将打开它。

有关打开标志的更多详细信息,请参阅this

更新: 在尝试打开文件之前,您甚至可以使用is_writable / is_readable功能检查文件访问权限。

答案 1 :(得分:1)

阅读http://us2.php.net/manual/en/function.fopen.php示例#1与Unix系统相关。