$data 以函式 print_r 輸出到檔案 /tmp/file.txt。
$output = print_r($data, true);
file_put_contents('/tmp/file.txt', $output);
[root@kvm3 html]# vi b.php
<?php
$dbhost = 'localhost:3306';
$dbuser = 'root';
$dbpass = '123123';
$conn = mysqli_connect($dbhost, $dbuser, $dbpass)
or die(mysqli_connect_error().PHP_EOL);
file_put_contents('/tmp/file.txt','Connected successfully'.PHP_EOL);
echo 'Connected successfully'.PHP_EOL;
mysqli_close($conn);
?>
[root@kvm3 html]# curl http://kvm3/b.php Connected successfully
file_put_contents() 顯示的 /tmp/file.txt,實際存在路徑是 /tmp/systemd-private-xxxxx-php-fpm.service-xxxx/tmp/file.txt。
[root@kvm3 html]# find /tmp -name file.txt /tmp/systemd-private-307efe49f6bb40e68baa0cf476a8f4fe-php-fpm.service-nTHwB5/tmp/file.txt