Jump to content


Photo

fwrite() screwy


  • Please log in to reply
5 replies to this topic

#1 legohead6

legohead6
  • Members
  • PipPipPip
  • Advanced Member
  • 434 posts

Posted 10 July 2006 - 06:25 AM

Ok I have a code that you can edit text files you upload and then you can save it but for some reason its all weird(first time using fwrite()) it has the '>' before the original text and when i remove everything text and that thing it doesnt erase the page! also when i add something it like only shows the first letter or it shows up 5 times! Please give me an fwite code that take exactly how the textarea is and make it what the page looks like! Please sumthing simple as this is more of a learning project for me!
"Syntax error" .. WHERE THE **** IS IT?!

#2 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 10 July 2006 - 07:02 AM

This script should save a file:
<?php
echo <<<EOF
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>File writer</title>
<style type='text/css'>
body
{
	text-align: center;
}
body, input
{
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 80%;
}
input, textarea
{
	border: 1px solid black;
	width: 100%;
}
textarea
{
	font-size: 90%;
	font-family: "Courier New", Courier, mono-spaced;
}
#form
{
	width: 500px;
	padding: 0px;
	margin: auto;
	text-align: left;
}
#form.buttonstrip
{
	text-align: center;
	margin: auto;
}
#form label
{
	font-weight: bold;
}
</style>
</head>
<body>

<h1>File writer</h1>


EOF;

$dir = "/var/www/stuff/";

if(!empty($_POST['content']) && !empty($_POST['filename']))
{
	chdir($dir);	
	
	if(file_exists($filename))
	{
		echo "Sorry, that file already exists";
	}
	else {
		$fp = fopen($_POST['filename'],'w');
		fwrite($fp,$_POST['content']);
		fclose($fp);
		
		echo "<p>The file has been saved.<br /><a href='javascript:history.go(-1)'>Back</a></p>";
	}
}
else {
	echo <<<EOF
<div id='form'>
<form action='{$_SERVER['REQUEST_URI']}' method='post'>
<label for='filename'>Filename:</label><br />
<input type='text' name='filename' id='filename' accesskey='F' /><br />
<label for='content'>Content:</label><br />
<textarea rows='15' cols='50' name='content' id='content' accesskey='C'></textarea>
<button type='submit' accesskey='S'>Save</button> <button type='reset' accesskey='R'>Reset</button>
</form>
</div>
EOF;
}

echo <<<EOF
</body>
</html>

EOF;
?>

Note: It is NOT secured, you need to make sure that the user won't save stuff outside of the directory if you don't trust them.

#3 legohead6

legohead6
  • Members
  • PipPipPip
  • Advanced Member
  • 434 posts

Posted 10 July 2006 - 03:35 PM

Ya but how do i make the edit thing! where the current content shows up in a textarea and whatever you change in ther it changes to the page erasing or adding new stuff
"Syntax error" .. WHERE THE **** IS IT?!

#4 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 10 July 2006 - 04:12 PM

He just showed you.

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#5 legohead6

legohead6
  • Members
  • PipPipPip
  • Advanced Member
  • 434 posts

Posted 10 July 2006 - 05:07 PM

his code creates a new file!( witch came in handy) i need one to edit existing files
"Syntax error" .. WHERE THE **** IS IT?!

#6 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 10 July 2006 - 06:08 PM

Remove the part that checks if the file exists and load the content of the file into the script by using fopen() along with fread() or file_get_contents() and echo it between the textarea tags.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users