2016-04-14 16 views
0

我正在做一些日誌,記錄可以存儲在.txt文件中的網站中所做的所有動作。

在做一個正常負載時工作正常,我的PHP代碼,但是當我使用AJAX好像我不讀則fopen,在PHP 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); 
     } 

    }); 
} 

我試着用阿賈克斯,不用彷徨,和.post的仍是同樣的錯誤。 它總是返回False並且不能在文件中寫入/打開。

回答

0

看來,文件打開操作返回false。你有沒有嘗試在$ path變量中輸入完整路徑,例如/var/www/html/demo/wp-content/themes/creation/templates/inc/log.txt「等,

+0

沒有路徑問題。路徑正在工作時,該網站是完全加載,但與Ajax它不工作..也我試圖完整的路徑和相同的結果..反正謝謝你的答案,仍然在尋找一個好的解決方案。 –