| Coding Forum Problems with your code? Let's hear about it. |
04-26-2007, 08:36 AM
|
#1 (permalink)
|
|
Inactive
Join Date: 11-07-06
Location: Springfield
Posts: 111
|
Using PHP to create a .txt file
My goal is to have an HTML form that create a text file. The problem I am facing is that i want to get the file name from one of the form fields.
Is there anyway to do that?
|
|
|
04-26-2007, 09:11 AM
|
#2 (permalink)
|
|
Inactive
Join Date: 04-10-07
Location: www.webfoyers.com
Posts: 68
Latest Blog: None
|
do you want to append the text file everytime it was submitted? or create another text file?
Quote:
|
The problem I am facing is that i want to get the file name from one of the form fields
|
name of form fields or from user input?
ill get back to you as soon as this is clear.
if possible post all of form fields involved so i can give you the more clear answer
|
|
|
04-26-2007, 12:18 PM
|
#3 (permalink)
|
|
Contributing Member
Join Date: 04-20-06
Posts: 333
Latest Blog: None
|
Quote:
Originally Posted by daniel0012
My goal is to have an HTML form that create a text file. The problem I am facing is that i want to get the file name from one of the form fields.
Is there anyway to do that?
|
Explain exactly what you want to accomplish, and I'll tell you how to accomplish it. 
|
|
|
04-26-2007, 12:44 PM
|
#4 (permalink)
|
|
Inactive
Join Date: 11-07-06
Location: Springfield
Posts: 111
|
I have a form with the fields,
event
date
time
description
file
When thats proccesed, I want it to create a text file with the name the user put in the file box. I want the text file to look like this
Code:
<tr>
<td><b>
Event
</b></td>
</tr>
<tr>
<td>
Date
</td>
</tr>
<tr>
<td>
Time
</td>
</tr>
<tr>
<td style="padding-bottom: 10px;">
"Description"
</td>
</tr>
<br />
which will be put into a directory.
|
|
|
04-26-2007, 01:04 PM
|
#5 (permalink)
|
|
Contributing Member
Join Date: 01-02-07
Location: PA, USA
Posts: 194
Latest Blog: None
|
Do you have PHP? If so then creating this will be simple.
__________________
Need a page made? Draw a diagram, I suggest using Paint, show the picture with your post, it'll help a lot more than you think. Other questions? Draw a diagram for that too!
|
|
|
04-26-2007, 01:06 PM
|
#6 (permalink)
|
|
Contributing Member
Join Date: 04-20-06
Posts: 333
Latest Blog: None
|
PHP Code:
// Make the filename safe for the OS (remove anything but letters, numbers, ., _, and -) $file = preg_replace ('/[^a-zA-Z0-9\._-]/', '', $_POST['file']);
$cont = '<tr><td><b>'.$_POST['event'].'</b></td></tr><tr><td>'.$_POST['date'].'</td></tr><tr><td>'.$_POST['time'].'</td></tr><tr><td style="padding-bottom: 10px;">"'.$_POST['description'].'"</td> </tr><br />';
$f = fopen ($file, 'w'); fputs ($f, $cont); fclose ($f);
That's very simplistic, you need to sanitize all user input before using it, but it'll give you an idea of how to go about it. Note that I'm using the $_POST supervariable to access the data, change this if you're submitting the form via GET.
|
|
|
04-26-2007, 01:12 PM
|
#7 (permalink)
|
|
Inactive
Join Date: 11-07-06
Location: Springfield
Posts: 111
|
Quote:
Originally Posted by exam
PHP Code:
// Make the filename safe for the OS (remove anything but letters, numbers, ., _, and -)
$file = preg_replace ('/[^a-zA-Z0-9\._-]/', '', $_POST['file']);
$cont = '<tr><td><b>'.$_POST['event'].'</b></td></tr><tr><td>'.$_POST['date'].'</td></tr><tr><td>'.$_POST['time'].'</td></tr><tr><td style="padding-bottom: 10px;">"'.$_POST['description'].'"</td> </tr><br />';
$f = fopen ($file, 'w');
fputs ($f, $cont);
fclose ($f);
That's very simplistic, you need to sanitize all user input before using it, but it'll give you an idea of how to go about it. Note that I'm using the $_POST supervariable to access the data, change this if you're submitting the form via GET.
|
Would this have to be in the same directory that the text files are going?
|
|
|
04-26-2007, 04:02 PM
|
#8 (permalink)
|
|
Contributing Member
Join Date: 04-20-06
Posts: 333
Latest Blog: None
|
As it is now, yes. But you can just prepend an absolute path to the beginning of the $file variable, and put it where ever you want.
|
|
|
04-27-2007, 08:59 AM
|
#9 (permalink)
|
|
Inactive
Join Date: 11-07-06
Location: Springfield
Posts: 111
|
How would I do that?
|
|
|
04-27-2007, 11:35 AM
|
#10 (permalink)
|
|
Contributing Member
Join Date: 04-20-06
Posts: 333
Latest Blog: None
|
For example, you could do:
$file = 'files/'.$file;
After the preg_replace line, to put all the files in a folder called "files" in the current directory.
|
|
|
04-27-2007, 12:56 PM
|
#11 (permalink)
|
|
Inactive
Join Date: 11-07-06
Location: Springfield
Posts: 111
|
I get the error
Quote:
Warning: fopen(.../events/) [function.fopen]: failed to open stream: No such file or directory in /home/scabapti/public_html/test/add/add_event_form.php on line 29
Warning: fputs(): supplied argument is not a valid stream resource in /home/scabapti/public_html/test/add/add_event_form.php on line 30
Warning: fclose(): supplied argument is not a valid stream resource in /home/scabapti/public_html/test/add/add_event_form.php on line 31
|
Lines 29-31 are
Code:
$f = fopen ($file, 'w');
fputs ($f, $cont);
fclose ($f);
|
|
|
04-27-2007, 02:28 PM
|
#12 (permalink)
|
|
Contributing Member
Join Date: 01-02-07
Location: PA, USA
Posts: 194
Latest Blog: None
|
create the directory events and let us know if you're still receiving this error.
__________________
Need a page made? Draw a diagram, I suggest using Paint, show the picture with your post, it'll help a lot more than you think. Other questions? Draw a diagram for that too!
|
|
|
04-27-2007, 10:16 PM
|
#13 (permalink)
|
|
Inactive
Join Date: 11-07-06
Location: Springfield
Posts: 111
|
I did and it still shows the same errors.
Code:
$file = preg_replace ('/[^a-zA-Z0-9\._-]/', '', $_POST['file']);
$file = '../'.'events/'.$file;
Is that code correct?
|
|
|
04-28-2007, 09:56 AM
|
#14 (permalink)
|
|
Inactive
Join Date: 04-10-07
Location: www.webfoyers.com
Posts: 68
Latest Blog: None
|
try this instead;
$file = preg_replace ('/[^a-zA-Z0-9\._-]/', '', $_POST['file']);
$file = $_SERVER['DOCUMENT_ROOT'].'/event/'.$file
|
|
|
04-28-2007, 05:12 PM
|
#15 (permalink)
|
|
Inactive
Join Date: 11-07-06
Location: Springfield
Posts: 111
|
Now I'm not getting any errors but it isn't creating the file.
Here is the code:
Code:
<?php
$event = $_POST['event'];
$date = $_POST['date'];
$time = $_POST['time'];
$description = $_POST['description'];
$file = preg_replace ('/[^a-zA-Z0-9\._-]/', '', $_POST['file']);
$file = $_SERVER['DOCUMENT_ROOT'].'/event/'.$file;
$cont = '<tr><td><b>'.$_POST['event'].'</b></td></tr><tr><td>'.$_POST['date'].'</td></tr><tr><td>'.$_POST['time'].'</td></tr><tr><td style="padding-bottom: 10px;">"'.$_POST['description'].'"</td> </tr><br />';
$f = fopen ($file, 'w');
fputs ($f, $cont);
fclose ($f);
?>
|
|
|
04-29-2007, 02:26 PM
|
#16 (permalink)
|
|
Inactive
Join Date: 04-10-07
Location: www.webfoyers.com
Posts: 68
Latest Blog: None
|
do you have a file permission?
what are your error reporting settings?
there must be something wrong with the code.
at the top write this.
error_reporting(E_ALL);
see if it will give you an error
|
|
|
04-29-2007, 06:36 PM
|
#17 (permalink)
|
|
Inactive
Join Date: 11-07-06
Location: Springfield
Posts: 111
|
Quote:
Notice: Undefined index: event in /home/scabapti/public_html/test/add/add_event_form.php on line 20
Notice: Undefined index: date in /home/scabapti/public_html/test/add/add_event_form.php on line 21
Notice: Undefined index: time in /home/scabapti/public_html/test/add/add_event_form.php on line 22
Notice: Undefined index: description in /home/scabapti/public_html/test/add/add_event_form.php on line 23
Notice: Undefined index: file in /home/scabapti/public_html/test/add/add_event_form.php on line 24
Notice: Undefined index: event in /home/scabapti/public_html/test/add/add_event_form.php on line 27
Notice: Undefined index: date in /home/scabapti/public_html/test/add/add_event_form.php on line 27
Notice: Undefined index: time in /home/scabapti/public_html/test/add/add_event_form.php on line 27
Notice: Undefined index: description in /home/scabapti/public_html/test/add/add_event_form.php on line 27
|
I changed the permissions and I got this error.
|
|
|
04-29-2007, 06:51 PM
|
#18 (permalink)
|
|
Inactive
Join Date: 04-10-07
Location: www.webfoyers.com
Posts: 68
Latest Blog: None
|
the problem was clear your POST variable was empty..
check you form and make sure you have method="post" attribute or else it will be defaulted to GET method.
|
|
|
04-29-2007, 07:06 PM
|
#19 (permalink)
|
|
Inactive
Join Date: 11-07-06
Location: Springfield
Posts: 111
|
Code:
<form action="add_event_form.php" method="post">
This is the first line of the form.
|
|
|
04-29-2007, 07:16 PM
|
#20 (permalink)
|
|
Inactive
Join Date: 04-10-07
Location: www.webfoyers.com
Posts: 68
Latest Blog: None
|
that was weird. use:
var_dump($_POST);
to verify if your post variable was not empty
also is your php code and your form in different file?
also check if your form tag was properly closed.
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|