Go Back   Computer Forums > Web Design | Website Development > Web Programming
Join Computer forums Today

Thread Tools Search this Thread Display Modes
Old 04-10-2011, 05:22 AM   #1
Beta Member
Join Date: Mar 2009
Posts: 5
Default How to save a variable data into text file using PHP

I'm trying to write a code that takes two variables entered by the user via input boxes and saves them in a text file, without overwriting other entries. Unfortunately the strings are not showing up in the text file. Heres the code I'm using

Originally Posted by PHP
$handle = fopen("datalog.txt", "a");
foreach($_POST as $variable => $value) {
fwrite($handle, $variable);
fwrite($handle, "=");
fwrite($handle, $value);
fwrite($handle, "\r\n");
fwrite($handle, "\r\n");
I am new to PHP, and I'm sure I'm making a dumb mistake. Any advice would be greatly appreciated.

H3mp is offline   Reply With Quote
Old 04-13-2011, 11:44 PM   #2
Solid State Member
Join Date: Feb 2011
Posts: 17
Default Re: How to save a variable data into text file using PHP

The PHP may not have read/write permissions for your file. If you didn't it would probably throw some kind of error, but depending on the context you may not see it (eg via AJAX). Try outputing some values (ie the return values for fopen, fwrite and fclose) and maybe making the code a bit more robust by throwing in some ifs like

$test_var = is_readable("datalog.txt");
echo "Is Readable: ".$test_var;
$handle = fopen("datalog.txt","a");
echo "File is ".$handle;
if($handle !== FALSE)
//print stuff to file
$test_var = fclose($handle);
echo "File Close: ".$test_var;
Something like that should help you trace the source of your error. I'm assuming the text file is in same folder as the php file, otherwise you have to specify the location more specifically.

Just a side note, because of the carriage return with the newline character I'm assuming you're processing this file on a windows system?
hamalnamal is offline   Reply With Quote

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

All times are GMT -5. The time now is 03:48 AM.

Powered by vBulletin® Version 3.8.8 Beta 4
Copyright ©2000 - 2016, vBulletin Solutions, Inc.
Search Engine Friendly URLs by vBSEO 3.6.0