Jump to content


Photo

Writing to a file/reading from question


  • Please log in to reply
2 replies to this topic

#1 glenelkins

glenelkins
  • Members
  • PipPipPip
  • Advanced Member
  • 760 posts
  • LocationBridlington - East Riding of Yorkshire - United Kingdom

Posted 16 October 2006 - 12:26 PM

Hi

When reading a file with fopen() and writing with write() then displaying the content in a <textarea> each time the file is written to it seems to add a load of white spaces before the content....and th first time the file is read there is these spaces as well. I have used:

if (!(fwrite ($file_handle,trim($contents," ")))) {
"Cannot Write To File";
exit();
}
and

$file_contents = fread($file_handle,filesize($file));
$file_contents = trim($file_contents," ");

http://thewebsolutionprovider.com - A Solution For Everything Web!

#2 glenelkins

glenelkins
  • Members
  • PipPipPip
  • Advanced Member
  • 760 posts
  • LocationBridlington - East Riding of Yorkshire - United Kingdom

Posted 16 October 2006 - 12:29 PM

ah,it was to do with the <textarea></textarea>  it seems to do this when you put

<textarea>
Content
</textarea>

but not

<textarea>Content</textarea>

Strange thing!
http://thewebsolutionprovider.com - A Solution For Everything Web!

#3 glenelkins

glenelkins
  • Members
  • PipPipPip
  • Advanced Member
  • 760 posts
  • LocationBridlington - East Riding of Yorkshire - United Kingdom

Posted 16 October 2006 - 12:33 PM

consider this code..when the save button is hit, it saves fine, but the page has so be refreshed to show the new content, why is this when the read file code comes after the save file code? it does not make sense

<?

	// Get the file and A
	$file = $_GET['file'];
	$action = $_GET['a'];
	$contents = $_POST['filecontents'];
	
	// Check if file is empty
	if (empty($file)) {
		echo "Please Enter A File Name";
		exit();
	}
	
	// Check file exists
	if (!(file_exists($file))) {
		echo "The File Does Not Exist";
		exit();
	}
	
	// Check if the filesize is above 0
	if ($file_size = filesize($file) <= 0) {
		echo "There Is Nothing In The File";
		exit();
	}

	// Check for save action
	if ($action == "save") {
		if (!(is_writeable($file))) {
			echo "This File Is Not Writeable";
			exit();
		}
		
		// Open for writing
		if (!($file_handle = fopen($file,"w"))) {
			"Cannot Open The File";
			exit();
		}
		
		// Write to file
		if (!(fwrite ($file_handle,trim($contents," ")))) {
			"Cannot Write To File";
			exit();
		}
		
		// Close
		fclose ($file_handle);
		
		// Show message
		$_GET['msg'] = "File Saved!";
	}

	// Open the file for reading	
	if(!($file_handle = fopen($file,"r"))) {
		echo "Cannot Read File";
		exit();
	}
	
	// Read the contents of the file
	$file_contents = fread($file_handle,filesize($file));
	$file_contents = trim($file_contents," ");
	
	// Close the file
	fclose ($file_handle);
		
	// Display The File
	// In a form
?>

<!-- START HTML -->
<form name="openfileform" action="?file=<? echo $file; ?>&a=save" method="post">
	<p style="font-weight: bold"><? echo $_GET['msg']; ?></p>
	<p>
	<table border="0" cellpadding="0" cellspacing="0">
		<tr>
			<td>
				File Size: <? echo $file_size; ?> bytes
			</td>
		<tr>
			<td>
				<textarea name="filecontents" rows="50" cols="100"><? echo $file_contents; ?></textarea>
			</td>
		</tr>
		<tr>
			<td>
				<input type="submit" value="Save">
			</td>
		</tr>
	</table>
	</p>
</form>
<!-- END HTML -->

http://thewebsolutionprovider.com - A Solution For Everything Web!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users