当前位置: 首页 > 知识库问答 >
问题:

带有txt文件的php基本表单找不到目录

白宏放
2023-03-14

埃弗里翁。我正在尝试创建一个基本表单,将姓氏和姓氏保存在一个文本文件中,因此当我必须登录时,我将从txt文件中检索它们,但显然我对txt文件的目录做了一些错误

现在我更新了代码,出现了这个安全错误

警告:fopen(/home/sbau01/public_www/php/fma/data/data.txt):无法打开流:在/home/sbau01/public_www/php/fma/form中的权限被拒绝。php第66行注意:未定义索引:fName in/home/sbau01/public_www/php/fma/form。php第67行注意:未定义索引:sName in/home/sbau01/public_www/php/fma/form。php第67行警告:fwrite()希望参数1是资源,布尔值在/home/sbau01/public_www/php/fma/form中给出。php第68行警告:fclose()希望参数1是资源,布尔值在/home/sbau01/public_www/php/fma/form中给出。php在线76!

   <?php
    session_start();
    echo "<a href = 'logout.php'>logout</a>";
    ?>
     <!DOCTYPE html>
     <html lang="en">
     <body>
    <?php
    include ('includes/header.php');
    require_once 'includes/function.php';
    ?>
        <div class="nav">
            <?php
            include ('includes/menu.php');
            ?>
        </div>
        <footer>
            <?php include ('includes/footer.php');?>
        </footer>
    </body>
    </html>
    <?php
    $output ="";
    $form_is_submitted = false;
    $errors_detected = false;

    $clean = array();
    $errors = array();
    $nameArray = array();
    if(isset($_POST['SUBmit'])){
    $form_is_submitted = true;
    if (isset($_POST['fName'])) {
        if (empty($_POST['fName'])){
            $errors_detected = true;
            $errors = 'First name not entered';
            }else {
                if(ctype_alpha($_POST['fName'])){
                        $clean['fName'] = trim($_POST['fName']);
                    }else {
                        $errors_detected = true;
                        $errors = 'First name must contain alphabetical  letters only';
                    }
                }
            }
     if (isset($_POST['sName'])) {
        if (empty($_POST['sName'])){
            $errors_detected = true;
            $errors = 'Surname name not entered';
            }else {
                if(ctype_alpha($_POST['sName'])){
                        $clean['sName'] = trim($_POST['sName']);
                    }else {
                        $errors_detected = true;
                        $errors = 'Surname name must contain alphabetical letters only';
                    }
                }
            }


     }if($form_is_submitted === true && $errors_detected === false){
       $dir = '/home/sbau01/public_www/php/fma/data';
if (is_dir($dir)){
    $handleDir = opendir('/home/sbau01/public_www/php/fma/data');
    while(false !==($file = readdir($handleDir))){
        $path = "/home/sbau01/public_www/php/fma/data/".$file;
        if(is_file($path)){
            $handle =  fopen($path, 'a');
            $text = $clean['fName'].' '.$clean['sName']. PHP_EOL;
            $result = fwrite($handle, $text);
                if($result === false){
                    $formsErorrs = '<p>Oops! data not written</p>';
                }
                else{
                    $formsErorrs = '<p>Thank you for register with us. Yours details has been saved </p>';
                    $fullName = htmlentities($clean['fName']).' '.htmlentities($clean['sName']);
                }
                fclose($handle);
            }
            else{
                $formsErorrs= '<p>Oops!!! file  has not been found >>>  '.$path.'</p>';
            }
        }closedir($handleDir);
        $output= '<p>Registration form has been submitted</p>';
    }else{
       $formsErorrs = '<p>Oops, the file has NOT been found!!!</p>'; 
    }
}
    else{
    $self = htmlentities($_SERVER['PHP_SELF']);
    $formsErorrs = '';
    if($form_is_submitted === true){
        foreach($errors as $r){
            $formsErorrs = $formsErorrs .'<p>'.$r.'</p>';                   
        }   
    }
    if (isset($clean['fName'])) {
        $firstN = htmlentities($clean['fName']);
    } 
    else {
        $firstN = '';
    }
    if (isset($clean['sName'])) {
        $lastN = htmlentities($clean['sName']);
    } 
    else {
        $lastN = '';
    }
    $output = '<form action="'.$self.'" method="post">
        <fieldset>
                <table>

                    <tr>
                    <td><label for="fn">First Name</label></td>
                    <td>&#42; <input type="text" name="fname" id="fn"   value="'.$firstN.'" /></td>
                    </tr>
                    <tr>
                    <td><label for="ln">Last Name</label></td>
                    <td>&#42; <input type="text" name="lname" id="ln" value="'.$lastN.'" /></td>
                    </tr>
                    <tr>
                    <td><input type="submit" name="SUBmit" value="SUBMIT DATA" /></td>
                    </tr>
                </table>
        </fieldset>
    </form><p>&#32;</p>';
}
   echo make_heading('Registration Form',3);
    echo '<div id="breadcrumbs"> <a title="Back to home page"   href="index.php?">         Back to home page</a></div>';
   echo $output;
    echo $formsErorrs;
    ?>

共有1个答案

戚升
2023-03-14

students.txt不是一个目录。我应该改变

$dir = 'students.txt';
if (is_dir($dir)){

对此

$file = 'students.txt';
if (is_file($file)){
 类似资料:
  • '未处理的异常: 系统。IO. FileNotFoundException:找不到文件"/Logins.txt"发生' 大家好,新手在xamarin c#中使用Systsem中的StreamReader创建了一个非常基本的登录/注册系统。IO读取包含存储用户名和密码的文本文件。txt文件与位于同一目录中。cs文件本身,并在解决方案资源管理器中可见。我尝试了完整的路径,但没有成功,我以管理员的身份运

  • 我有一个带有子目录的目录,其中包含各种大小的文件。我想复制一个特定的文件,并立即用所有文件替换它。 例如我有:C:\Folder\Sub_Folder\file1.extC:\Folder\Sub_Folder\file2.ext 我想制作“random_file.ext”的副本,然后将所有副本替换为上面提到的具有原始名称的文件file1.extfile2.ext这样random_file.ext

  • 问题内容: 我正在尝试创建文件并将其保存到站点的根目录,但是我不知道它在哪里创建文件,因为看不到任何文件。而且,如果可能的话,我需要每次都覆盖该文件。 这是我的代码: 如何设置保存在根目录下? 问题答案: 它在与脚本相同的目录中创建文件。试试这个吧。

  • 我在一天中做了一些API调用,但在使用Adobe Sign的新设置时遇到了问题。我可以得到请求很好,但得到HTTP404时,尝试任何张贴或放入他们的服务器。例如,到这里.... 当然,$HttpCode返回404,如上图所示。我为邮局做错什么了吗?

  • 所以我检查了gradlew是否在项目目录下,但我找不到它。但我仍然可以运行任务从‘分级项目’窗口。 怎么了?

  • 问题内容: 我有一个.txt文件,其中包含以下详细信息: 我想做的是解析此广告,使值以更易读的格式(如果可能)转换为数组。 谢谢 问题答案: 您可以这样轻松地做到这一点 首先,您可以使用函数打开文本文件,然后使用函数在换行符上剪切字符串。这样,您将获得一个数组,其中所有行都分开。然后使用该函数可以删除第一行,因为它是标题。 获取行之后,您可以遍历数组并将所有信息放入名为的新数组中。从零行开始,您将