如何检查文本框中的名称是否已经作为localhost server (PHP)中的文件夹存在,而不使用mysql

时间:2022-10-07 04:27:54
form.php

<?php
// define variables and set to empty values
$nameErr = $ageErr = $qualErr = $mobileErr = "";
$name = $age = $qual = $mobile = "";
$error="";

if ($_SERVER["REQUEST_METHOD"] == "POST")
{
   if (empty($_POST["age"]))
     {$ageErr = "Age is required";}
   else
     {$age = test_input($_POST["age"]);}

   if (empty($_POST["qual"]))
     {$qualErr = "Qualification is required";}
   else
     {$qual = test_input($_POST["qual"]);}

   if (empty($_POST["mobile"]))
     {$mobileErr = "mobile Number is required";}
   else
     {$mobile = test_input($_POST["mobile"]);}
     if (empty($_POST["name"]))
     {$nameErr = "Name is required";}
   else
     {$name = test_input($_POST["name"]);
     if(file_exists($name)!=$name)
     {
    $ab=mkdir($name,0777);//creating folder in server side, entered name as folder name
    $c=realpath($name);//created folder path
    $d=$name.".txt";//creating text file which should come under folder with the same name 
    $sam="$c/".$d;
    $a=fopen($sam,'w+');
    $space="\r\n";
    $details=$name.$space.$age.$space.$qual.$space.$mobile;
    fwrite($a,$details);//inside text file information is stored

    }

     else
     {
         echo $name."\talready exists";

     }
     }

}
function test_input($data)
{

     $data = trim($data);
     $data = stripslashes($data);
     $data = htmlspecialchars($data);

     return $data;
}
?>
-------------------------------------------------------------------------------------------
form.html


<html>
 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script> 
    <script src="http://malsup.github.com/jquery.form.js"></script> 
 <script>
 $(document).ready(function()
 {
         $('#sub').click(function()
         {
             $("#formajax").submit(function(e)
         {
        e.preventDefault();
        dataString=$("#formajax").serialize();


$.ajax({
type: "POST",
url:"form.php",
data:dataString,

success:function(data){
    alert("success");

},
error:function(data){
    alert("name already exists as a folder");
    }

})
return false;

});
});
});

</script>    
<style>
.error {color: #FF0000;}
</style>
</head>

<body>
<form id="formajax"  action="" method="post" enctype="multipart/form-data">
<pre>
NAME         :<input type="text" name="name" id="name"><span class="error">*<?php echo $nameErr;?></span><br/>
AGE          :<input type="text" name="age" id="age"><span class="error">*<?php echo $ageErr;?></span><br/>
QUALIFICATION:<input type="text" name="qual" id="qual"><span class="error">*<?php echo $qualErr;?></span><br/>
MOBILE       :<input type="text" name="mobile" id="mob"><span class="error">*<?php echo $mobileErr;?></span>
<br/>

              <input id="sub" type="submit" value="submit" name="submit">
<pre>
</form>
</body>

when i execute this code,after entering details folder is created inside the folder text file is created,inside text file information is stored...both folder and text file name is created with user entered name..if it already exists then it doesnot created with that name....now i'm getting alert message like success even name is alredy exits.. i cant get name already exists message through alert.. pl find out the mistake and reply thanks

当我执行这段代码时,输入details文件夹在文件夹文本文件中创建,在文本文件信息中存储…文件夹和文本文件名都是用用户输入的名称创建的。如果它已经存在,那么它不使用该名称创建....现在我收到了成功之类的警告信息,连名字都有。我不能通过警报得到已经存在的消息。找出错误并回复谢谢

1 个解决方案

#1


3  

File exists function is called incorrectly (it returns boolean)

文件存在函数调用不正确(返回布尔值)

http://php.net/manual/en/function.file-exists.php

http://php.net/manual/en/function.file-exists.php

if(file_exists($name)!=$name)

should be replaced with

应该换成

if(!file_exists($name))

#1


3  

File exists function is called incorrectly (it returns boolean)

文件存在函数调用不正确(返回布尔值)

http://php.net/manual/en/function.file-exists.php

http://php.net/manual/en/function.file-exists.php

if(file_exists($name)!=$name)

should be replaced with

应该换成

if(!file_exists($name))