PHP将字符串保存到文件中
感谢enijar,我发现了第一个错误,但现在我发现它没有向我的文件写入任何内容。再说一次,我不知道发生了什么PHP将字符串保存到文件中,php,Php,感谢enijar,我发现了第一个错误,但现在我发现它没有向我的文件写入任何内容。再说一次,我不知道发生了什么 function clean_string($string) { $bad = array("content-type","bcc:","to:","cc:","href"); return str_replace($bad,"",$string); } $fp = fopen("demoFormD
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$fp = fopen("demoFormData.txt", "a");
$savestring = clean_string($name).", ".clean_string($company).", ".clean_string($email).", ".$model.", ".$os.", ".$comments."\n"."-----------------------------------\n";
fwrite($fp, $savestring);
fclose($fp);
echo 'form saved';
这是我所有的相关代码,包括错误检查:
if(isset($_POST["submit"])){
function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
if(!isset($_POST['name']) ||
!isset($_POST['company']) ||
!isset($_POST['email']) ||
!isset($_POST['model']) ||
!isset($_POST['OS']) ||
!isset($_POST['comments'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$name = $_POST["name"];
$company = $_POST["company"];
$email = $_POST["email"];
$model = $_POST["model"];
$os = $_POST["OS"];
$comments = $_POST["comments"];
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp, $email)){
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp, $name)){
$error_message .= 'The Name you entered does not appear to be valid.<br/>';
}
if(!preg_match($string_exp, $company)){
$error_message .= 'The Company you entered does not appear to be valid.<br/>';
}
if(strlen($comments) < 2) {
$error_message .= 'The Comments you entered do not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$fp = fopen("demoFormData.txt", "a");
$savestring = clean_string($name).", ".clean_string($company).", ".clean_string($email).", ".$model.", ".$os.", ".$comments."\n"."-----------------------------------\n";
fwrite($fp, $savestring);
fclose($fp);
echo 'form saved';
}
if(设置($\u POST[“提交”])){
函数失效($error){
//您的错误代码可以转到这里
echo“非常抱歉,您提交的表单存在错误。”;
echo“这些错误出现在下面。
”;
echo$错误。“
”;
echo“请返回并修复这些错误。
”;
模具();
}
如果(!isset($\u POST['name']))||
!isset($_POST['company']))||
!isset($\u POST['email'])||
!isset($\u POST['model']))||
!isset($\u POST['OS']))||
!isset($_POST['comments'])){
死亡('很抱歉,您提交的表格似乎有问题');
}
$name=$_POST[“name”];
$company=$_POST[“company”];
$email=$_POST[“email”];
$model=$_POST[“model”];
$os=$_POST[“os”];
$comments=$_POST[“comments”];
$error_message=“”;
$email_exp='/^[A-Za-z0-9.[U%-]+@[A-Za-z0-9.-]+\[A-Za-z]{2,4}$/';
如果(!preg_match($email_exp,$email)){
$error_message.=“您输入的电子邮件地址似乎无效。
”;
}
$string_exp=“/^[A-Za-z.-]+$/”;
如果(!preg_match($string_exp,$name)){
$error_message.=“您输入的名称似乎无效。
”;
}
如果(!preg_匹配($string_exp,$company)){
$error_message.=“您输入的公司似乎无效。
”;
}
if(strlen($comments)<2){
$error_message.=“您输入的注释似乎无效。
”;
}
如果(strlen($error_message)>0){
死亡($error_message);
}
函数clean_string($string){
$bad=数组(“内容类型”,“密件抄送:”、“收件人:”、“抄送:”、“href”);
返回str_replace($bad,“,$string);
}
$fp=fopen(“demoFormData.txt”,“a”);
$savestring=clean_string($name)。“,”、“.clean_string($company)。”、“.clean_string($email)。”、“$model.”、“$os.”、“$comments.”\n.“---------------------------------------------------\n”;
fwrite($fp,$savestring);
fclose($fp);
回显“保存表单”;
}
对PHP非常陌生,但对文件的读写并不陌生,因此非常感谢您的帮助。您在代码末尾缺少了一个}。首先,您需要定义$savestring,然后才能添加任何内容(虽然它在大多数情况下都可以工作,但从技术上来说,响应仍然是意外的) 从性能角度来看,调用clean_string()函数的次数太多了。 只要做: 这样,您甚至不需要初始化$savestring变量。 若要跟踪错误,请将“开发错误报告”设置为启用
ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);
另外,使用Firefox或Chrome网络监视器查看您从web服务器获得的响应
最后,正如Kovo所指出的,假设这是您的完整代码,那么您在结尾缺少一个结束括号
希望这有帮助 错误代码是什么?@Yani我不知道如何得到它。。。我的表单位于一个单独的PHP文件中,该文件调用此表单。我发现这句话是一种古老的评论和祈祷方式。如何查看错误代码?我认为clean_string函数前面的这个点导致了错误$savestring=.clean_string($name)。我;;需要查看您的错误。@Enijar很好地解决了错误,但它没有向文件写入任何内容……可能是因为您需要为您的
if(isset($\u POST[“submit”]){
在echo'form saved';
后加上右括号。@metsales我在php.ini文件中设置了吗?但是,到目前为止,我不认为我会收到错误,因为代码执行完毕。
$savestring = cleanstring($name . ", " $company .", " . $email .", " .$model. ", " .$os. ", ".$comments."\n"."-----------------------------------\n");
ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);