#21087 [Fbk->Opn]: Rewriting file no longer works

From: Date: Fri, 20 Dec 2002 03:21:56 +0000
Subject: #21087 [Fbk->Opn]: Rewriting file no longer works
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-28060@lists.php.net to get a copy of this message
 ID:               21087
 User updated by:  gfraley5@earthlink.net
 Reported By:      gfraley5@earthlink.net
-Status:           Feedback
+Status:           Open
 Bug Type:         Filesystem function related
 Operating System: XP
 PHP Version:      4.3.0RC3
 New Comment:

It appears to be the flock() call.  This fails.  However, if you
comment out the flock() calls it works.

$filename = "c:/phptest.txt";
$data = "This is test text";
$fp = fopen("$filename",'w');
flock($fp,1);
$len = fwrite($fp,$data);
flock($fp,3);
fclose($fp);
echo "Wrote $len bytes";


Previous Comments:
------------------------------------------------------------------------

[2002-12-19 11:27:09] wez@php.net

Keep this at feedback.
(and note that I am not mira-junk@cekit.cz and that my previous comment
on this was my first).

------------------------------------------------------------------------

[2002-12-19 11:17:24] gfraley5@earthlink.net

Sorry for the misunderstanding.  First of all I did not know that you
were a developer working the problem. Nothing in your comment told me
that.  Secondly, I have already stated that I will try to produce a
smaller script (see my original post).  I just haven't had time to work
the problem.  As to the buffering, without it, the full text is not
written out.  It gets truncated.

------------------------------------------------------------------------

[2002-12-19 07:43:14] wez@php.net

Please try and cut down the script to a complete self-contained example
that I can run here.
I'm not going to spend hours debugging a long, incomplete code
fragment.
What I'm looking for it something like this:

$fp = fopen($filename, "w");
$len = fwrite($fp, $data);
fclose($fp);
echo "Wrote $len bytes";

If that works, try adding in the flock calls.
If that works, try adding in those set_file_buffer calls
(NB: there does not seem to be a need to play with file
buffering in this case).

Please help me to help you.

------------------------------------------------------------------------

[2002-12-19 07:16:01] gfraley5@earthlink.net

I'm running Apache 1.3.27.  I haven't tested it under *nix.  As far as
a code snippet, I will assume that it is in this code, somewhere:

if ($admin_from_web==true) {
	$row = 0;
	$insert_msg = '';
	$fp = fopen("$path_to_gb",'r') or die("$unable_to_access_file_msg
$path_to_gb");
	set_file_buffer($fp,$buffersize);
	flock($fp,1);
	$num = 16;
	for ($i=0;$i<$num;$i++) {
		$j = $i+1;
		$head_array[$i] = ${"col$j"};
	}
	while ($data = fgetcsv($fp,$csv_buffer_size,",")) {
		$row++;
		if ($row<2) continue;
		$new_msg = "";
		if ($data[0]==$msgid) {
			if (!empty($update)) {
				reset ($HTTP_POST_VARS);
				while (list ($key, $val) = each ($HTTP_POST_VARS)) {			
					$found=false;
					for ($i=0;$i<count($head_array);$i++) {
						if ($key==$head_array[$i]) {
							$rec[$i] = $val;
							$found=true;
						}
						if ($found) break;
					}
				}
				$msg = $label_name."$data[4] (ip = ".getenv(REMOTE_ADDR).") -
$head_array[0]:$msgid ".$on." ".date($date_format)." $at
".date($time_format)."\n";
				for ($c=0;$c<$num;$c++) {
					if ($c==0) $comma=""; 
					else $comma=",";
					if ($c==13&&$row>1) {
						$rec[$c] = htmlspecialchars($rec[$c],ENT_QUOTES);
						$rec[$c] = str_replace("&amp;#","&#",$rec[$c]);						
						$new_msg .= $comma."\"$rec[$c]\"";						
					}
					elseif ($c<4) $new_msg .= $comma."$rec[$c]";
					else $new_msg .= $comma."\"$rec[$c]\"";
					if (chop($data[$c])!=chop($rec[$c])) {
							$msg .= "$head_array[$c]: [$data[$c]]\n\n$head_array[$c]:
[$rec[$c]]\n\n\n";
					}
				}
				$new_msg .= "\n";
				if ($admin_email_advice_on_gb_edit && !empty($admin_email_address))
{
					$to = "$admin_email_address,$admin_email_address_addl";
					$subject = "$admin_email_subject_on_gb_edit";
					@mail($to, $subject, "$msg","From: $admin_email_address") ;
				}
			}
		}
		else {
			for ($c=0;$c<$num;$c++) {
				$val = $data[$c];
				if ($c==0) $comma=""; 
				else $comma=",";
				if ($c==13&&$row>1) $new_msg .= $comma."\"$val\"";
				elseif ($c<4&&$row>1) $new_msg .= $comma."$val";
				else $new_msg .= $comma."\"$val\"";
			}
			$new_msg .= "\n";						
		}
		if (!empty($new_msg)) $newgb[] = $new_msg;
	}
	flock($fp,3);
	fclose($fp);
	unset($delete);
	unset($update);
	$buffersize = round(filesize($path_to_gb)*1.5);	
	$fp = fopen("$path_to_gb",'w') or die("$unable_to_access_file_msg
$path_to_gb");
	set_file_buffer($fp,$buffersize);
	flock($fp,1);	
	for ($i=0;$i<count($newgb);$i++) {
		if ($i==0) $prefx = "<? Header(\"HTTP/1.0 403 Forbidden\");exit;
?>\n";
		else $prefx = "";
		fwrite($fp,$prefx.$newgb[$i]);
	}
	flock($fp,3);
	fclose($fp);
}

------------------------------------------------------------------------

[2002-12-19 06:29:35] mira-junk@cekit.cz

I've read similar bug report from several of our users (we have free
hosting server with aprox. 60,000 users, currently running latest RC of
php-4.3.0 with apache-2.0.43), but I've not been able to reproduce it.
Can you please paste here part of your code ?

------------------------------------------------------------------------

The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
    http://bugs.php.net/21087

-- 
Edit this bug report at http://bugs.php.net/?id=21087&edit=1



Thread (13 messages)

« previous php.bugs (#28060) next »