简体   繁体   English

file_put_content资源ID#233

[英]file_put_content Resource id #233

$data = json_encode($data, JSON_UNESCAPED_UNICODE);

$log = base_path()."/storage/logs/trade.log";

if ( !file_exists ($log) ) {
    $data = fopen($log, "w");
}

file_put_contents($log, $data . PHP_EOL.PHP_EOL, FILE_APPEND);

I have a page use file_put_contents to record log. 我有一个页面使用file_put_contents记录日志。 when my folder don't exist file, it will auto create the file and add log into it 当我的文件夹不存在文件时,它将自动创建文件并向其中添加日志

My problem is when first time auto create file and place content into it. 我的问题是第一次自动创建文件并将内容放入其中时。 It comes out - Resource id #233 出来了-资源ID#233

2nd time without crate file will be normal no any problem 第二次没有板条箱文件将是正常的,没有任何问题

anyone know how to fix this? 有人知道怎么修这个东西吗?

fopen() returns a resource . fopen()返回一个resource

file_put_contents() write your file and converts $data into a string (calls __toString() on the resource ). file_put_contents()写入文件并将$data转换为字符串(在resource上调用__toString() )。

The second times, the file exists. 第二次,文件存在。 So, the program doesn't pass into the if-condition and it writes $data that contains JSON. 因此,该程序不会传递到if条件,而是写入包含JSON的$data

To solve your problem, just remove your if block. 要解决您的问题,只需删除if块。

Documentation said : 文件说:

If filename does not exist, the file is created. 如果文件名不存在,则创建文件。 Otherwise, the existing file is overwritten, unless the FILE_APPEND flag is set. 否则,除非设置了FILE_APPEND标志,否则现有文件将被覆盖。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM