468,536 Members | 1,549 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,536 developers. It's quick & easy.

PHP fwrite() keeps doubling document length

5
Hey all,

Sorry to be the noob with two posts asking a question, but I've been fighting with this for a couple days now, and as far as I can tell, this should be working, but PHP isn't my main language, so I'm sure there's something being missed.

I've made a small program that allows the user to first select a country, then a state/province, and finally add a city to that location. So, when a new city is submitted, I load the .TXT file containing all the other cities belonging to that region into an array, add the new city to the end of that array, sort() it, the write it to the text file by way of while loop.

What's happening though is that when the first city is added, it has one city added to the file, and a carriage return (because of the "\r\n"). When the second city is added, it has the first two cities, followed by two carriage returns (making four lines). When the third city is added, it starts with four carriage returns, three cities, then one carriage return (making eight lines). And it just keep going on like that, doubling the number of lines every time.

[PHP] $country = file ('../../country/country.txt'); //loads all the saved countries into an array
$cou = count ($country);
//First, determine the location of the list of states or provinces for the selected country
$x = $cou - 1;

//we have to knock off the blank space at the end of the variable if it is not the last place in the array
$flat_cnt = trim($country[$_COOKIE['country']]);

$state = file ('../../country/'.$flat_cnt.'/states.txt');
$sta = count ($state);
$flat_sta = trim($state[$_COOKIE['state']]);

$cities = file ('../../country/'.$flat_cnt.'/'.$flat_sta.'/cities.txt');
$cit = count ($cities);//sets a variable to the number of items in the array
$x = 0;
if ( isset ($_COOKIE['city']))
{
print ($cities[$_COOKIE['city']]);
print ('<form method="post" action="post.php"><input type="submit" name="newcity" value="Reselect State" /></form>');
}
else
{
//Print out selection box
print ('<br /><br /><b>Please Enter A New City:</b><br />');
print ('<form method="post" name="submitCity"><input type="text" name="city" size="100"><br /><input type="submit" name="submitCity" value="Add New City" /></form>');
}

if (isset ($_POST['submitCity'])) //handle form.
{
if ($fp = fopen ('../../country/'.$flat_cnt.'/'.$flat_sta.'/cities.txt', 'w+')) //try to open the file. 'w+' means a new file will be created if needed and old files will be overwritten with new contents
{
//Add entered city into cities array
$cities[$cit] = $_POST['city'];
//make the cities array alphabetical
sort($cities);

flock ($fp, LOCK_EX);//lock the file so multiple users can NOT edit file simultaneously by accident
//Write the $cities array to the file
$z = 0;
while ($z <= $cit)
{
fwrite ($fp, "$cities[$z]\r\n");
$z++;
}
flock ($fp, LOCK_UN);//unlocks the file so another user can add cities
fclose ($fp); //close the file

//print a message
print "<p><font color=red><b>The new city, </font><font color=black>".$_POST['city']." </font><font color=red>has been successfully added!</b></font></p>";
}
else //Could not open the file
{
print "<p>The city could not be stored due to a system error.</p>";
}
}[/PHP]

Any help you can offer will be greatly appreciated (o^_^o)

~Xean
Mar 22 '08 #1
3 1969
ronverdonk
4,258 Expert 4TB
Are you sure that you do not have an "\r\n" already in your $cities array read in from the original text file? That would double the output, since you also add that with your ourput.

Ronald
Mar 22 '08 #2
Xean45
5
Are you sure that you do not have an "\r\n" already in your $cities array read in from the original text file? That would double the output, since you also add that with your ourput.

Ronald

Thanks Ronald! Boy, my face is red. Logically, that should have made sense. I just used the str_replace("\r\n","",$variable); to cure that right as rain.

~Xean
Mar 25 '08 #3
ronverdonk
4,258 Expert 4TB
Don't be emberrassed. Happens to everyone sometimes. At least your problem is solved. See you next time.

Ronald
Mar 25 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by Connie Walsh | last post: by
17 posts views Thread by SW1 | last post: by
15 posts views Thread by Suraj Kurapati | last post: by
10 posts views Thread by cs | last post: by
4 posts views Thread by janssenssimon | last post: by
10 posts views Thread by Sheldon | last post: by
2 posts views Thread by alexus | last post: by
3 posts views Thread by anandhmca05 | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.