gpt4 book ai didi

php - 如何检查文本框中的名称是否已作为本地主机服务器(PHP)中的文件夹存在而不使用mysql

转载 作者:行者123 更新时间:2023-12-01 08:01:25 25 4
gpt4 key购买 nike

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>

当我执行此代码时,输​​入详细信息后,会在文件夹内创建文件夹文本文件,并存储文本文件信息...文件夹和文本文件名都是使用用户输入的名称创建的。如果它已经存在那么它不会用该名称创建...现在我收到类似成功的警报消息,即使名称已经退出..我无法通过警报获取名称已存在消息..请找出错误并回复谢谢

最佳答案

文件存在函数被错误调用(返回 bool 值)

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

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

应替换为

if(!file_exists($name))

关于php - 如何检查文本框中的名称是否已作为本地主机服务器(PHP)中的文件夹存在而不使用mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19589968/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com