If you're anything like me, then you'll feel curious about how to include an effective error checking mechanism inside the file uploading script that I built in the preceding article of this series. Thus, let me quickly show you the complete source code of this application, including the corresponding web form that permits users to browse files in their respective client machines. Here are the source files that comprise the file uploading application: (definition of 'upload_form.htm' file) <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Uploading files with PHP</title> <style type="text/css"> body{ padding: 0; margin: 0; background: #fff; } h1{ font: bold 16pt Arial, Helvetica, sans-serif; color: #000; text-align: center; } p{ font: normal 10pt Arial, Helvetica, sans-serif; color: #000; } form{ display: inline; } #formcontainer{ width: 50%; padding: 10px; margin-left: auto; margin-right: auto; background: #eee; border: 1px solid #666; } </style> </head> <body> <h1>Uploading files with PHP</h1> <div id="formcontainer"> <form enctype="multipart/form-data" action="upload_file.php" method="post"> <input type="hidden" name="MAX_FILE_SIZE" value="2000000" /> <p>File to upload <input name="userfile" type="file" /> <input type="submit" name="send" value="Upload File" /></p> </form> </div> </body> </html> (definition of 'upload_file.php' file)
if($_POST['send']){ // set upload directory (for Windows users) $uploadDir='C:uploaded_files'; // set destination of uploaded file $uploadFile=$uploadDir.basename($_FILES['userfile']['name']); if(move_uploaded_file($_FILES['userfile']['tmp_name'],$uploadFile)){ echo 'The target file was successfully uploaded!<br />'; echo 'Name of uploaded file: '.$_FILES['userfile']['name'].'.<br />'; echo 'MIME type of uploaded file: '.$_FILES['userfile']['type'].'.<br />'; echo 'Size of uploaded file: '.$_FILES['userfile']['size'].' bytes.<br />'; echo 'Temporary name of uploaded file: '.$_FILES['userfile']['tmp_name'].'<br />'; } else{ echo 'Error uploading target file!'; } } As you'll possibly recall, the two source files listed above are all that you need to build a simple file uploading application with PHP that has the capacity to display relevant data about the file uploaded. Not too difficult to grasp, right? Well, at this point, I showed you how to create a basic file uploading system by using a couple of simple source files. So the next thing that I'm going to teach you will be how to incorporate an effective error checking module into the system in question, which will extend its existing functionality. Want to see how this will be done? Great! Click on the link below and keep reading.
blog comments powered by Disqus |
|
|
|
|
|
|
|