Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP form to create text file

Status
Not open for further replies.

KnightWolfJK

IS-IT--Management
Joined
Jun 6, 2003
Messages
8
Location
US
Can anyone point me to an example or tutorial on creating a PHP form that will create a delimited txt file and place it in /folderX?

For example, filling out the form would create file.txt that would look like:

Code:
entry_one|entry_two|entry_three

I need to support spaces and puncuation in some fields.

Thanks in advance
 
have u searched this forum?

Known is handfull, Unknown is worldfull
 
A search for "delimited" returned 0 hits, thus the post.
 
nope search for "create files"...

Known is handfull, Unknown is worldfull
 
Nope, still zero results.

Have u searched this forum?
 
Okay, firewall shut off, search working. Sorry, jackass I is says I
 
To create a file like what you mentioned
Please have a look at this code..

$fp = fopen('file.txt',"a+");
$f = "entry_one|entry_two|entry_three\r\n";
$s = fwrite($fp,"$f");
$s = fclose($fp);

Hope this helps..Thanx
 
I pretty much have the file writing part down... and passing the variable from the form... I'm stuck on combining the form variable with the delimiter, though.

I have
Code:
$field1 = $HTTP_POST_VARS['text'];
$delimiter = "|";
$handle = fopen ("file.txt", "w");
$output = implode($delimiter, $field1);
fwrite($handle, $output);
fclose($handle);
and I'm getting Bad arguments to implode().

I checked php.net/implode, seems like I'm doing it right. Obviously not though, eh?
 
is $field1 an array?

Known is handfull, Unknown is worldfull
 
No, $field1 is the text from the form on the previous page...


Okay, wait... Use the form to add variables to an array, then write the whole array to the text file... Catching on slowly. Hang on, let me see if I can work this out.

For what it's worth, I looked for the noob board. =0)
 
Say your form is:

Code:
<form name=&quot;form&quot; method=&quot;post&quot; action=&quot;blah.php&quot;>
  <input type=&quot;text&quot; name=&quot;blah1&quot;>
  <br>
  <input type=&quot;text&quot; name=&quot;blah2&quot;>
  <br>
  <input type=&quot;text&quot; name=&quot;blah3&quot;>
  <br>
  <input type=&quot;submit&quot; name=&quot;Submit&quot; value=&quot;Submit&quot;>
</form>

Your code can be like:

Code:
$output = $blah1.&quot;|&quot;.$blah2.&quot;|&quot;.$blah3.&quot;\n&quot;;
$handle = fopen (&quot;file.txt&quot;, &quot;w&quot;);
fwrite($handle, $output);
fclose($handle);


 
Tested it, and it works.

Code:
$output = $blah1.&quot;|&quot;.$blah2.&quot;|&quot;.$blah3.&quot;\r\n&quot;;
$handle = fopen (&quot;file.txt&quot;, &quot;w&quot;);
fwrite($handle, $output);
fclose($handle);


but dont forget that if you want to keep on adding to the file, you need to change


$handle = fopen (&quot;file.txt&quot;, &quot;w&quot;);


To

$handle = fopen (&quot;file.txt&quot;, &quot;a&quot;);
 
I've had success with
Code:
$field[1] = $HTTP_POST_VARS['sku'];
//..snipped..//
$field[16] = $HTTP_POST_VARS['keywords'];
$delimiter = &quot;|&quot;;
$handle = fopen (&quot;file.txt&quot;, &quot;w&quot;);
$output = implode($delimiter, $field);
fwrite($handle, $output);
fclose($handle);

But now I'm trying to work out a method to dynamically write the filename based on whatever the user enters into the sku field.

I've tried
Code:
$HTTP_POST_VARS['sku']; = $filename;
//.. and then..//
$handle = fopen ($filename, &quot;w&quot;);
but I'm aware that not only does this not work, it doesn't include the .txt extension I'm looking for.

Any suggestions? Thanks
 
try this:
$filename=$_POST['sku'];
//.. and then..//
$handle = fopen ($filename.&quot;.txt&quot;, &quot;w&quot;);





Known is handfull, Unknown is worldfull
 
Thanks vbkris, that worked like a charm. And thanks also to sleipner214 for the operators guide. Hadn't quite gotten that far yet in the man
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top