我正在学习php中的文件处理。我编写了以下代码来创建文件:
<?php
$fileName = "testFile.txt";
$fileHandle = fopen($fileName,"w") or die ("can't open file");
fclose($fileHandle);
phpinfo();
?>
问题是我得到“无法打开文件”。我将包含此文件的目录的权限和目录中的所有文件更改为777仍然存在问题。 有人可以帮我解决这个问题吗? 感谢
答案 0 :(得分:1)
您是否尝试过使用$ filename
的绝对路径$fileName = "/path/to/file/testFile.txt";
或者您可以在打开文件之前更改dir
chdir('/path/to/file');
http://nz.php.net/manual/en/function.chdir.php
修改答案,试试这个
<?php
$path = "/path/to/file";
$fileName = "testFile.txt";
if (! file_exists($path)) {
die ("$path doesn't exist");
}
$fileHandle = fopen("$path/$fileName","w") or die ("can't open file");
fclose($fileHandle);
phpinfo();
答案 1 :(得分:1)
是同一个文件夹中的php脚本和文件吗? 如果不是,则需要指定testFile.txt的相对路径
答案 2 :(得分:0)
尝试:
$fileHandle = fopen( dirname(__FILE__) . '/' . $fileName , 'w' ) ....