An excellent way to demonstrate how to improve the logic of the file uploading script developed in the preceding article of the series is to list the respective definitions of its two source files, so you can recall how they were created on that specific occasion. That being said, here’s is how the mentioned source files looked, so study them in detail: (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="20000" /> <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!'; } else{ echo 'Error uploading target file!'; } } As I explained before, the above file uploading application is comprised of two basic source files. The first one is responsible for creating a basic file uploading web form and the last one takes up the file selected by the user and implements the logic necessary for moving it from a temporary directory in the web server to its final location. In this case, the upload directory is located at C:uploaded_files, and it’s only valid for Windows users, but this option can be changed to work with a different operating system. So far, so good. At this point, I will assume that you’re familiar with the source code that I used to perform file uploads with PHP. Thus, it’s time to see how to introduce some modifications to the previous script. Given that the $_FILES superglobal array provides complete information on a file uploading operation, I’m going to use some of its additional elements to display accurate data about this process, including the size and MIME type, the size of the file being uploaded, and so forth. To see how these minor improvements will be incorporated into the prior file uploading application, jump into the next section and keep reading.
blog comments powered by Disqus |
|
|
|
|
|
|
|