PHP将字符串保存到文件

时间:2014-01-21 20:14:11

标签: php

感谢enijar发现了第一个错误,但现在我发现它没有写任何内容到我的文件中。所以再一次,不知道发生了什么。

        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';
    }

对PHP很新,但对文件的读写并不陌生,所以非常感谢任何帮助。

2 个答案:

答案 0 :(得分:1)

您在代码末尾缺少一个}。

答案 1 :(得分:0)

首先,您需要先定义$ savestring,然后才能向其中添加任何内容(虽然它在大多数情况下会起作用,但响应在技术上仍然是意料之外的。)

$savestring = '';

从性能角度来看,您调用clean_string()函数的次数太多了。 只是做:

$savestring = cleanstring($name . ", " $company .", " . $email .", " .$model. ", " .$os. ", ".$comments."\n"."-----------------------------------\n");

这样你甚至不需要初始化$ savestring变量。 要跟踪您的错误,请将您的error_reporting设置为开启。

ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);

此外,使用Firefox或Chrome网络监视器查看从Web服务器获得的响应。

最后,正如Kovo指出的那样,假设这是你的完整代码,你最后错过了一个结束括号。

希望这有帮助!