Jump to content

Archived

This topic is now archived and is closed to further replies.

legohead6

fwrite() screwy

Recommended Posts

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!

Share this post


Link to post
Share on other sites
This script should save a file: [code]<?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;
?>[/code]

[b]Note:[/b] 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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
his code creates a new file!( witch came in handy) i need one to edit existing files

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

×

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.