View Single Post
  #2 (permalink)  
Old 03-14-09, 08:11 AM
de.monkeyz's Avatar
de.monkeyz de.monkeyz is offline
Wannabe Coder
 
Join Date: Apr 2008
Location: Leeds, UK
Posts: 116
Thanks: 0
Thanked 0 Times in 0 Posts
It's just basic file methods.

PHP Code:

<?php
if(isset($_POST['save']))
{
      
$dir '/path/that/files/are/saved/'#Leave blank if you want them in same folder.

      
$first_variable = empty($_POST['first_variable']) ? '' $_POST['first_variable'];
      
$second_variable = empty($_POST['second_variable']) ? '' $_POST['second_variable'];

       
$filename = empty($_POST['filename']) ? '' $_POST['filename'];

       
$errors = array();

       if(empty(
$filename)) $errors[] = "Filename must be specified";
       
       
$filename $dir.$filename;

       if(
file_exists($filename)) $errors[] = "That file already exists";

       if(!
$errors) {

           
$file $fopen($filename'w');

           
$contents "<?php
         \$variable1 = '"
.addslashes($first_variable)."';
         \$variable2 = '"
.addslashes($second_variable)."';

?>"

         
fwrite($file$contents);
         
fclose($file);
        }

}
?>
<?php 
if(!empty($errors)) {
        echo 
'<ul>';
        foreach(
$errors as $error) echo '<li>'$error'</li>';
       echo 
'</ul>';
?>
<form action="" method="post">
<p><label>Variable one:</label><input name="first_variable" type="text"/></p>
<p><label>Variable two:</label><input name="second_variable" type="text"/></p>
<p><label>Save As:</label><input name="filename" type="text"/></p>
<p><input type="submit" name="save" value="Save"/></p>
</form>
If you look here: www.deathmonkeyz.com/tutorials in the AJAX section, I have a video on how to make a file editor with Javascript And PHP (It's called "First AJAX Application"). You can ignore the Javascript part if you wish, but the php will help.

These scripts are very unsecure at the moment however. As it allows anyone to create these files. But the code I have made only allows new files to be made, and cannot change existing files. And will only make a page defining two variables.
__________________
Wanna learn AJAX? Goto http://www.deathmonkeyz.com/tutorials for free video tutorials.

AJAX - Lesson 11 - AJAX Guestbook (23/8/08)
C++ - Lesson 10 - Classes (24/8/08)
JavaScript - Lesson 03 - The DOM (24/8/08)
Need an AJAX app? Look no further, I'm available for work

Last edited by de.monkeyz; 03-14-09 at 08:26 AM.
Reply With Quote