【问题标题】:PHP code snippet is not able to create filePHP代码段无法创建文件
【发布时间】:2018-07-04 03:42:46
【问题描述】:

我有一个 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;
?> 

但可能是问题所在?

【问题讨论】:

  • 您应该检查您的fopen 调用是否成功。您还应该在此处发布您收到的任何错误消息。
  • 没有收到任何错误消息,但文件 'username.txt' 只是没有创建,
  • 我对php没有先验知识,只是在做一个项目
  • 愚蠢的疏忽,但要确保路径是可写的,并且你对目录有写权限
  • 如何给它写权限?

标签: php html forms file-handling


【解决方案1】:

尝试使用这个,确保你对文件有写权限。

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

【讨论】:

    【解决方案2】:
    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!");
    }
    

    【讨论】:

      猜你喜欢
      • 2018-11-24
      • 2013-07-10
      • 1970-01-01
      • 1970-01-01
      • 2018-05-26
      • 1970-01-01
      • 2012-11-02
      • 2011-04-01
      • 1970-01-01
      相关资源
      最近更新 更多