Jump to content

fwrite() screwy


legohead6

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!
Link to comment
https://forums.phpfreaks.com/topic/14145-fwrite-screwy/
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.
Link to comment
https://forums.phpfreaks.com/topic/14145-fwrite-screwy/#findComment-55418
Share on other sites

Archived

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

×
×
  • 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.