duangang79177 2018-11-17 05:19
浏览 147
已采纳

WordPress:wp_filesystem-> put_contents只写最后一次调用

I have a WordPress issue and want to simply write log messages to a text file. I am aware that error_log exists, but want to have a more segregated log file for different messages.

I am using wp_filesystem->put_contents, and it DOES write to the file and succeeds, but it ONLY outputs the last call's data.

I have the following method:

public static function log_message($msg) {
  error_log($msg);
  require_once(ABSPATH . 'wp-admin/includes/file.php');
  global $wp_filesystem;
  if ( ! is_a( $wp_filesystem, 'WP_Filesystem_Base') ){
      $creds = request_filesystem_credentials( site_url() );
      wp_filesystem($creds);
  }

  $bt = debug_backtrace();
  $caller = array_shift($bt);
  $logStr = date("Y-m-d hh:ii A",time())." - ".$caller['file'].":".$caller['line']." - ".$msg;
  $filePathStr = SRC_DIR.DIRECTORY_SEPARATOR.$logFileName;

  $success = $wp_filesystem->put_contents(
      $filePathStr,
      $logStr,
      FS_CHMOD_FILE // predefined mode settings for WP files
  );

  if(!$success) {
      error_log("Writing to file \"".$filePathStr."\" failed.");
  } else {
      error_log("Writing to file \"".$filePathStr."\" succeeded.");
  }
}

I call it using:

log_message("
Test 1");
log_message("
Test 2");
log_message("
Test 3");

The output is ALWAYS ONLY Test 3 with the other invocations being ignored yet, their output appears in the debug.log as well as all the success messages.

Why would this be?

Looking at the WPCodex for the source code of this, it uses fwrite behind the scenes. The file is closed in this code, and I cannot use any "flush" technique.

Is there a way to figure this out?

  • 写回答

1条回答 默认 最新

  • doutan2228 2018-11-18 04:33
    关注

    I found that the source of WP_Filesystem uses file_put_contents (as the name does suggest), and I assumed this is for APPENDING to the file's data.

    This is incorrect.

    This function is to take data, and then WRITE it to the file, erasing prior data. Mainly useful for creating resources, downloading a file, etc.

    If I want to APPEND to a file, I need to use 'fwrite'.

    This post describes that.

    This is the example to APPEND to a file:

    $filepath = '\path\to\file\';
    $filename = 'out.log';
    $fullpath = $filepath.$filename;
    
    if(file_exists($fullpath)) {
      $file = fopen($filepath.$filename, "a");//a for append -- could use a+ to create the file if it doesn't exist
      $data = "test message";
      fwrite($file, "
    ". $data);
      fclose($file);
    } else {
      error_log("The file \'".$fullpath."\' does not exist.");
    }
    

    The fopen docs describe this method and it's modes.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题