Help me make a php editing tool for webmasters

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jameslat
Forum Newbie
Posts: 7
Joined: Fri Feb 26, 2010 5:08 pm

Help me make a php editing tool for webmasters

Post by jameslat »

hey guys last night i had an idea to make a simple editing tool.
now i know similar tools like this are out there, but i thought it would be cool to make one.
so basicaly i want it to do the following.
1. load the file from off the server
2. include the file in a text field (so that i can edit it)
3. then at the bottum of this text field have a save button which will write the files to the new page.
i though this would be just a slight chalenge but i have run into a problem when it comes time to laod the document.
we will use a txt for the example.
the code

Code: Select all

 
<html>
<head></head>
<body>
<h2>Get er done</h2>
<?php
///by James Lat
if (!isset($_POST['submit'])){
?>
<form action = "<?php echo $SERVER['PHP_SELF']; ?>" method="post">
Load you file: <input name="myfile" size="10">
<input type="submit" name="submit" value="Load">
</form>
<form method="post"> 
<textarea cols="50" rows="4" name="myfile">
<?php
}
else{
$myfile=$_POST['myfile'];
include("$myfile"); 
}
?>
</textarea>  
</form>
</body>
</html>
 
as u can see (if u run this code) that it will load the textfile but not in side the text area (which i intended for it to do)
you can also see that i haven't include the write code yet. I figure it is best to take one step at a time.
can anyone help me with this?
also if you try to load a .html document it will load the page. all i want it to loa is the code.
thanks for any help!
-James lat
lshaw
Forum Commoner
Posts: 69
Joined: Mon Apr 20, 2009 3:40 pm
Location: United Kingdom

Re: Help me make a php editing tool for webmasters

Post by lshaw »

You need to use the fopen() function

read the file:

Code: Select all

 
$handle=fopen("yourfile.txt","r"); //open a file for reading
$read=fread($handle,filesize($handle)); //read the whole length of the file using the filesize() function
echo "<textarea>"; //start a textarea
echo $read; // show file in box
echo "</textarea>";
fclose($handle);
 
After submitting the textarea, assuming its name is textarea.
To save the file:

Code: Select all

 
$text=$_POST['textarea']; //from the texarea page
$handle=fopen("yourfile.txt","w"); //open the file for writing
$write=fwrite($handle,$text); //write the new values to the file
fclose($handle);
 
Dont try and copy and paste it as there will be sytax errors, but like i always say, you get the idea

This would be my approach

Lewis
Post Reply