As I said in the previous section, the $_FILES array stores a predefined set of error codes when a file upload has been performed. This facilitates the development of an error checking mechanism for keeping track of all the possible failures that might occur during a specific file upload. However, this will be better understood through an example. Please take a look at the signature of the following file uploading script, which implements a rudimentary error checking module capable of triggering different exceptions in accordance with the type of error caused when a file has been uploaded to the web server. The corresponding code sample is as follows: // example on checking file uploading errors with the $_FILES array try{ 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 />Name of uploaded file: '.$_FILES } else{ // display error messages when file upload fails switch ($_FILES['userfile']['error']){ case 1: throw new Exception('Target file exceeds maximum allowed size.'); break; case 2: throw new Exception('Target file exceeds the MAX_FILE_SIZE value specified on the upload form.'); break; case 3: throw new Exception('Target file was not uploaded completely.'); break; case 4: throw new Exception('No target file was uploaded.'); break; case 6: throw new Exception('Missing a temporary folder.'); break; case 7: throw new Exception('Failed to write target file to disk.'); break; case 8: throw new Exception('File upload stopped by extension.'); break; } } } } catch(Exception $e){ echo $e->getMessage(); exit(); } Things are becoming quite interesting at this point! As you can see, the above file uploading script has now been provided with the ability to trigger different exceptions according to the value stored in the $_FILES['userfile]['error'] array element. Obviously, the error codes stored in the array element aren't simply an invention of mine; they're automatically generated by the PHP engine to keep track of different failures that might arise when a file uploading process is being performed via an HTTP POST request. Based on this handy error handling mechanism, it's possible to determine what went wrong during a specific file upload. For instance, the size of the selected file might exceed an allowed limit, or may have an invalid extension, etc. In this case, each possible failure can be easily handled via the $_FILES PHP array, as demonstrated by the previous PHP script. All right, at this level, you've hopefully learned how to incorporate a basic error handling module into the file uploading application developed previously, which helps track different failures that might occur when a specific file is being uploaded to the web server. Nonetheless, the previous file uploading script looks rather disjointed if it's not accompanied by the corresponding web form that allows users to browse the files in their client machines. Thus, in the final section of this tutorial, I'll be listing the complete source code of this recently-improved file uploading application, including the web form in question. So, take a deep breath and read the following lines.
blog comments powered by Disqus |
|
|
|
|
|
|
|