在linux中通过php/shell脚本创建一个文件


Create a file in linux via php/shell script?

我试图创建一个php'shell脚本来创建一个文件。我使用exec()命令来创建shell和php本身之间的链接。我也试图从用户接收数据,我有一个php表单页面链接到脚本。

<?php
$username = $_POST['txt_username'];
exec("sudo echo $username > file.txt");
?>

根据我的研究,我发现exec()不接受$_POST或$_GET变量。我还尝试了extract(), getenv()和var_dump()等函数,以及escapeshellcmd()和escapeshelllang()。有人能帮帮我吗?

这是你要看的吗?

<?php
$uname = $_POST['UserName']; 
$file_to_write = "file.txt";
$open_file = fopen($file_to_write,'w') or die ("Cant Open File");
fwrite($file_to_write, $uname);
flcose($file_to_write);
?>

我会这样做。

<?php
    file_put_contents( 'file.txt', $_POST['txt_username'] );
?>