Php help please


#1

I have a form that looks like two squares. How do I make it so when they type in both squares and hit accept, it creates a txt file in a specific folder. The txt file will be named what they typed into the first box, and what they typed in the second box will go inside the txt file.

Thanks in advance

-Shaw


#2

take care of securety, u would never wan’t to give the user the posybillty to name a file freely!!!

… but it is simple to do it.
[php]
if($file=fopen($_REQUEST[‘box1’],‘w’))
{
fwrite($file,$_REQUEST[‘box2’]);
fclose($file);
}
else
echo ‘error’;
[/php]
(changed: added fclose)

take a look at the file-funcions:
http://php.net/manual/en/ref.filesystem.php

P.S.: i don’t know how experenced you are? but for general form-submitting take a look at:
http://php.net/manual/en/tutorial.forms.php


#3

Be sure to include fclose() as well. Not sure how PHP ties up loose ends, but I’ve heard that in many languages that not closing a resource connection is generally a Bad Thing.


#4

I am noticed that there has been a lot of $_REQUEST being used, if my memory serves me correctly, I do believe that the recommendation is to avoid $_REQEUST if possible. Try and use the $_POST/$_GET variables.

I believe its because of security and it is just good coding practice. I can say from a business stand point that if someone uses $_REQUEST for everything and then leaves, someone else needs too come and work with the code, its a lot easier to see where variables are coming from when using $_GET/$_POST.

Just my 2 cents.


#5

Yep, it’s best to avoid $_REQUEST when fetching external values. There are cases in which using $_REQUEST is preferrable, but those are few and far between, and usually require the developer to really know what these are about and why it would be preferrable :wink:

It’s best to use $_GET/$_POST/etc, in order to clear up where the input is coming from. $_SERVER values are usually pretty reliable, $_GET/$_POST values are never :wink: Don’t mix them up by using $_REQUEST.