12.3 Managing File Uploads


File uploads can be done with every RFC-1867 compliant browser, such as Mozilla and Netscape. File uploading means that you can store a file on a Web server where it can be used for further treatment. It does not matter if the file is in binary or ASCII format because the file is processed as a unit byte by byte. Usually files are uploaded with the help of a POST request, as shown in the next listing:

 <html> <head> <title>Title</title> </head> <body> <h1>Working With File Uploads</h1> <form  method="post" action="input_file.php"         enctype="multipart/form-data"> <p> Choose a file: <br> <input name="testfile" type="file" size="50" maxlength="100000"><br> <input name="submit" type="submit"> </p> </form> </body> </html> 

After things like the header and title, a form starts. The first field in the form uses file as type. This field will be used for the file upload. As you can see, many parameters can be defined. One of these parameters is maxlength, which defines the maximum size of the file being uploaded. The next field contains the Submit Query button for starting the POST request. After the Submit Query button, the form and the HTML file are terminated. To see what the HTML document looks like in a browser, take a look at Figure 12.2.

Figure 12.2. File uploads.

graphics/12fig02.jpg

What happens if somebody selects a file and wants to process the file? The file responsible for managing the file uploaded by the user is called input_file.php. Take a look at the code of this file:

 <?php         if      ($testfile)         {                 if      (is_uploaded_file($testfile))                 {                         echo "userfile: $testfile<br>\n";                         echo "userfile_name: $testfile_name<br>\n";                         echo "userfile_size: $testfile_size<br>\n";                 }                 else                 {                         echo "no file uploaded";                 }         }         else         {                 echo "a problem has occurred" ;         } ?> 

The field in the HTML file responsible for performing the upload is called testfile. PHP automatically defines a variable having the same name as the field in the HTML file. This variable can be checked and if it is defined, you can try to find out if a file has been uploaded correctly. If so, some variables are displayed, which you can use to retrieve information about the file that has been processed. $testfile contains the name of the file on the Web server. $testfile_name contains the original name of the file on the user's machine. When uploading, the file won't have the original name on the Web server because this could cause problems with identical names. In addition to $testfile and $testfile_name, $testfile_size has been defined, which contains the size of the file in bytes.

If you select a file and click on the Submit Query button, the result might look like this:

 userfile: /tmp/phpTdZESc userfile_name: example.tar.gz userfile_size: 7194 

As you can see, the content of $testfile is rather cryptic, but the advantage is that the filenames generated by PHP are always unique.



PHP and PostgreSQL. Advanced Web Programming2002
PHP and PostgreSQL. Advanced Web Programming2002
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 201

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net