Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php fwrite/fclose警告_Php_Fwrite_Fclose - Fatal编程技术网

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);