PHP代码段无法创建文件

时间:2018-07-04 03:42:47

标签: php html forms file-handling

我在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;
?> 

但是可能是问题所在吗?

2 个答案:

答案 0 :(得分:0)

尝试使用此功能,请确保您对该文件具有写权限。

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

答案 1 :(得分:0)

ini_set('error_reporting', E_ALL);

$fName = "members.txt";
$file = fopen($fName, "w");

if ($file !== false)
{
    foreach($_POST as $variable => $value)  
    {
        if($variable == 'email')
        {
            fwrite($file, $variable ."=". $value .PHP_EOL);
        }
    }
    fclose($file);
}
else
{
    Echo("The file ". $fName ." is unable to open!");
}