473,472 Members | 2,181 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How do I save a text file?

Hi all

I want to save the contents of a textarea within my webform as a text file .txt
on my webserver. In addition, I want to save it with the same file name as the
next autoincrement number in my sql table.(e.g. If I have 15 records in my table
then the next autoincrement number will be 16 when I add a new record and I want
the text file to be saved as 16.txt)

sadly this is far as I have got cos im not sure how to do either of the above.

<?php
$textfilecontents=$_POST['text'];
?>

Any help greatly appreciated

Regards
Dynamo

Jul 17 '05 #1
3 11622
Dynamo wrote:
I want to save the contents of a textarea within my webform as a text file .txt
on my webserver. In addition, I want to save it with the same file name as the
next autoincrement number in my sql table.(e.g. If I have 15 records in my table
then the next autoincrement number will be 16 when I add a new record and I want
the text file to be saved as 16.txt)


If you're actually putting it into the table, you can get the ID by
calling mysql_insert_id() after inserting it into the table.

So something like this should work:

function insert_and_write($data) {
if (mysql_query("INSERT INTO `table` (fieldname) VALUES ('$data')",
$link)) {
$filename = mysql_insert_id().".txt";
$fp = fopen($filename, 'w');
fwrite($fp, $data);
fclose($fp);
return true;
}
return false
}

This will also prevent it from trying to write to the file if the INSERT
query fails. I haven't tested this, but I'm pretty sure mysql_query()
returns false if the query fails for some reason.
Roy W. Andersen
--
ra at broadpark dot no / http://roy.netgoth.org/

"Hey! What kind of party is this? There's no booze
and only one hooker!" - Bender, Futurama
Jul 17 '05 #2
Dynamo wrote:
Hi all

I want to save the contents of a textarea within my webform as a text file
.txt on my webserver. In addition, I want to save it with the same file
name as the next autoincrement number in my sql table.(e.g. If I have 15
records in my table then the next autoincrement number will be 16 when I
add a new record and I want the text file to be saved as 16.txt)

sadly this is far as I have got cos im not sure how to do either of the
above.

<?php
$textfilecontents=$_POST['text'];
?>

Any help greatly appreciated

Regards
Dynamo


Hi Dynamo,

Wouldn't it be easier for you to store that text in the database itself?

That aside.

This is how to do that:
1) Get the next autoincrement.
(I have no clue about the excact syntax because you din't mention the
database.)

For now I take that you find that number and store it in $ai, ok?

2) Create a new file with the right name ($ai.txt).
to do this:
- go to www.php.net
- look for the function fopen. (And when you are there, you can find all I
write here too, better, with more examples)

resource fopen ( string filename, string mode [, bool use_include_path [,
resource zcontext]])

So:
$filename = $ai.".txt";
$dir = "/home/dynamo/myfiles/"; // adjust to where you want to save
$fullpath = $dir.$filename;
$handle = fopen ($fullpath, "w+");

// now write
fwrite($handle, $_POST["text"]);

// and be a nice and close:
fclose($handle);
It is all on www.php.net
This is a good startingpoint:
http://nl3.php.net/manual/en/ref.filesystem.php

Please note that I didn't implement any errorhandling.
But you should.

Regards,
Erwin Moller

PS: I didn't test the code.
Jul 17 '05 #3
In article <41**********************@news.xs4all.nl>, Erwin Moller says...
Hi Dynamo,

Wouldn't it be easier for you to store that text in the database itself?

That aside.

This is how to do that:
1) Get the next autoincrement.
(I have no clue about the excact syntax because you din't mention the
database.)

For now I take that you find that number and store it in $ai, ok?

2) Create a new file with the right name ($ai.txt).
to do this:
- go to www.php.net
- look for the function fopen. (And when you are there, you can find all I
write here too, better, with more examples)

resource fopen ( string filename, string mode [, bool use_include_path [,
resource zcontext]])

So:
$filename = $ai.".txt";
$dir = "/home/dynamo/myfiles/"; // adjust to where you want to save
$fullpath = $dir.$filename;
$handle = fopen ($fullpath, "w+");

// now write
fwrite($handle, $_POST["text"]);

// and be a nice and close:
fclose($handle);
It is all on www.php.net
This is a good startingpoint:
http://nl3.php.net/manual/en/ref.filesystem.php

Please note that I didn't implement any errorhandling.
But you should.

Regards,
Erwin Moller

PS: I didn't test the code.


Thanks for that. Tried it and now working OK

Jul 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: BadOmen | last post by:
I have a text file that I want to save from my program. But I don't want to save the empty lines. I want to delete everything after the last character, Is that possible? Then when I read the...
3
by: Newbie | last post by:
I am trying to get the save/open dialog figured out. I am able open the save dialog but when I put in a file name (whatever.txt) and save the file does save with the name but it is blank. Below...
2
by: gnv | last post by:
Hi all, I am writing a cross-browser(i.e. 6 and netscape 7.1) javascript program to save an XML file to local file system. I have an xml string like below: var xmlStr = "<?xml version="1.0"...
0
by: a | last post by:
Save text file as html kloepper 17:42 23 Jul '04 I'm using httpwebresponse and a StringBuilder to return a stream that originates as a file with the .txt suffix (My download code converts the html...
4
by: Glenn M | last post by:
I have a shared XML file on a server . i also have one xslt file that performs a simple transform on in to view the data. now i want to have another page that lets users modify the shared xml...
8
by: DanB | last post by:
This is probably soooo simple but I can't seem to get it. I have a text file that I want users to download via a web page. I want the file to be saved to a default folder (or one that they...
10
by: GJP | last post by:
Hello. Ive been asked to make my own notepade for college assignment. All ig going well, but i cant get the save to work. I can get Save a (shows dialog box), i can get it to just save too,...
4
by: Jonny | last post by:
Hello Group How do I open a Save File Dialog from an ASPX page behind a browse button? Any help would be fantastic!! I am using ASP.NET 1.1 using VB.NET as the coding language TIA
2
by: nuhura01 | last post by:
Hi.. I'm trying to save query path which is in text box to text file. Below is the coding that i'm using currently: Private Sub SaveQueryPath() 'save to text file Response.Clear()...
3
by: =?Utf-8?B?YzY3NjIyOA==?= | last post by:
Hi all, I have a question for you. I have a .csv file which has many lines of data. Each line has many data fields which are delimited by ",". Now I need to extract part of data from this...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.