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 我的b站在没有碰到屏幕的情况下偶尔会自动跳出进度条,就像在屏幕上点了一下一样,但我并没有点。而且视频进度并没有变。这可能是什么原因造成的?
  • ¥30 STK matlab python仿真
  • ¥15 关于IMageEnView 图标定位问题
  • ¥20 求解答(matlab)
  • ¥30 ffmpeg库使用过程中遇到的问题
  • ¥15 pyqt5 中python如何通过Qtwebchannel主动发消息给web前端
  • ¥15 关于HTML中title获取xml内容的问题
  • ¥15 fanuc机器人PRIO083数字信号未复原错误,如何解决?
  • ¥20 如何为现有电路板增加远程控制功能
  • ¥15 UE5打包失败,求解决