Homework assignment #4.

In this assignment you need to handle the file with the image you are uploading to the web server. When you used the arrays $_POST in your PHP script that handles the information passed by the image input form, you may notice that information about the file name entered into the input element of the type file is not there. This happens because in PHP there is a special array that contains this kind of information. This is the $_FILES array. This array is slightly different from the arrays $_GET and $_POST we have considered. Since the browser sends several pieces of information about each file you uploading to the server (such as name, size, type, etc) this array is a 2 dimensional array. Both dimensions are string-indexed. The first dimension index is just the name of file input element. For example, if in my HTML form I have

 <input type="file" name="my_image" />
then in my PHP script I would access info about the file as $_FILES["my_image"]. Since more than one piece of information is sent about the file, $_FILES["my_image"] is an array by itself. Thus, in order to access the size information I will use $_FILES["my_image"]["size"]. To access the name of the file I would use $_FILES["my_image"]["name"]. The table below contains all the elements of the array and their string indexes.
Index Description
name The original name of the file on the client machine.
type The mime type of the file, if the browser provided this information. An example would be "image/gif". This mime type is however not checked on the PHP side and therefore don't take its value for granted.
size The size, in bytes, of the uploaded file.
tmp_name The temporary filename of the file in which the uploaded file was stored on the server.
error The error code associated with this file upload. This element was added in PHP 4.2.0
Once a file uploaded to the server, it will be saved by the browser into a temp directory (which depends on the server settings) under a temp name. In order to get the temp name we will, of course, use $_FILES["my_image"]["tmp_name"]. If the file has been successfully uploaded, then it's the script's responsibility to move it into a permanent location. This can be done with special function move_uploaded_file(). This function takes two arguments: the source file and the destination file. For example, if I want to move the file temporarily stored in a temp folder in the "img" subfolder of the folder where my script resides, I would execute:
  move_uploaded_file($_FILES["my_image"]["tmp_name"], "img/" . basename($_FILES["my_image"]["name"]));
If the file has been successfully moved the function returns true and it returns false otherwise. You can use the return value to determine what kind of message to print back to the user. Please find more information about handling file uploads on the PHP site.

The actual assignment for this homework would be the following. Create a PHP script that takes all the data sent by the input form and:

Please don't forget to use method POST for the <form> element and also set enctype attribute to multipart/form-data.