...

View Full Version : fwrite Code outputting extra file.



Gono
09-09-2011, 04:12 AM
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! :)





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");
}

?>

sunfighter
09-09-2011, 04:12 PM
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

Gono
09-09-2011, 05:59 PM
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...

Fou-Lu
09-09-2011, 10:40 PM
Two runs = 2 files, but 3 runs = 2 files. Thats due to this line:


$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.

Gono
09-09-2011, 11:45 PM
Two runs = 2 files, but 3 runs = 2 files. Thats due to this line:


$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.

Gono
09-10-2011, 03:38 AM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum