Jump to content

writing to a txt file


newbie5050

Recommended Posts

hi guys, for a simple project i have to save some form data to a .txt file, using one of the form variables ( #studentid ) as the file name, e.g. if the #studentid submitted was 123 then the file would save as 123.txt.

I have tried a number of different ways to do this but am stuck!!1

any help will be much appreciated!!

Link to comment
Share on other sites

A simple code:

 

<?php
$filename = $_POST['filename'];
if(!file_exists($filename . ".txt")){
     $handle = fopen($filename . ".txt", 'w+');
     $content = 'some text here';
     fwrite($handle, $content);
     fclose($handle);
} else{
     echo "The file u want to create already exists";
}
?>

 

It will check if the file exists and if not it will create it with fopen and write some contents.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.