Current location: Hot Scripts Forums » Programming Languages » PHP » help: write new php file to server using a form.

help: write new php file to server using a form.

Reply
  #1 (permalink)  
Old 03-14-09, 05:54 AM
jimmyjim jimmyjim is offline
Newbie Coder
 
Join Date: Oct 2008
Posts: 33
Thanks: 0
Thanked 0 Times in 0 Posts
help: write new php file to server using a form.

Hey all,

I am trying to make a form that makes a php file and saves it to my server.
eg: 1.php

The form would have a few fields say "variable1" and "variable 2" and "save as" with a submit button.

The save as would be a text imput field where i can type in the name i want the file to be saved as eg: 1.php and the 2 other text fields would put the entered info into the php page eg: somethinggoeshere or somethingelsegoeshere

Below is how 1.php would look after the form has done its job.

PHP Code:
<?php
$variable1 
'somethinggoeshere';
$variable2 'somethingelsegoeshere';
?>
I have no idea how to do this at all and am wondering if someone could help me out with it.
If you need any more info please let me know.

Any help would be much appreciated.

Thanks
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 03-14-09, 07: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 07:26 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 03-14-09, 07:36 AM
jimmyjim jimmyjim is offline
Newbie Coder
 
Join Date: Oct 2008
Posts: 33
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks for that but it does not seem to work de.monkeys i get
Parse error: syntax error, unexpected T_STRING on line 29.

Do you know what it could be?

I have no idea.

Thanks
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 03-14-09, 09:38 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
I accidentally left a ; out on ?>"
Put a semi-colon at the end of line 27.

When php throws and unexpected error. It usually means a semi colon is missing. You should learn to look out for them.
__________________
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 03-14-09, 10:45 AM
wirehopper's Avatar
wirehopper wirehopper is offline
Community Liaison
 
Join Date: Feb 2006
Posts: 2,321
Thanks: 16
Thanked 90 Times in 88 Posts
If this is for a publicly accessible production system, be sure the validation is very good - otherwise, there are serious security risks.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #6 (permalink)  
Old 03-14-09, 11:21 AM
jimmyjim jimmyjim is offline
Newbie Coder
 
Join Date: Oct 2008
Posts: 33
Thanks: 0
Thanked 0 Times in 0 Posts
if i were to put it into a password protected folder would it be ok?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #7 (permalink)  
Old 03-14-09, 11:35 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
Password protected folders are very minimal when it comes to security to be honest. With something like this, I'd have the password stored in a db, with a sha512 hash on it
__________________
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #8 (permalink)  
Old 03-14-09, 11:13 PM
jimmyjim jimmyjim is offline
Newbie Coder
 
Join Date: Oct 2008
Posts: 33
Thanks: 0
Thanked 0 Times in 0 Posts
for some reason now im getting
Fatal error: Function name must be a string on line 21

any ideas?

Thanks
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #9 (permalink)  
Old 03-15-09, 03:29 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
I tried to call fopen, but accidentally put a $ in front of it. So it thought it had to call the contents of $fopen which is null.

Again, if it says error line:<something> and just look at that link. It's usually something obvious like this
__________________
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #10 (permalink)  
Old 03-15-09, 03:43 AM
jimmyjim jimmyjim is offline
Newbie Coder
 
Join Date: Oct 2008
Posts: 33
Thanks: 0
Thanked 0 Times in 0 Posts
fantastic works well now thanks for that.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Hello all, can you help me? K4ot1K Other Languages 2 01-23-09 05:23 AM
PHP - Transfer file from one server to another. NabZ PHP 5 05-30-07 10:45 AM
Uploading audio/video file on server in PHP rashish12 Script Requests 1 04-01-07 04:03 PM
move files around an ftp server, with php file upload script? wapchimp PHP 2 12-19-04 07:27 AM
php write to file tiny Script Requests 2 01-14-04 09:12 PM


All times are GMT -5. The time now is 11:32 PM.
vBulletin® Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.