Apr 222012
 
Article PHP

(Leer este artículo en español)

The parameters sent by means of a HTML form to our server are accessible through a predefined array $_POST.

For instance, if we create a form with fields:

  • Text field ‘name’
  • Radio button ‘genre’ with values ‘Male’ and ‘Female’
  • Select field ‘country’ with options ‘Portugal’ and ‘Brazil’

The HTML code of our form is:

And the PHP code of our  “process_form.php” script is:

Processing of file uploads

The HTML form may also have a file to upload files (documents, images,…) to our server.

In this case, the information required to process the data uploaded is in the variable $_FILES (an associative array). If the HTML form is as follows:

When this form is used to upload a file named ‘data.txt’, the $_FILES variable holds the following information:

As we can see, the original filename is in:

$_FILES[‘uploadfile’][‘name’]

and the temporary filename where the data has been uploaded in the server is in:

$_FILES[‘uploadfile’][‘tmp_name’].

Then, we just need to call the function ‘move_uploaded_file’ to change the name of the uploaded file and move it were we want to keep it.

The full script to process file uploads is:

Index of posts related to PHP programming

 Posted by at 7:45 pm

  One Response to “How to process HTML forms in PHP”

 Leave a Reply

(required)

(required)