Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

create a file using php

Status
Not open for further replies.

MJB3K

Programmer
Joined
Jul 16, 2004
Messages
524
Location
GB
hi, how would you go about creating a file using php?

#page1#

textbox called filename
textarea called pageinfo

#page2#

how would you create a page called the filename and with pageinfo in it??

thanx

Martin


Gaming Help And Info:
 
Something like this (warning... typed, but not tested)
Code:
/* page 1 */
<form action="<? echo $_SERVER['PHP_SELF'] ?>" method="POST">
Filename:&nbsp;<input type=text name=filename>
Content: <textarea name=pageinfo></textarea>
<input type=submit name=submit value="Create File">
</form>

/* page 2 */
<?
 $filename = $_POST['filename'];
 $pageinfo = stripslashes($_POST['pageinfo']);
 $fp = fopen($filename,'w+'); /* open for write/append */
 fprintf($fp,$pageinfo);
 fclose($fp);
 echo "Thanks\n";
?>
Notice: I have done no error checking in this code.
Things to watch out for:
1) No filename entered
2) Filename entered, but not in the directory you expect. i.e. a hacker has discovered your code and is trying to make a system level file.

Ken
 
how can you create a file using php??

the filename would be from a $_POST['filename']; and the stuff to be written to the file would be $_POST['info'];

how would you create a file with the filename from the $_POST with teh stuff to be written from a $_POST?



Thanx

Martin

Gaming Help And Info:

 
are you trying to do command like stuff ?
if you are you can just display in the .php script and pipe it at the command line e,g,
c:\php\php -q script.php >> file.txt
 
it creates the page, but nothing is written to the file. it comes up blank??

any ideas??


Regards,

Martin

Gaming Help And Info:
 
Not without seeing your code or a URL.

Our mindreading powers only go so far... :-)

Ken
 
Code:
<?php
$filename = $_POST['filename'];
$somecontent = $_POST['info'];

// 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, 'a')) {
         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";
}
?>

creates the file, but its blank

url:



Regards,

Martin

Gaming Help And Info:
 
From this line:

[tt]if (fwrite($handle, $somecontent) === FALSE) {[/tt]

Where in your script do you put a value into $somecontent?


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Sorry.

I'm not sure what to tell you.

On my system using this HTML page:

Code:
<html><body>
<form method="post" action="test_write.php">
<input type="text" name="filename"><br>
<input type="text" name="info"><br>
<input type="submit"></form></body></html>

And your script, if the file exists and my web server's user has permission to write to the file, the script does what to expect.


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
i no this might not be relevant, but when i try to change the CHMOD to 777, the page then produces a 500 - Internal Server Error.

why is this doing this??


Regards,

Martin

Gaming Help And Info:
 
I'm guessing because you're not the owner.

CHMOD on the actual server, not using php.

[cheers]
Cheers!
Laura
 
i am changing the CHMOD through an FTP program. i am the owner of webrevolt.biz. i havnt had this problem before on other servers

any other ideas??

Regards,

Martin

Gaming Help And Info:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top