Valhalla Legends Forums Archive | Web Development | PHP: File Write Trouble

AuthorMessageTime
HolyoneI have a script to write to a text file. However, it only works sometimes lol Now, the whole folder has full control for every file for every user(myself, everyone, system). Now, when I test it logged into a machine under my login, it works fine about 19/20 times. But, if I login on the same machine or different machine as a different user it works about 1/10 times. It's very wierd, I must have checked the permissions 1000 times. I tried pausing between the file open and write commands since I figured maybe the terminals were too slow or something. I've tried a lot of testing with this, but can't get it to work perfect and it has to be perfect. If anyone has any suggestions or anything, I'd greatly appreciated it. Thanks guys. July 05, 2005, 01:10 pm
Arta[vL]Show the script?July 05, 2005, 01:13 pm
HolyoneRight now, I've been testing with just opening the file and reading it, closing it. Then reopening it for writing. Here's the reading code:

<?php
$filename = "img/postings.txt";
$counter = 0;
$fpost = fopen($filename, "r") or die('Under Construction');

while (!feof($fpost)){
   $counter++;
   $postings[$counter] = fgets($fpost, 4096);
}

fclose($fpost);
?>





And the writing code:

if ($_POST['sp'] == 'Submit') {
      $fpost = fopen("img/testpost.txt", "w");
      
      $fout = fwrite($fpost, $_POST['name']) . fwrite($fpost, "
") . fwrite($fpost, $_POST['input_post']) . fwrite($fpost, "
");
      for($a = 1; $a <= $counter; $a = $a + 2){
         fwrite($fpost, $postings[$a] . $postings[$a + 1]);
      }
      
      fclose($fpost);
   }



Thank you
July 05, 2005, 01:45 pm
JTN DesignerShow the WHOLE thing please, HTML included.July 06, 2005, 12:59 am
AC_DrkanJust as a Little side note:
You should make your code easier to read becauser then its easier to debug. Example:
Code:
case "logs":
if (SilentAuthUser($username, $password, 0) == 1) {
if (isset($page)) {
dispLogs($username, $page);
} else {
displogs($username, 0);
}
}
break;
July 06, 2005, 10:46 am
BlazeYou can make it even easier then that if you use the proper tags:

[php ]
[/php ]
July 06, 2005, 06:21 pm
AC_Drkanits codeJuly 06, 2005, 08:10 pm
Warrior[ php ] [ /php ] highlights the PHP Code...July 06, 2005, 08:28 pm
Blaze<?php
$filename
= "img/postings.txt";
$counter = 0;
$fpost = fopen($filename, "r") or die('Under Construction');

while (!
feof($fpost)){
   
$counter++;
   
$postings[$counter] = fgets($fpost, 4096);
}

fclose($fpost);
?>





And the writing code:
if ($_POST['sp'] == 'Submit') {
      
$fpost = fopen("img/testpost.txt", "w");
      
      
$fout = fwrite($fpost, $_POST['name']) . fwrite($fpost, "
"
) . fwrite($fpost, $_POST['input_post']) . fwrite($fpost, "
"
);
      for(
$a = 1; $a <= $counter; $a = $a + 2){
         
fwrite($fpost, $postings[$a] . $postings[$a + 1]);
      }
      
      
fclose($fpost);
   }
?>


Is what it will look like..  The PHP code should be lighter shades on this theme but meh. Tongue
July 06, 2005, 08:31 pm
AC_DrkanBetter Optimized, Removed some unnecessary items, made it easier to read.
<?php
if (isset($_POST['sp'])) {
	
$fpost = fopen("img/testpost.txt", "aw");
     
	
$fout = fwrite($fpost, $_POST['name']).fwrite($fpost, "").fwrite($fpost, $_POST['input_post']).fwrite($fpost, "");
	
for(
$a = 1; $a <= $counter; $a = $a + 2){
	
	
fwrite($fpost, $postings[$a] . $postings[$a + 1]);
	
}
	
fclose($fpost);
}
?>

July 06, 2005, 08:44 pm
Holyone<html>





<
head>
<
title>Welcome to Parker McCay Legal Links</title>



<
style type="text/css">
<!--
.
style3 {font-family: Verdana; font-size: 24px; font-weight: bold; color: #211C20; }
.style12 {color: #000000}
.style13 {font-family: Verdana}
a:visited {
	
color: #000000;
	
text-decoration: none;
}
a:link {
	
color: #000000;
	
text-decoration: none;
}
a:active {
	
text-decoration: none;
}
a:hover {
	
color: #0000FF;
	
text-decoration: underline;
}
-->
</
style>



<
meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"></head>
	


<
body text=#000000 bgcolor=#FFFFFF>

<center>
<
h1 class="style3"><img src="img/intranet.jpg" width="590" height="87"> </h1>
</
center>


<
table width="100%"  border="0" cellspacing="0" cellpadding="2">
  <tr>
    <td bgcolor="#000000"><table width="100%"  border="1" cellpadding="0" cellspacing="0" bgcolor="#CBB876">
        <tr>
          <td width=20%><div align="center"><a href="http://marlton5/webview">Time Entry </a></div></td>
          <td width=20%><div align="center"><a href="http://marlton5/links/webphonelist.htm">Phone List </a></div></td>
          <td width=20%><div align="center"><a href="http://marlton5/links/training.htm">IT Training </a></div></td>
          <td width=20%><div align="center"><a href="http://marlton5/links/firmnews.php">Firm News </a></div></td>
          <td width=20%><div align="center"><a href="http://marlton5/links/default.php">Home Page </a></div></td>
        </tr>
    </table></td>
  </tr>
</
table><br>








<!--
Script To Load In The Resource Menu -->
<?
php
$filename
= "img/resources.txt";
$counter = 0;
$fresource = fopen($filename, "a+") or die('Under Construction');

while (!
feof($fresource)){
	
$counter++;
	
$line[$counter] = fgets($fresource, 4096);
}

fclose($fresource);
?>








<!-- Script To Load The Resources Table -->
<hr>


<table width="30%"  border="0" cellspacing="0" cellpadding="2" align="right">
<tr>
<td>




<table width="100%" border="1" bordercolor="#000000" bgcolor="#CBB876" cellpadding="1" cellspacing="1" align="right">

<tr><td>
	
<!-- Script To Add Link -->
	
<FORM METHOD="post" ACTION=<?php echo $_SERVER['PHP_SELF']; ?>>
	
<strong><center>Add a Resource</center></strong>
	
<font size="2">Name: </font><br>
	
<INPUT TYPE=text NAME=input_name SIZE=30><br>
	
<font size="2">URL: </font><br>
	
<INPUT TYPE=text NAME=input_url SIZE=30 VALUE="http://"><br><BR>
	
<center><INPUT TYPE=submit NAME="submitresource" onClick="history.go(0)" VALUE="Submit"></center>
</td>
</tr>
</table>
</tr>
</td>
</table>





<!-- Script To Load In The Business Postings Table -->
<?php
$filename
= "img/postings.txt";
$counter = 0;
$fpost = fopen($filename, "r") or die('Under Construction');

while (!
feof($fpost)){
	
$counter++;
	
$postings[$counter] = fgets($fpost, 4096);
}

fclose($fpost);
?>


<!-- Script To Load In The Non-Business Postings Table -->
<?php
$filename
= "img/misc.txt";
$count = 0;
$fmisc = fopen($filename, "r") or die('Under Construction');

while (!
feof($fmisc)){
	
$count++;
	
$misc[$count] = fgets($fmisc, 4096);
}

fclose($fmisc);

?>




<!-- Postings Table -->





	
<table width="50%" border="1" bordercolor="#000000" bgcolor="#CBB876" cellpadding="1" cellspacing="1" align="left">
	
	
<tr>
	
	
<td>
	
	
	
<!-- Script To Add Post -->
	
	
	
<FORM METHOD="post" ACTION=<?php echo $_SERVER['PHP_SELF']; ?>>
	
	
	
<strong><center>Add a Business Post</center></strong>
	
	
	
<TEXTAREA ROWS=10 COLS=43 NAME=input_post></TEXTAREA><BR><BR>
	
	
	
Name: <INPUT TYPE=text NAME=name SIZE=50><BR><BR>
	
	
	
<center><INPUT TYPE=submit NAME="sp" onClick="history.go(0)" VALUE="Submit"></center>
	
</tr>
	
</td>
	
</table>

<BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR>

	
<table width="50%" border="1" bordercolor="#000000" bgcolor="#CBB876" cellpadding="1" cellspacing="1" align="left">
	
	
<tr>
	
	
<td>
	
	
	
<!-- Script To Add Post -->
	
	
	
<FORM METHOD="post" ACTION=<?php echo $_SERVER['PHP_SELF']; ?>>
	
	
	
<strong><center>Add a Non-Business Post</center></strong>
	
	
	
<TEXTAREA ROWS=10 COLS=43 NAME=input_misc></TEXTAREA><BR><BR>
	
	
	
Name: <INPUT TYPE=text NAME=name_misc SIZE=50><BR><BR>
	
	
	
<center><INPUT TYPE=submit NAME="misc" onClick="history.go(0)" VALUE="Submit"></center>
	
</tr>
	
</td>
	
</table>
	
	



<!-- Script To Write New Resource To Text -->
<font color="FFFFFF">
<?php



	
if (
$_POST['submitresource'] == 'Submit') {
	

	
	
$fresource = fopen("img/resources.txt", "a+");
	


	
	
$fout = fwrite($fresource, "
"
) . fwrite($fresource, $_POST['input_name']) . fwrite($fresource, "
"
) . fwrite($fresource, $_POST['input_url']);

	
	
fclose($fresource);
	
}




	
if (
$_POST['sp'] == 'Submit') {
	
	
$fpost = fopen("img/postings.txt", "w");
	
	

	
	
$fout = fwrite($fpost, $_POST['name']) . fwrite($fpost, "
"
) . fwrite($fpost, $_POST['input_post']) . fwrite($fpost, "
"
);
	
	
for(
$a = 1; $a <= $counter; $a = $a + 2){
	
	
	
fwrite($fpost, $postings[$a] . $postings[$a + 1]);
	
	
}
	
	

	
	
fclose($fpost);
	
}






	
if (
$_POST['misc'] == 'Submit') {
	
	
$fmisc = fopen("img/misc.txt", "w");
	


	
	
$fout = fwrite($fmisc, $_POST['name_misc']) . fwrite($fmisc, "
"
) . fwrite($fmisc, $_POST['input_misc']) . fwrite($fmisc, "
"
);
	
        for($b = 1; $b <= $count; $b = $b + 2){
	
	
	
fwrite($fmisc, $misc[$b] . $misc[$b + 1]);
	
	
}
	
	

	
	
fclose($fmisc);
	
}



	



$filename = "img/postings.txt";
$counter = 0;
$fpost = fopen($filename, "r") or die('Under Construction');

while (!
feof($fpost)){
	
$counter++;
	
$postings[$counter] = fgets($fpost, 4096);
}
	
for(
$a = 1; $a <= $counter; $a = $a + 2){
	
	
	
fwrite($fpost, $postings[$a] . $postings[$a + 1]);
	
}

fclose($fpost);




?>



</body>
</html>
?>
July 11, 2005, 08:35 am
AC_DrkanFor future Refrence, please use either the [code ] or the [php ] tags.

   

Code:
<INPUT TYPE=submit NAME="submitresource" onClick="history.go(0)" VALUE="Submit">
Remove the onClick action.

Code:
<INPUT TYPE=submit NAME="sp" onClick="history.go(0)" VALUE="Submit">
Also remove the onClick action.

Code:
<INPUT TYPE=submit NAME="misc" onClick="history.go(0)" VALUE="Submit">
and again Remove the onClick action.


Instead of doing this:
Code:
if ($_POST['submitresource'] == 'Submit')
You could do this:
Code:
if (isset($_POST['submitresource']))

Because that can only be 1 value, and that is submitresource

Do that for the misc and sp.
July 11, 2005, 08:54 am
R.a.B.B.i.TThere is such a thing as TOO MUCH whitespace.July 11, 2005, 11:29 am
WarriorNever complain about my whitespace again, Rabbit :pJuly 11, 2005, 11:57 am
Holyonelol nah....the more the better Wink I find it easier for myself while I'm scrolling searching for a specific block of code. But every coder is different, so maybe you prefer a small number of whitespaces. Thanks for the help guys, I appreciate it.July 11, 2005, 01:50 pm
R.a.B.B.i.T
Never complain about my whitespace again, Rabbit :p
You can't make me.
July 11, 2005, 02:35 pm
WarriorWhitespace I use a little for organization while I'm coding but end up removing it when I'm done
each time I imagine rabbit scolding me Sad
July 11, 2005, 03:48 pm
JTN DesignerTabbing > Whitespace.July 13, 2005, 11:45 pm
R.a.B.B.i.TTabs ARE whitespace :\July 14, 2005, 03:36 pm
JTN DesignerDon't be so technical. SmileyJuly 14, 2005, 04:15 pm
dubya(The PHP tags come up with colors that are really hard to see on this background...)
Code:
$fresource = fopen($filename, "a+") or die('Under Construction');
while (!feof($fresource)){
$counter++;
$line[$counter] = fgets($fresource, 4096);
}
fclose($fresource);
A.K.A. slurping. This is one of the stupidest things a person can do. Especially if you are unsure of the file size (i.e., you are expanding it with the use of user input). Additionally, if you have a line over 4kB, you will get that line split over two or more $line elements.

Code:
<?php echo $_SERVER['PHP_SELF']; ?>
There's short hand for that :
Code:
<?=$_SERVER['PHP_SELF']?>

Both of those get repeated several times... then I see
Code:
$fresource = fopen("img/resources.txt", "a+");
...
fclose($fresource);
What? For starters you are re-opening a closed file... why? Namely you are re-opening the file in the same file mode. Why not do all of the work as early as possible, anyway?
And a couple lines down I see you opening another file you had already closed. Did you know it requires more RAM and CPU time to open a file, close it, then open it again, than it does to keep the file open the whole time? Well it does. It also means there is a higher likelihood you will have to wait on the disk.

The second re-opening I see is even more annoying than the first however, which could simply be accidental: you are opening a file you had initially opened in read mode in write mode, which will truncate the file. If you want to truncate the file, consider looking at PHP ftruncate().

Next I notice this:
Code:
$fout = fwrite($fresource, "
") . fwrite($fresource, $_POST['input_name']) . fwrite($fresource, "
") . fwrite($fresource, $_POST['input_url']);
Whilst I notice something rather dumb going on here ("
" as opposed to "\n" - "\n" helps for readability), the question predominantly on my mind is what you are planning on doing with $fout. Well?
As you must know, fwrite returns the number of bytes written to a file - not particularly useful, is it? And since you are using the string concatenation operator (.), the value returned will be rather random, with several possibilities generating the same result. For example, if out line space happens to be 2 characters (on Windows), and $_POST['input_name'] is 22 characters long, we get 222. Rather ambiguous, huh? Also, if any of those fwrites fail, you do not know which and may have additional issues ($fout may look something like "222FALSE...").

That is likely where you have your problem.

What's this?
Code:
for($a = 1; $a <= $counter; $a = $a + 2){
fwrite($fpost, $postings[$a] . $postings[$a + 1]);
}
Call the doctor, I'm going to be sick.
I can think of dozens of ways to rewrite that single line, most of which look a lot better (to me), and, strangely, none of them include "$a = $a + 2". What is this, basic? In real languages we get useful operators like +=...

Oh yeah, and what is $a? $a is not in my vocabulary! Ahh!!!
If the normal $i, $x, or $y (or the ocassional $z) do not work for you, don't hesitate to settle for meaningful variable names. This goes for the rest of your code even more so. I see $counter and $count - can you be any less descriptive? How about just changing the case in some of the letters to make it even harder? Then you could have $counter, $Counter, and $counteR to make my life just that much harder (not that this is my life, of course).


I have a feeling that if you cleaned up your code a little better, it would make it much easier for you - you, not me - to diagnose the problem. Hell, it might even fix the problem.
August 15, 2005, 05:08 am
R.a.B.B.i.TWhile your input is good, please try to not revive dead topics.  Thanks.August 15, 2005, 03:45 pm