- Impact
- 5
Well, I had always wanted to learn how to create my own file editor so that I could edit my ad files on my websites, and well I have accomplished just that, and I will be teaching you how to do just that with this php tutorial.
Step 1:
Open up whatever program you use to create webpages with, and start out with a simple html page.
Now let me explain the php code in this tutorial.
Now save this file as editor.php, as you will notice there is a password field thrown in there as well. We don't want any visitors editing the file do we? To prevent the visitors we have password protected the editor.php file from access unless the individual can guess your password.
Step 2:
Now we will create the edit.php file, the code is below and an explenation will follow.
Now thats alot of php code. it isn't has hard as it looks the explenation has been included with the code, as I am currently feeling lazy and it was 2:00 AM when I was writing this tutorial.
That is all you need, an optional step if you are hosted on a linux server whould be to chmod the file to 777, so that you can actually edit it.
Hopefully this helps some people out there create a file editor, I might come back to this tutorial at a Later date but for now this is as far as I have gotten.
Step 1:
Open up whatever program you use to create webpages with, and start out with a simple html page.
PHP:
<html>
<head>
<title>File Editor</title>
</head>
<body>
<form action="edit.php" method="post" name="edit">
<h1>Password</h1>
<p><input name="password" type="password" id="password" value=""/></p>
<p><textarea name="edit" cols="70" rows="20">
<?php
$handle = fopen('file.php', 'r');
$file = file_get_contents('file.php');
echo ($file);
fclose($handle);
?>
</textarea></p>
<p><input name="submit" type="submit" value="Edit Ads File"/></p>
</form>
</body>
</html>
Now let me explain the php code in this tutorial.
PHP:
$handle = fopen('file.php', 'r'); //opens the file you want for reading
PHP:
$file = file_get_contents('file.php'); //gets the contents of the file you want to open
PHP:
echo ($file); //self explanatory, but it echoes the files contents into the textarea
PHP:
fclose($handle); //closes the open file
Now save this file as editor.php, as you will notice there is a password field thrown in there as well. We don't want any visitors editing the file do we? To prevent the visitors we have password protected the editor.php file from access unless the individual can guess your password.
Step 2:
Now we will create the edit.php file, the code is below and an explenation will follow.
PHP:
<?php
$filename = 'file.php';
$somecontent = stripslashes($edit);
$password = "yourpassword"; //checks the editor.php files password to see if it matches if not it goes to the else statement.
if($_POST['password']==$password)
{
// Let's make sure the file exists and is writable first.
if (is_writable($filename)) {
// In our example we're opening $filename in append mode.
// The file pointer is at the bottom of the file hence
// that's where $somecontent will go when we fwrite() it.
if (!$handle = fopen($filename, 'w+')) {
echo "Cannot open file ($filename)";
exit;
}
// Write $somecontent to our opened file.
if (fwrite($handle, $somecontent) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}
echo "Success, wrote ($somecontent) to file ($filename)";
fclose($handle);
} else {
echo "The file $filename is not writable";
}
}else {
echo ("Wrong Password Entered");
}
?>
Now thats alot of php code. it isn't has hard as it looks the explenation has been included with the code, as I am currently feeling lazy and it was 2:00 AM when I was writing this tutorial.
That is all you need, an optional step if you are hosted on a linux server whould be to chmod the file to 777, so that you can actually edit it.
Hopefully this helps some people out there create a file editor, I might come back to this tutorial at a Later date but for now this is as far as I have gotten.







