Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Exclamation 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
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    3,763
    Thanks
    23
    Thanked 548 Times in 547 Posts
    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

  • Users who have thanked sunfighter for this post:

    Gono (09-09-2011)

  • #3
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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...

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    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.

  • Users who have thanked Fou-Lu for this post:

    Gono (09-09-2011)

  • #5
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote 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:Jsmith@mailinator.net
    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.

  • #6
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Smile

    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.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •