5. POST register.jsp HTTP/1.1 Host: hi/iq User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.2) Gecko/20021126 Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8, video/x-mng,image/png,image/jpeg,image/gif;q=0.2,text/css,*/*;q=0.1 Accept-Language: en-us, en;q=0.50 Accept-Encoding: gzip, deflate, compress;q=0.9 Accept-Charset: ISO-8859-1, utf-8;q=0.66, *;q=0.66 Keep-Alive: 300 Connection: keep-alive Content-Type: multipart/form-data; boundary=---------------------------29772313742745 Content-Length: 452 -----------------------------29772313742745 Content-Disposition: form-data; name="name" J.Doe -----------------------------29772313742745 Content-Disposition: form-data; name="email" [email_address] -----------------------------29772313742745 Content-Disposition: form-data; name="file-upload"; filename="test.txt" Content-Type: text/plain test data with some high ascii: ¿Como estás? -----------------------------29772313742745-- An example of data sent via POST method with enctype="multipart/form-data" (Copied from http://www.devx.com/Java/Article/17679/0/page/2 )
6. The $_FILES Array The error code associated with any problem. error The temporary filename of the uploaded file as it was stored on the server. tmp_name The size of the uploaded file in bytes. size The MIME type of the file, as provided by the browser. type The original name of the file (as it was on the user's computer). name Meaning Index
7. Processing the uploaded items // "upload" is the name assigned to the input element, as in // <input type="file" name="upload" /> if (isset( $_FILES['upload'] )) { if ( $_FILES['upload']['error'] > 0)) { // File upload fails. See next slide for detailed info about the // meaning of the error code. } else { // e.g., only allows JPEG image files to be uploaded // Note: This is not a complete list of MIME types for JPEG images $allowed = array('image/jpeg', 'image/jpg'); // Continue next page …
8. Processing the uploaded items ( … continue) if (in_array( $_FILES['upload']['type'] , $allowed)) { $tmp = $_FILES['upload']['tmp_name'] ; $dst = "C:/uploads/{ $_FILES['upload']['name'] }"; if ( move_upload_file ($tmp, $dst)) { // Success ! } } } // End of else // Manually delete the temporary uploaded file if // it still exists $tmp = $_FILES['upload']['tmp_name'] ; if (file_exists($tmp) && is_file($tmp)) unlink($tmp); } Note: move_uploaded_file() will overwrite an existing file without warning.