Php fwrite/fclose警告
顺便说一句,我有以下代码片段:Php fwrite/fclose警告,php,fwrite,fclose,Php,Fwrite,Fclose,顺便说一句,我有以下代码片段: $txt = "<?php include 'work/uploads/".$php_id.".html';?>"; $slot = file_put_contents('../offer/slots.php', $txt.PHP_EOL , FILE_APPEND); fwrite($slot, $txt); fclose($slot); $theCounterFile = "../offer/count.txt"; $oc
$txt = "<?php include 'work/uploads/".$php_id.".html';?>";
$slot = file_put_contents('../offer/slots.php', $txt.PHP_EOL , FILE_APPEND);
fwrite($slot, $txt);
fclose($slot);
$theCounterFile = "../offer/count.txt";
$oc = file_put_contents($theCounterFile, file_get_contents($theCounterFile)+1);
fwrite($oc);
fclose($oc);
也许我的逻辑是错误的。也许有人可以在这里透露一些信息?
file\u put\u contents
一次完成打开、写入和关闭所有操作–之后不需要调用fwrite
和fclose
。(不仅不需要,甚至没有任何意义,因为对于文件内容
来说,您甚至连一个文件句柄都没有。)
file\u put\u contents
返回写入的字节数,一个整数值–这就是为什么会出现这些警告。使用file\u put\u contents()时,您根本不需要fwrite()
或fclose()
。发件人:
此函数与依次调用fopen()
、fwrite()
和fclose()
将数据写入文件相同
您的代码应该如下所示:
$file = fopen("../offer/work/uploads/".$php_id.".html","w");
fwrite($file,$data); // Note: you could use file_put_contents here, too...
fclose($file);
$txt = "<?php include 'work/uploads/".$php_id.".html';?>";
$slot = file_put_contents('../offer/slots.php', $txt.PHP_EOL , FILE_APPEND);
$theCounterFile = "../offer/count.txt";
$oc = file_put_contents($theCounterFile, file_get_contents($theCounterFile)+1);
$file=fopen(“../offer/work/uploads/”$php_id..html”,“w”);
fwrite($file,$data);//注意:您也可以在此处使用文件内容。。。
fclose($文件);
$txt=”“;
$slot=file\u put\u contents(“../offer/slots.php”,$txt.php\u EOL,file\u APPEND);
$theCounterFile=“../offer/count.txt”;
$oc=文件内容($counter文件,文件内容($counter文件)+1);
至于当前代码出现错误的原因:fwrite()
和fclose()
希望第一个参数是资源(从fopen()
获得的返回值类型)。但是您正在向它们传递由file\u put\u contents()
返回的值,它是一个整数。所以,您得到了一个错误。第81、82、85和86行是什么?是b/c您错误地使用了这些函数。在手册的第一行之后查找这个应该类似于if(!$file)die('Failed to write../offer/work/uploads/'.$php_id..html')
@ekerner这会使代码变得更好,但我并没有试图在这里为OP编写更好的代码;我试图演示OP问题的解决方案。
$file = fopen("../offer/work/uploads/".$php_id.".html","w");
fwrite($file,$data); // Note: you could use file_put_contents here, too...
fclose($file);
$txt = "<?php include 'work/uploads/".$php_id.".html';?>";
$slot = file_put_contents('../offer/slots.php', $txt.PHP_EOL , FILE_APPEND);
$theCounterFile = "../offer/count.txt";
$oc = file_put_contents($theCounterFile, file_get_contents($theCounterFile)+1);