Jump to content

fputs and fopen to a remote server creates a blank file every once in awhile?


phpchick

Recommended Posts

I have a php script on a server writing to an html file on a remote server. I'm using the following combination of fopen and fputs. The problem is that once out of maybe every 10 or 15 writes to the file, the file that is written is blank with nothing in it.

 

Has anyone here had experience with this before? The file is fixed the next time it writes to it. this code is within a loop that runs every 10 seconds.

 

 

 

	// Allows overwriting of existing files on the remote FTP server
	$stream_options = array('ftp' => array('overwrite' => true));

	// Creates a stream context resource with the defined options
	$stream_context = stream_context_create($stream_options);

	// Opens the file for writing and truncates it to zero length 
	if ($fh = fopen($ftp_path, 'w',0, $stream_context))
	{

	if ( $change > 0   )
	fputs($fh, "the contents of the html file is here");      //the case if change is positive

	else
	fputs($fh, "the contents of the html file is here");   //the case if change is negative

	// Closes the file handle
	fclose($fh);
	}
	else
	{
	die('Could not open file.');
	}

Link to comment
Share on other sites

I think i found the solution.

 

basically what i decided to do is I put the entire writing portion into a do-while loop.

 

that is:

 

do the writing portion while the length of the html file is less than 100. (because I know my html should always be more than 100, if its less, it means the blank error occured).

 

and since it is a do-while loop, it will run once, and then cancel itself out at the end because the while condition is evaluated at the end of the loop. unless it is blank, then it the loop to try to rewrite the file will initiate.

 

 

 

 


	if ($fh = fopen($ftp_path, 'w', 0, $stream_context))
	{
	// Writes contents to the file
do{


	if ( $change > 0   )
	fputs($fh, "stuff that goes into the remote html file");      //the case if change is positive

	else
	fputs($fh, "stuff that goes into the remote html file");   //the case if change is negative




$check = file_get_contents ("$ftp_path");
echo strlen($check);

} 	while (         strlen($check)    < 100       );
	}

	else
	{
	die('Could not open file.');
	}
	// Closes the file handle
	fclose($fh);


Link to comment
Share on other sites

You should fclose() before your file_get_contents.  Before the fclose, it's possible the server would save the content in a buffer and not write it out to the file.  In that event your file_get_contents is not accurate.

 

An alternative to file_get_contents would be to test the return value of fputs() to see if it wrote all the data.

Return Values

fwrite() returns the number of bytes written, or FALSE on error.

 

If the return does not equal the strlen() of what you tried to write, you have a problem.

Link to comment
Share on other sites

kicken: I did what you suggest and put the fclose before the get_file_contents so that it looks like this.

 

 



if ($fh = fopen($ftp_path, 'w', 0, $stream_context))
	{
	// Writes contents to the file
do{


	if ( $change > 0   )
	fputs($fh, "stuff that goes into the remote html file");      //line 224

	else
	fputs($fh, "stuff that goes into the remote html file");   //the case if change is negative



// Closes the file handle
fclose($fh);          //line 231

$check = file_get_contents ("$ftp_path");
echo strlen($check);

} 	while (         strlen($check)    < 100       );
	}

	else
	{
	die('Could not open file.');
	}


 

 

It runs fine until it the error occurs and the html file is less than 100 in length. then this error occurs:

 

Warning:  fputs(): 422 is not a valid stream resource in <b>/root/fuzzy/htmlmain8.php on line 224

Warning:  fclose(): 422 is not a valid stream resource in <b>/root/fuzzy/htmlmain8.php</b> on line 231

size of written html: 0

 

line 224 is this line: fputs($fh, "stuff to be written");      //the case if change is positive

 

and line 231 is this line:        fclose($fh);          //where I moved the fclose above the file_get_contents

Link to comment
Share on other sites

I see the problem. the file is closed off inside the loop.

 

but the file isn't opened inside the loop.

 

so when it it hits that error, and executes inside the loop, it can't open up the file.

 

Thank you kicken :)

 

It looks like the program is functioning properly now.

Link to comment
Share on other sites

here's the code if other people want to use this, or learn

 

 


do {
	$fh = fopen($ftp_path, 'w', 0, $stream_context);		// Opens the file for writing and truncates it to zero length 

	// Writes contents to the file


	if ( $change > 0   )
	fputs($fh, "$tuff to write");      //the case if change is positive

	else
	fputs($fh, "$tuff to write");   //the case if change is negative


	fclose($fh);	// Closes the file handle
	$check = file_get_contents ("$ftp_path");
echo "size of written html: ";
echo strlen($check);
echo "\n\n";	
} 	                       while (    strlen($check)      < 100    );  



 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.