Friday, March 6, 2009

PHP and Form

PHP handles variable and upload file in HTML form differently.

1. For Variable:

Following is an example form in a HTML page that uses variable.php to process the form.

<FORM method="POST" action="variable.php">
<INPUT type="text" name="variable">
<INPUT type="submit" name="submit" value="Submit Name">

In the PHP script, variable.php, $_POST['variable'] will keep the value of the variable that user entered in the above form.

2. For File:

Following is an example form to upload a file from the HTML page.

<FORM enctype="multipart/form-data" method="POST" action="file.php">
<INPUT type="file" name="file" size="30">
<INPUT type="submit" name="submit" value="Upload File">

In the file.php script $_FILES will keep all information about the file, and details are shown below.

$_FILES['file']['name'] - The file name.
$_FILES['file']['tmp_name'] - The file name with the full path.
$_FILES['file']['size'] - The file size in Byte.
$_FILES['file']['type'] - The Mime type of the file.

Using above information, the PHP script can save the file to the file system or insert the file name with full path or content of the file into the MySQL database.

No comments: