File Writing and Appending

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

File Writing and Appending

Post by tecktalkcm0391 »

I'm using something close to this for creating a document.

Code: Select all

$template_file = "whslabels.rtf";
$handle = fopen($template_file , "r");
$contents = fread($handle, filesize($template_file));

$original= array("Address1", "Address2");
$new = array("pizza", "beer");

$newphrase = str_replace($original, $new, $contents);

$handle2 = fopen("newlabel6.rtf" , "w");
fwrite ( $handle2 ,$newcontents);

fclose ($handle);
fclose($handle2);
How can I open that document and append to it? I'm having trouble it think its just over writing the file.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

http://php.net/function.fopen#id2759719 lists all the valid values you can give for the mode argument. Read them carefully.
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

a+ i know... its not working heres what i have....

Code: Select all

$numberoftests = $_POST['numberoftests']; 
		
		//chmod("/home/cmmvideo/public_html/eTests/QuickTests/documents/","777");
		
		$documentname = md5(time());
		//echo $documentname;
		
		
		for($counter = 1; $counter <= $numberoftests; $counter += 1){
			
			if(!file_exists("documents/".$documentname.".doc")){
				$new_doc = fopen("documents/".$documentname.".doc" , "w");
			} else {
				$new_doc = fopen("documents/".$documentname.".doc" , "a+");
			}
			
			if($counter > 1){
				$newtests = generate_more_tests($line);
				$newtestquestions = $newtests[0];
				$newtestanswers = $newtests[1];
				$d_q_output = $newtestquestions;
				$d_a_output = $newtestanswers;
			} else {
				$d_q_output = $q_output;
				//print_r($d_q_output);
				$d_a_output = $a_output;
			}
			
			$template_file = "QuickTestTemplate.rtf";
			$handle = fopen($template_file , "r");
			$contents = fread($handle, filesize($template_file));
			
			$original= array("TestNumberHere", "TestQuestionsHere", "TestAnswersHere");
			
			
			$new_questions = implode('',$d_q_output);
			$new_answers = implode('',$d_a_output);
			$new = array("$counter", $new_questions, $new_answers);
				
			$newcontents = str_replace($original, $new, $contents);
			$newcontents = str_replace("&nbsp;", " ", $newcontents);
			$newcontents = str_replace("<br>", "\n", $newcontents);
			
			fwrite($new_doc ,$newcontents);
			
			fclose ($new_doc);
		}
			
		
		fclose($handle);
Also, how do I make a new line in for the Word Document that is being created because \n doesn't seem to be working....
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

tecktalkcm0391 wrote:Also, how do I make a new line in for the Word Document that is being created because \n doesn't seem to be working....
"\n" is Linux.
"\r" is Mac.
"\r\n" is Windows.

I think.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

The constant PHP_EOL is set for the server's native carriage return.
Post Reply