首页 > 解决方案 > PHP代码片段无法创建文件

问题描述

我有一个 HTML 表单,带有 action="file.php" 和 method="post"

但是这个“file.php”不能创建文件:

<?php
$my_file = 'username.txt';
$handle = fopen($my_file,'w');
foreach($_POST as $variable => $value)  
{
    if($variable == 'email')
    {
        fwrite($handle, $variable);
        fwrite($handle, "=");
        fwrite($handle, $value);
        fwrite($handle, "\r\n");
    }
}

fwrite($handle, "\r\n");
fclose($handle);
exit;
?> 

但可能是问题吗?

标签: phphtmlformsfile-handling

解决方案


尝试使用它,确保您对文件有写权限。

<?php
$myfile = fopen("username.txt", "w") or die("Unable to open file!");
$txt = (isset($_POST['email'])) ? $_POST['email'] : "";
fwrite($myfile, $txt);
fclose($myfile);
?>

推荐阅读