Web Analytics Made Easy -
StatCounter fwrite Code outputting extra file. - CodingForum

Announcement

Collapse
No announcement yet.

fwrite Code outputting extra file.

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • fwrite Code outputting extra file.

    In the code below. The $fp1 is outputting two files one correct with the clients information and the other empty other than a $Refnumb string. Any ideas? I am pretty new to PHP, I am sure this is something nooby. Any help would be great!


    Code:
    function rand_string( $length ) {
    
    $str = " ";
    	$chars = "0123456789";	
    
    	$size = strlen( $chars );
    	for( $i = 0; $i < $length; $i++ ) {
    		$str .= $chars[ rand( 0, $size - 1 ) ];
    	}
    
    	return $str;
    }
    
    $Refnumb = rand_string( 5 );
    
    
    
    
    $text = $_POST['first_name']; # Form must use POST. 
    $text1 = $_POST['last_name']; # Form must use POST. 
    $text2 = $_POST['email']; # Form must use POST. 
    $text3 = $_POST['telephone']; # Form must use POST. 
    $text4 = $_POST['comments']; # Form must use POST. 
    
    $mypathso = ".\\so\\";
    $mypath = ".\\requests\\";
    
    $filename =$mypath .  $text1 . $text  . (".txt");
    $sofilename =$mypathso . $Refnumb . (".txt");
    
    if (file_exists($filename) == true)
    
    {
        $filename = $mypath .  $text1 . $text .  + 1   . (".txt");
    } 
    else{
    
    }
    
    
    $supertext =  ("First Name:") .  $text . " \r\n"  . ("Last Name:") .  $text1 . " \r\n"  . ("Contact Number:") . $text3 . " \r\n"  . ("Email Address:") . $text2  . " \r\n" . ("Customer Number:") .   $Refnumb ." \r\n" .  ("Status: N/A")  . " \r\n" . ("Details: N/A") . " \r\n" . ("Problem:") . " \r\n"  . " \r\n"  . $text4;
    
    
    $fp = fopen ($filename, "w");
    if ($fp) {
    fwrite ($fp, $supertext);
    fclose ($fp);
    }
    
    $fp1 = fopen ($sofilename, "w");
    if ($fp1) {
    fwrite ($fp1, $supertext);
    fclose ($fp1);
    
    echo ("Service Inquiry submitted sucessfully! Thankyou, " . $text . " I will contact you shortly regarding your inquiry! - Derek (Owner of The Boro Tech) <br><br>");
    echo ("<b>Customer Number:" . $Refnumb . "<br><br> Record this number.</b> If you continue with a computer service you can use this number to check the status of your service order.");
    
    }
    else {
    echo ("File was not written");
    }
    
    ?>

  • #2
    The code reads correct. So I ran it and things worked the way you would think. The so and the requests directory had only one file written to them. The $fp1 only outputs a single file to the so dir.

    BUT the second running of the code using the same form input strings does yield a file named last_namefirst_name1.txt

    The third time this file is just over written
    Evolution - The non-random survival of random variants.
    Physics is actually atoms trying to understand themselves.

    Comment


    • #3
      Thanks for the reply. That's odd. I am still experiencing two files being put into the SO dir. With different filenames given to them by the rand_string function. One of them has all of the clients info. The other is formated but empty of data other than the Refnumb variable. So odd...

      Comment


      • #4
        Two runs = 2 files, but 3 runs = 2 files. Thats due to this line:
        PHP Code:
        $filename $mypath .  $text1 $text .  + 1   . (".txt"); 
        + 1 simply indicates positive one, and always results in $mypath . $text1 . $text . '1.txt' as the name of the file. So if a collision exists it will instead overwrite a previously created file.

        Can you clarify the problem a bit? The initial almosts sounds like you are indicating that the contents of the file are $Refnumb, but I don't see that as being the case in any of the code provided.
        PHP Code:
        header('HTTP/1.1 420 Enhance Your Calm'); 
        Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

        Comment


        • #5
          Originally posted by Fou-Lu View Post
          Two runs = 2 files, but 3 runs = 2 files. Thats due to this line:
          PHP Code:
          $filename $mypath .  $text1 $text .  + 1   . (".txt"); 
          + 1 simply indicates positive one, and always results in $mypath . $text1 . $text . '1.txt' as the name of the file. So if a collision exists it will instead overwrite a previously created file.

          Can you clarify the problem a bit? The initial almosts sounds like you are indicating that the contents of the file are $Refnumb, but I don't see that as being the case in any of the code provided.


          using a form that post's to my php script. I am getting two files in each directory (so and requests) In each directory I receive one correct file like:


          one that is correctly named "SmithJohn.txt"
          ========================

          First Name:John
          Last Name:Smith
          Contact Number:555-555-1234
          Email Address:[email protected]
          Customer Number: 62046
          Status: N/A
          Details: N/A
          Problem:

          My computer caught on fire! Please help!

          =========================

          and one empty... other than the $Refnumb string... It is getting named " .txt"
          In the SO. The name of the blank file is a seperate output of the rand_string function.

          =========================


          First Name:
          Last Name:
          Contact Number:
          Email Address:
          Customer Number: 46699
          Status: N/A
          Details: N/A
          Problem:

          ==========================



          I hope this makes more sense.

          Comment


          • #6
            I found a solution i guess. The problem only occurs on the web server it self. (where I am developing). If I run the script from a different machine it outputs correctly.


            Very odd.. Thanks everyone for helping out. I guess problem solved.

            Comment

            Working...
            X
            😀
            🥰
            🤢
            😎
            😡
            👍
            👎