略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: readline_write_history

2024-11-14

readline_write_history

(PHP 4, PHP 5, PHP 7, PHP 8)

readline_write_history写入历史记录

说明

readline_write_history(string $filename = ?): bool

这个函数将命令历史写入到一个文件.

参数

filename

保存文件的路径.

返回值

成功时返回 true, 或者在失败时返回 false

add a noteadd a note

User Contributed Notes 1 note

up
3
jonathan dot gotti at free dot fr
16 years ago
readline_write_history() doesn't take care of the $_SERVER['HISTSIZE'] value, here's an example on how to handle an history file in your apps taking care of user preferences regarding history size.

at the begining of your script:
<?php
$history_file
= $_SERVER['HOME'].'/.PHPinteractive_history';
# read history from previous session
if(is_file($history_file))
 
readline_read_history($history_file);
....
# your application's code
....
# put this at the end of yur script to save history and take care of $_SERVER['HISTSIZE']
if( readline_write_history($history_file) ){
 
# clean history if too long
 
$hist = readline_list_history();
  if( (
$histsize = count($hist)) > $_SERVER['HISTSIZE'] ){
   
$hist = array_slice($hist, $histsize - $_SERVER['HISTSIZE']);
   
# in php5 you can replaces thoose line with a file_puts_content()
   
if( $fhist = fopen($history_file,'w') ){
     
fwrite($fhist,implode("\n",$hist));
     
fclose($fhist);
    }
  }
}
?>

官方地址:https://www.php.net/manual/en/function.readline-write-history.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3