Current location: Hot Scripts Forums » General Community » Script Requests » PHP write to text file


PHP write to text file

Reply
  #1 (permalink)  
Old 02-28-04, 12:25 PM
veeco veeco is offline
Newbie Coder
 
Join Date: Feb 2004
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
PHP write to text file

I came up with this php script to get 3 fields from an HTML form. I want the text field "name" to be the name of the txt file to write to. Here is my code:

<?php
$name = @$_POST["name"];
$file = "$name" . ".txt";
$v1 = @$_POST["v1"];
$v2 = @$_POST["v2"];


// Set the string to be written to the file
$values = "&v1=$v1\r\n";
$values .= "&v2=$v2\r\n";

// Open the file for truncated writing
$fp = @fopen($file, "w") or die("Couldn't open $file for writing!");
$numBytes = @fwrite($fp, $values) or die("Couldn't write values to file!");

@fclose($fp);
echo "Wrote $numBytes bytes to $file successfully!";

?>

$file is the name of the file. So if $name is aaron, it will write to aaron.txt, but my code is wrong, and I don't know how to fix it (I'm very new to php). Can anyone help?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 03-02-04, 02:14 PM
digioz's Avatar
digioz digioz is offline
Community VIP
 
Join Date: Oct 2003
Location: Chicago, IL
Posts: 2,167
Thanks: 3
Thanked 8 Times in 8 Posts
Try this one:

PHP Code:

<?php
 
$name 
$_POST["name"]; 
$v1 $_POST["v1"]; 
$v2 $_POST["v2"]; 
 
$file "$name.txt";
 
$values "$v1\r\n $v2\r\n"
 
$fp fopen($file"w") or die("Couldn't open $file for writing!"); 
$numBytes fwrite($fp$values) or die("Couldn't write values to file!"); 
 
fclose($fp); 
echo 
"Wrote $numBytes bytes to $file successfully!"
 
?>




Quote:
Originally Posted by veeco
I came up with this php script to get 3 fields from an HTML form. I want the text field "name" to be the name of the txt file to write to. Here is my code:

<?php
$name = @$_POST["name"];
$file = "$name" . ".txt";
$v1 = @$_POST["v1"];
$v2 = @$_POST["v2"];


// Set the string to be written to the file
$values = "&v1=$v1\r\n";
$values .= "&v2=$v2\r\n";

// Open the file for truncated writing
$fp = @fopen($file, "w") or die("Couldn't open $file for writing!");
$numBytes = @fwrite($fp, $values) or die("Couldn't write values to file!");

@fclose($fp);
echo "Wrote $numBytes bytes to $file successfully!";

?>

$file is the name of the file. So if $name is aaron, it will write to aaron.txt, but my code is wrong, and I don't know how to fix it (I'm very new to php). Can anyone help?
__________________
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 02-10-10, 03:48 PM
Wapfuture10 Wapfuture10 is offline
New Member
 
Join Date: Feb 2010
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
could you please help me i want to make a form that can create text files
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP code to edit a text file mdhall Script Requests 12 12-23-10 05:03 AM
How to write a php script to load a text file into a table automatically? xmxpcom PHP 2 02-12-04 09:37 AM
php write to file tiny Script Requests 2 01-14-04 10:12 PM
picking random entries with a filter... Double selection problem dsumpter PHP 7 11-16-03 08:19 PM
PHP - Dir listing and write to file NickyWhicko PHP 4 08-24-03 03:42 AM


All times are GMT -5. The time now is 03:30 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.