本來想用PHP 執行Linux sed插入文檔 簡單方便,但是目前php無權限執行shell命令。請問,用php代碼怎么實現該要求?
2 回答

瀟湘沐
TA貢獻1816條經驗 獲得超6個贊
//a.txtaaa bbb//add text hereccc
$need_add_text = '1111111xxxxxx';$text = file_get_contents('a.txt');$text_new = str_replace('//add text here',"//add text here\n".$need_add_text,$text);file_put_contents('a.txt',$text_new);

慕神8447489
TA貢獻1780條經驗 獲得超1個贊
$handle = fopen('log.txt', 'r+');$i = -1;$lastLine = '';while(true){ fseek($handle, $i, SEEK_END); $char = fgetc($handle); if($char == "\n"){ fwrite($handle, "new line \n". $lastLine); exit(); }else{ $lastLine .= $char; } $i --; }
編輯:$lastLine 順序應該錯了,不過很好修改,就不修改源代碼了,還有一些校驗也沒做,主要就針對樓主需求做個簡單示例。關鍵函數是:fseek
- 2 回答
- 0 關注
- 1304 瀏覽
添加回答
舉報
0/150
提交
取消