Uploading Files with PHP
To add a file upload control to a form, you need do nothing more than make sure that the method and enctype attributes of the form tag are set to "POST" and "multipart / form-data", respectively, and then add the following HTML to your form:
< input type="file" name="file1" /> location of the file on the server $file1 $HTTP_POST_FILES['file1']['tmp_name'] original name of the file on the user's computer $file1_name $HTTP_POST_FILES['file1']['name'] If available, type of the file $file1_type $HTTP_POST_FILES['file1']['type'] example ; // test to make sure we got a file if (!is_array($HTTP_POST_FILES['file1'])) { echo "You did not upload a file"; exit; } // if we got here, // we must have gotten // something $f =& $HTTP_POST_FILES['file1']; $dest_dir = 'uploads'; $dest = $dest_dir . '/' . $f['name']; // make sure we can actually // put the fie where it's supposed // to go if (!is_uploaded_file ($f['tmp_name'])) { echo "Error: no file uploaded."; exit; } // all clear, move the file // to its permanent location $r = move_uploaded_file ($f ['tmp_name'], $dest); if ($r == false) { // something's wrong echo "Error: could not copy file "; } |

