In consonance with the concepts deployed in the prior section, below I listed the two source files that make up this PHP-driven file uploading application, including the pertinent web form and the script that I modified earlier. That being said, here’s how these source files look: (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!'; } } There you have it. With the two source files listed above at your disposal, you shouldn’t have major problems developing your own PHP file upload application, even though its current state is quite primitive. However, if you’re learning the basic concepts of how to implement a simple file uploading mechanism with PHP, the entirety of the code samples included in this tutorial should be good enough to fit your needs for now. Final thoughts In this second chapter of the series, you learned how to use the $_FILES superglobal array included with PHP in order to create a simple file uploading application that is also capable of displaying relevant information about an uploaded file. As you saw earlier, utilizing this useful array isn’t rocket science. So in consequence, you shouldn’t have major problems understanding how to use it when building your own file upload applications. In the next tutorial of the series, things will become more interesting because I’m going to teach you how to incorporate an effective error handling module to the previous PHP script, which will come in handy when tracking the eventual failures that might occur during a particular file uploading process. Now that you’re aware of the topics that will be discussed in the upcoming article, you won’t want to miss it!
blog comments powered by Disqus |
|
|
|
|
|
|
|