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

时间:2018-01-01 16:10:55

标签: php

evryone。我正在尝试创建一个基本表单来保存文本文件中的姓氏和姓氏,所以当我必须登录时,我将从txt文件中检索它们,但显然我对txt文件的目录做错了< / p>

现在我更新了代码,我遇到了这个安全性错误

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

   <?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 个答案:

答案 0 :(得分:0)

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

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

到这个

$file = 'students.txt';
if (is_file($file)){