【问题标题】:Wordpress File handle - fopen, fwrite not working with $.ajax or $.post JqueryWordpress 文件句柄 - fopen、fwrite 不适用于 $.ajax 或 $.post Jquery
【发布时间】:2016-04-14 10:03:38
【问题描述】:

我正在为站点中完成的所有操作制作一些日志,可以存储在 .txt 文件中。

我的 php 代码在正常加载时工作正常,但是当我使用 ajax 时,似乎我没有阅读 php 上的 fopen、fpost 方法..

请检查我的代码。

这是我的函数和 ajax 处理程序

function action_log( $msg ) {
    $current_user = wp_get_current_user();
    $path = "wp-content/themes/creation/templates/inc/log.txt";
    $myfile = (file_exists($path)) ? fopen($path, "a+") : fopen($path, "w+");
    $txt = $msg.$current_user->display_name."\n";

    if ( $myfile ) {
        fwrite( $myfile, $txt );
        fclose( $myfile );
        chmod($path, 0777);
        $result = $txt;
        return $result;
    } else {
        $result = $myfile;
        return $txt;
    }
}

add_action('wp_ajax_action-log-ajax', 'action_log_ajax');
add_action('wp_ajax_nopriv_action-log-ajax', 'action_log_ajax');
function action_log_ajax(){

    $post = $_REQUEST;
    $msg = $post['msg'];
    $write = action_log( $msg );

    exit( wp_send_json_success( $write ) );
}

我的 jquery 插件功能

  $.send_log_changes = function( $msg ) {

    var msgs = $msg;
    var action = 'action-log-ajax';
    console.log(msgs);

    $.post({
      url: plugins_ajax.ajaxUrl,
      method: 'POST',
      data: {
        action: action,
        msg: msgs
      },
      dataType: 'json',
      success: function( respones ) {
        console.log(respones);
      }

    });
 }

我尝试使用 .ajax、.get、.post 仍然是同样的错误。 它总是返回False并且不能在文件中写入/打开。

【问题讨论】:

    标签: php jquery ajax wordpress fopen


    【解决方案1】:

    文件打开操作似乎返回 false。您是否尝试过在 $path 变量中输入完整路径,即 /var/www/html/demo/wp-content/themes/creation/templates/inc/log.txt" 等。

    【讨论】:

    • 路径没有问题。当站点完全加载时,路径正在工作,但使用 ajax 它无法正常工作..而且我尝试了完整的路径和相同的结果.. 无论如何感谢您的回答,仍在寻找一个好的解决方案。
    猜你喜欢
    • 1970-01-01
    • 2017-02-07
    • 1970-01-01
    • 2011-07-08
    • 2013-02-20
    • 2014-02-19
    • 2015-06-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多