Webmaster Forum


Go Back   Webmaster Forum > Web Development > Web Design Lobby > Coding Forum
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Coding Forum Problems with your code? Let's hear about it.

Lionsanime Directory   ClickBooth Network   V7N Directory

Reply
 
LinkBack Thread Tools Display Modes
Old 04-26-2007, 08:36 AM   #1 (permalink)
Inactive
 
Join Date: 11-07-06
Location: Springfield
Posts: 111
iTrader: 0 / 0%
daniel0012 is on the right pathdaniel0012 is on the right path
Send a message via AIM to daniel0012 Send a message via MSN to daniel0012 Send a message via Yahoo to daniel0012
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?
daniel0012 is offline  
Add Post to del.icio.us
Reply With Quote
Sponsored Links
SEO Hosting by HostGator  Advertise Here  Buy Blog Links
Old 04-26-2007, 09:11 AM   #2 (permalink)
Inactive
 
Join Date: 04-10-07
Location: www.webfoyers.com
Posts: 68
iTrader: 0 / 0%
Latest Blog:
None

brealmz is liked by somebodybrealmz is liked by somebodybrealmz is liked by somebody
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
brealmz is offline  
Add Post to del.icio.us
Reply With Quote
Old 04-26-2007, 12:18 PM   #3 (permalink)
Contributing Member
 
exam's Avatar
 
Join Date: 04-20-06
Posts: 333
iTrader: 0 / 0%
Latest Blog:
None

exam is a highly respected web proexam is a highly respected web proexam is a highly respected web proexam is a highly respected web proexam is a highly respected web proexam is a highly respected web proexam is a highly respected web proexam is a highly respected web proexam is a highly respected web proexam is a highly respected web proexam is a highly respected web pro
Quote:
Originally Posted by daniel0012 View Post
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.
exam is offline  
Add Post to del.icio.us
Reply With Quote
Old 04-26-2007, 12:44 PM   #4 (permalink)
Inactive
 
Join Date: 11-07-06
Location: Springfield
Posts: 111
iTrader: 0 / 0%
daniel0012 is on the right pathdaniel0012 is on the right path
Send a message via AIM to daniel0012 Send a message via MSN to daniel0012 Send a message via Yahoo to daniel0012
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> &nbsp; </tr> <br />
which will be put into a directory.
daniel0012 is offline  
Add Post to del.icio.us
Reply With Quote
Old 04-26-2007, 01:04 PM   #5 (permalink)
Contributing Member
 
Arenlor's Avatar
 
Join Date: 01-02-07
Location: PA, USA
Posts: 194
iTrader: 0 / 0%
Latest Blog:
None

Arenlor is liked by somebodyArenlor is liked by somebodyArenlor is liked by somebodyArenlor is liked by somebody
Send a message via ICQ to Arenlor Send a message via AIM to Arenlor Send a message via MSN to Arenlor Send a message via Yahoo to Arenlor
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!
Arenlor is offline  
Add Post to del.icio.us
Reply With Quote
Old 04-26-2007, 01:06 PM   #6 (permalink)
Contributing Member
 
exam's Avatar
 
Join Date: 04-20-06
Posts: 333
iTrader: 0 / 0%
Latest Blog:
None

exam is a highly respected web proexam is a highly respected web proexam is a highly respected web proexam is a highly respected web proexam is a highly respected web proexam is a highly respected web proexam is a highly respected web proexam is a highly respected web proexam is a highly respected web proexam is a highly respected web proexam is a highly respected web pro
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>&nbsp;  </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.
exam is offline  
Add Post to del.icio.us
Reply With Quote
Old 04-26-2007, 01:12 PM   #7 (permalink)
Inactive
 
Join Date: 11-07-06
Location: Springfield
Posts: 111
iTrader: 0 / 0%
daniel0012 is on the right pathdaniel0012 is on the right path
Send a message via AIM to daniel0012 Send a message via MSN to daniel0012 Send a message via Yahoo to daniel0012
Quote:
Originally Posted by exam View Post
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>&nbsp;  </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?
daniel0012 is offline  
Add Post to del.icio.us
Reply With Quote
Old 04-26-2007, 04:02 PM   #8 (permalink)
Contributing Member
 
exam's Avatar
 
Join Date: 04-20-06
Posts: 333
iTrader: 0 / 0%
Latest Blog:
None

exam is a highly respected web proexam is a highly respected web proexam is a highly respected web proexam is a highly respected web proexam is a highly respected web proexam is a highly respected web proexam is a highly respected web proexam is a highly respected web proexam is a highly respected web proexam is a highly respected web proexam is a highly respected web pro
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.
exam is offline  
Add Post to del.icio.us
Reply With Quote
Old 04-27-2007, 08:59 AM   #9 (permalink)
Inactive
 
Join Date: 11-07-06
Location: Springfield
Posts: 111
iTrader: 0 / 0%
daniel0012 is on the right pathdaniel0012 is on the right path
Send a message via AIM to daniel0012 Send a message via MSN to daniel0012 Send a message via Yahoo to daniel0012
How would I do that?
daniel0012 is offline  
Add Post to del.icio.us
Reply With Quote
Old 04-27-2007, 11:35 AM   #10 (permalink)
Contributing Member
 
exam's Avatar
 
Join Date: 04-20-06
Posts: 333
iTrader: 0 / 0%
Latest Blog:
None

exam is a highly respected web proexam is a highly respected web proexam is a highly respected web proexam is a highly respected web proexam is a highly respected web proexam is a highly respected web proexam is a highly respected web proexam is a highly respected web proexam is a highly respected web proexam is a highly respected web proexam is a highly respected web pro
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.
exam is offline  
Add Post to del.icio.us
Reply With Quote
Old 04-27-2007, 12:56 PM   #11 (permalink)
Inactive
 
Join Date: 11-07-06
Location: Springfield
Posts: 111
iTrader: 0 / 0%
daniel0012 is on the right pathdaniel0012 is on the right path
Send a message via AIM to daniel0012 Send a message via MSN to daniel0012 Send a message via Yahoo to daniel0012
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);
daniel0012 is offline  
Add Post to del.icio.us
Reply With Quote
Old 04-27-2007, 02:28 PM   #12 (permalink)
Contributing Member
 
Arenlor's Avatar
 
Join Date: 01-02-07
Location: PA, USA
Posts: 194
iTrader: 0 / 0%
Latest Blog:
None

Arenlor is liked by somebodyArenlor is liked by somebodyArenlor is liked by somebodyArenlor is liked by somebody
Send a message via ICQ to Arenlor Send a message via AIM to Arenlor Send a message via MSN to Arenlor Send a message via Yahoo to Arenlor
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!
Arenlor is offline  
Add Post to del.icio.us
Reply With Quote
Old 04-27-2007, 10:16 PM   #13 (permalink)
Inactive
 
Join Date: 11-07-06
Location: Springfield
Posts: 111
iTrader: 0 / 0%
daniel0012 is on the right pathdaniel0012 is on the right path
Send a message via AIM to daniel0012 Send a message via MSN to daniel0012 Send a message via Yahoo to daniel0012
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?
daniel0012 is offline  
Add Post to del.icio.us
Reply With Quote
Old 04-28-2007, 09:56 AM   #14 (permalink)
Inactive
 
Join Date: 04-10-07
Location: www.webfoyers.com
Posts: 68
iTrader: 0 / 0%
Latest Blog:
None

brealmz is liked by somebodybrealmz is liked by somebodybrealmz is liked by somebody
try this instead;
$file = preg_replace ('/[^a-zA-Z0-9\._-]/', '', $_POST['file']);
$file = $_SERVER['DOCUMENT_ROOT'].'/event/'.$file
brealmz is offline  
Add Post to del.icio.us
Reply With Quote
Old 04-28-2007, 05:12 PM   #15 (permalink)
Inactive
 
Join Date: 11-07-06
Location: Springfield
Posts: 111
iTrader: 0 / 0%
daniel0012 is on the right pathdaniel0012 is on the right path
Send a message via AIM to daniel0012 Send a message via MSN to daniel0012 Send a message via Yahoo to daniel0012
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>&nbsp; </tr><br />'; $f = fopen ($file, 'w'); fputs ($f, $cont); fclose ($f); ?>
daniel0012 is offline  
Add Post to del.icio.us
Reply With Quote
Old 04-29-2007, 02:26 PM   #16 (permalink)
Inactive
 
Join Date: 04-10-07
Location: www.webfoyers.com
Posts: 68
iTrader: 0 / 0%
Latest Blog:
None

brealmz is liked by somebodybrealmz is liked by somebodybrealmz is liked by somebody
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
brealmz is offline  
Add Post to del.icio.us
Reply With Quote
Old 04-29-2007, 06:36 PM   #17 (permalink)
Inactive
 
Join Date: 11-07-06
Location: Springfield
Posts: 111
iTrader: 0 / 0%
daniel0012 is on the right pathdaniel0012 is on the right path
Send a message via AIM to daniel0012 Send a message via MSN to daniel0012 Send a message via Yahoo to daniel0012
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.
daniel0012 is offline  
Add Post to del.icio.us
Reply With Quote
Old 04-29-2007, 06:51 PM   #18 (permalink)
Inactive
 
Join Date: 04-10-07
Location: www.webfoyers.com
Posts: 68
iTrader: 0 / 0%
Latest Blog:
None

brealmz is liked by somebodybrealmz is liked by somebodybrealmz is liked by somebody
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.
brealmz is offline  
Add Post to del.icio.us
Reply With Quote
Old 04-29-2007, 07:06 PM   #19 (permalink)
Inactive
 
Join Date: 11-07-06
Location: Springfield
Posts: 111
iTrader: 0 / 0%
daniel0012 is on the right pathdaniel0012 is on the right path
Send a message via AIM to daniel0012 Send a message via MSN to daniel0012 Send a message via Yahoo to daniel0012
Code:
<form action="add_event_form.php" method="post">
This is the first line of the form.
daniel0012 is offline  
Add Post to del.icio.us
Reply With Quote
Old 04-29-2007, 07:16 PM   #20 (permalink)
Inactive
 
Join Date: 04-10-07
Location: www.webfoyers.com
Posts: 68
iTrader: 0 / 0%
Latest Blog:
None

brealmz is liked by somebodybrealmz is liked by somebodybrealmz is liked by somebody
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.
brealmz is offline  
Add Post to del.icio.us
Reply With Quote
Go Back   Webmaster Forum > Web Development > Web Design Lobby > Coding Forum

Reply



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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
how to create 404 file on win server Jalpari