[advanced search]
Results from the most recent live auction are here.
25 members in the live chat room. Join Chat!
Register Rules & FAQ NP$ Store Active Threads Mark Forums Read
Go Back   NamePros.Com > Design and Development > Webmaster Tutorials
User Name
Password

Old 07-31-2006, 01:39 PM   · #1
bquast
NamePros Member
 
bquast's Avatar
 
Name: Brett Quast
Location: Hardisty, Alberta Canada
Trader Rating: (8)
Join Date: Nov 2005
Posts: 180
NP$: 77.50 (Donate)
bquast will become famous soon enough
PHP File Editor

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.

PHP Code:
<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 Code:
$handle = fopen('file.php', 'r'); //opens the file you want for reading


PHP Code:
$file = file_get_contents('file.php'); //gets the contents of the file you want to open


PHP Code:
echo ($file); //self explanatory, but it echoes the files contents into the textarea


PHP Code:
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 Code:
<?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.


Please register or log-in into NamePros to hide ads
bquast is offline   Reply With Quote
Old 08-01-2006, 07:51 AM   · #2
spacetrain
NamePros Member
 
Trader Rating: (2)
Join Date: May 2006
Posts: 133
NP$: 10.00 (Donate)
spacetrain will become famous soon enoughspacetrain will become famous soon enough
Thanks for the info, Rep Added
spacetrain is offline   Reply With Quote
Old 08-01-2006, 10:01 AM   · #3
brianmn
NamePros Member
 
Trader Rating: (3)
Join Date: Oct 2005
Posts: 172
NP$: 77.85 (Donate)
brianmn is on a distinguished road
Thanks for the great tutorial...

I'll definately be referencing this tutorial for use on my site.
brianmn is offline   Reply With Quote
Old 08-01-2006, 01:16 PM   · #4
bquast
NamePros Member
 
bquast's Avatar
 
Name: Brett Quast
Location: Hardisty, Alberta Canada
Trader Rating: (8)
Join Date: Nov 2005
Posts: 180
NP$: 77.50 (Donate)
bquast will become famous soon enough
no problem guys, I might have some more up sometime here soon.
__________________
Photoshop-ed Your source for everything photoshop
FH4YOU Free file hosting for you
ThemezFree Your ultimate source for free proxy templates
bquast is offline   Reply With Quote
Old 08-21-2006, 02:40 AM   · #5
fleshas
NamePros Regular
 
fleshas's Avatar
 
Location: Lithuania
Trader Rating: (20)
Join Date: Mar 2005
Posts: 606
NP$: 16.10 (Donate)
fleshas is a jewel in the roughfleshas is a jewel in the roughfleshas is a jewel in the rough
Cystic Fibrosis
thanks.. rep added
__________________
Founder of Lithuanian Domainers Community
fleshas is offline   Reply With Quote
Closed Thread

NamePros is a revenue sharing forum.

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


Site Sponsors
Proof is in the Parking free webhosting http://www.mobisitetrader.com/
Advertise your business at NamePros
All times are GMT -7. The time now is 11:57 AM.


Powered by: vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 2.4.0