创建文件出错
Error by creating file
我在使用此 php 代码创建文件时遇到错误:
$userdbfile = file('userfiles/' . $steamprofile['steamid'] . '.txt');
$fhuserdb = fopen($userdbfile, 'a');
fwrite($fhuserdb, $steamprofile['steamid']);
fwrite($fhuserdb, "0");
close($fhuserdb);
header("Location: index.html");
exit;
错误:
Warning: file(userfiles/76561198043436466.txt): failed to open stream: No such file or directory in /home/u294343259/public_html/admin/lw/login.php on line 7
Warning: fopen(): Filename cannot be empty in /home/u294343259/public_html/admin/lw/login.php on line 12
Warning: fwrite() expects parameter 1 to be resource, boolean given in /home/u294343259/public_html/admin/lw/login.php on line 13
Warning: fwrite() expects parameter 1 to be resource, boolean given in /home/u294343259/public_html/admin/lw/login.php on line 14
Fatal error: Call to undefined function close() in /home/u294343259/public_html/admin/lw/login.php on line 15
file()
doesn't create a new file! It only reads file. So just remove it and use fopen()
,像这样
$userdbfile = 'userfiles/' . $steamprofile['steamid'] . '.txt';
$fhuserdb = fopen($userdbfile, 'a');
//checks that the file was successfully opened
if($fhuserdb) {
fwrite($fhuserdb, $steamprofile['steamid']);
fwrite($fhuserdb, "0");
fclose($fhuserdb);
}
//^ The function is 'fclose()' not 'close()' to close your file
header("Location: index.html");
exit;
- 还要确保该文件夹确实具有适当的写入权限。
阅读手册:
- the
file
function 将整个文件读入数组。第一个警告告诉您请求的文件 不存在
- the
fopen
function 期望第一个参数是一个字符串,file
returns 一个数组或者 false
失败。 fopen
的第一个参数应该是一个字符串,指定要打开的文件的路径。它 return 是一个资源(文件句柄)或 false
失败时
fwrite
希望你传递一个 有效的 文件句柄,你不检查 fopen
的 return 值(这是false
在你的情况下),所以你不是在写一个实际的文件
close
不存在,fclose
does,同样:这需要在 有效 文件句柄上调用,你没有,因此这条线也将失败
- the
header
function 只有在未发送输出时才能调用(请仔细阅读 描述 下方的位),您的代码正在生成警告和错误,从而产生输出.因此,调用 header
为时已晚
那现在呢?
将您传递给 file
的路径传递给 fopen
,检查其 return 值并相应地继续:
$userdbfile = 'userfiles/' . $steamprofile['steamid'] . '.txt';
$fh = fopen($userdbfile, 'a');
if (!$fh)
{//file could not be opened/created, handle error here
exit();
}
fwrite($fh, $steamprofile['steamid']);
fwrite($fh, '0');
fclose($fh);
header('Location: index.html');
exit();
我在使用此 php 代码创建文件时遇到错误:
$userdbfile = file('userfiles/' . $steamprofile['steamid'] . '.txt');
$fhuserdb = fopen($userdbfile, 'a');
fwrite($fhuserdb, $steamprofile['steamid']);
fwrite($fhuserdb, "0");
close($fhuserdb);
header("Location: index.html");
exit;
错误:
Warning: file(userfiles/76561198043436466.txt): failed to open stream: No such file or directory in /home/u294343259/public_html/admin/lw/login.php on line 7
Warning: fopen(): Filename cannot be empty in /home/u294343259/public_html/admin/lw/login.php on line 12
Warning: fwrite() expects parameter 1 to be resource, boolean given in /home/u294343259/public_html/admin/lw/login.php on line 13
Warning: fwrite() expects parameter 1 to be resource, boolean given in /home/u294343259/public_html/admin/lw/login.php on line 14
Fatal error: Call to undefined function close() in /home/u294343259/public_html/admin/lw/login.php on line 15
file()
doesn't create a new file! It only reads file. So just remove it and use fopen()
,像这样
$userdbfile = 'userfiles/' . $steamprofile['steamid'] . '.txt';
$fhuserdb = fopen($userdbfile, 'a');
//checks that the file was successfully opened
if($fhuserdb) {
fwrite($fhuserdb, $steamprofile['steamid']);
fwrite($fhuserdb, "0");
fclose($fhuserdb);
}
//^ The function is 'fclose()' not 'close()' to close your file
header("Location: index.html");
exit;
- 还要确保该文件夹确实具有适当的写入权限。
阅读手册:
- the
file
function 将整个文件读入数组。第一个警告告诉您请求的文件 不存在 - the
fopen
function 期望第一个参数是一个字符串,file
returns 一个数组或者false
失败。fopen
的第一个参数应该是一个字符串,指定要打开的文件的路径。它 return 是一个资源(文件句柄)或false
失败时 fwrite
希望你传递一个 有效的 文件句柄,你不检查fopen
的 return 值(这是false
在你的情况下),所以你不是在写一个实际的文件close
不存在,fclose
does,同样:这需要在 有效 文件句柄上调用,你没有,因此这条线也将失败- the
header
function 只有在未发送输出时才能调用(请仔细阅读 描述 下方的位),您的代码正在生成警告和错误,从而产生输出.因此,调用header
为时已晚
那现在呢?
将您传递给 file
的路径传递给 fopen
,检查其 return 值并相应地继续:
$userdbfile = 'userfiles/' . $steamprofile['steamid'] . '.txt';
$fh = fopen($userdbfile, 'a');
if (!$fh)
{//file could not be opened/created, handle error here
exit();
}
fwrite($fh, $steamprofile['steamid']);
fwrite($fh, '0');
fclose($fh);
header('Location: index.html');
exit();