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
https://forums.phpfreaks.com/topic/67382-writing-to-a-txt-file/
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
https://forums.phpfreaks.com/topic/67382-writing-to-a-txt-file/#findComment-338179
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.