如何将$ _GET数据保存到PHP文件中

如何将$ _GET数据保存到PHP文件中

问题描述:

I'm trying to save all key values from the GET parameters, but its not writing anything to the file.

foreach ($_GET as $key => $value) {
   $contents = $key . " => " . $value . "<br>";
   echo($contents);
   file_put_contents("./test.log", $contents, FILE_APPEND);
}

Don't use file_put_contents() inside loop. Put it outside:-

$contents='';
foreach ($_GET as $key => $value) {
    $contents .= $key . " => " . $value . "
"; // or use `"
"`            
}
file_put_contents("./test.log", $contents, FILE_APPEND);

Note:- Check that file have write permission (644) + folder in which this file lies have the permission too (777) and path of the file is correct.

Below are the screen-shots of working code at my local end:- http://prntscr.com/e98o04 And http://prntscr.com/e98oco

AS i seen the save data in file is in every iteration an only at iteration current position:

$contents='';
foreach ($_GET as $key => $value) {
    $contents.= $key . " => " . $value . "<br>";            
}
file_put_contents("./test.log", $contents, FILE_APPEND);

Same path of php code?, not needed "./", could you try to open file, before and put here if there is no error.

print_r will give you the same output you are trying to build, and you can solve this using just one line.

file_put_contents("./test.log", print_r($_GET, true), FILE_APPEND);