PHP代码段无法创建文件

PHP代码段无法创建文件,php,html,forms,file-handling,Php,Html,Forms,File Handling,我有一个带有action=“file.php”和method=“post”的HTML表单 但此“file.php”无法创建文件: <?php $my_file = 'username.txt'; $handle = fopen($my_file,'w'); foreach($_POST as $variable => $value) { if($variable == 'email') { fwrite($handle, $variable);

我有一个带有action=“file.php”和method=“post”的HTML表单

但此“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;
?> 


但这可能是个问题吗

尝试使用此选项,确保您对该文件具有写入权限

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


您应该检查您的
fopen
呼叫是否成功。你也应该在这里发布你收到的任何错误消息。没有收到任何错误消息,但文件“username.txt”只是没有创建,我之前没有php方面的知识,只是在处理一个项目,但是要确保路径是可写的,并且你对目录有写权限如何授予它写权限?
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!");
}