Uploading Files


It is common to upload files using PHP. After the file has been uploaded, its data is accessible to your PHP code.

For example, say that you wanted to upload a file named file.txt, which has these contents:

 Now is the time.

In order to make this work, you need a multi-part <form> element in the Web page that uploads this file, file.html. You create a multi-part form by setting the form’s enctype attribute to multi-part/form-data:

 <html>     <head>         <title>             Uploading Files         </title>     </head>     <body>         <center>             <h1>                Uploading Files             </h1>             <form                 enctype="multipart/form-data"                 .                 .                 .             </form>         </center>     </body> </html>

then you set the action to the PHP application that will handle the uploaded file, and set the method to POST:

 <html>     <head>         <title>             Uploading Files         </title>     </head>     <body>         <center>             <h1>                Uploading Files             </h1>             <form                 enctype="multipart/form-data"                 action="file.php" method="POST">                 .                 .                 .             </form>         </center>     </body> </html>

Finally, you can add an HTML file-upload control, an <input type="file"> element, like this (note that I named this control userfile in this example):

 <html>     <head>         <title>             Uploading Files         </title>     </head>     <body>         <center>             <h1>                 Uploading Files             </h1>             <form                 enctype="multipart/form-data"                 action="file.php" method="POST">                 Upload this file: <input name="userfile"                   type="file" />                 <br>                 <br>                 <input type="submit" value="Upload File" />             </form>         </center>     </body> </html>

Now you have to read the file’s contents in PHP. You use the PHP array $_FILE S to handle uploaded files. Following are the specific elements you can use and what they mean (note that the first index is the name of the file upload control, which is userfile here):

  • $_FILES['userfile']['name']: The name of the file on the user’s machine.

  • $_FILES['userfile']['type']: The MIME type of the file. For example, this could be image/gif or text/plain.

  • $_FILES['userfile']['size']: The size of the uploaded file, in bytes.

  • $_FILES['userfile']['tmp_name']: The temporary file name of the file in which the uploaded file was stored on the server.

  • $_FILES['userfile']['error']: The error code associated with this file upload.

To read the text from the file in file.php, start this way:

 <html>     <head>         <title>Reading uploaded files</title>     </head>     <body>         <center>             <h1>Reading uploaded files</h1>             <br>             Here is what's in the file:             <br>             <br>             <b>             .             .             .             </b>         </center>     </body> </html>

When you upload a file, it’s stored as a temporary file that you can access as $_FILES['user-file']['tmp_name']. Here’s how you recover and display the contents of that file: you start by getting a file handle, which stands for the file, using the fopen function (file open-the r argument here means you want PHP to open the file for reading):

 <html>     <head>         <title>Reading uploaded files</title>     </head>     <body>         <center>             <h1>Reading uploaded files</h1>             <br>             Here is what's in the file:             <br>             <br>             <b>               <?                 $handle = fopen($_FILES['userfile']['tmp_name'],                   "r");                 .                 .                 .             ?>             </b>         </center>     </body> </html>

Now that you’ve got a file handle for the temporary file, you can loop over the lines in that file. You do that with a while loop, which keeps executing until you reach the end of the file-something you can test with the PHP function feof, which returns true when you’re at the end of the file:

 <html>     <head>         <title>Reading uploaded files</title>     </head>     <body>         <center>             <h1>Reading uploaded files</h1>             <br>             Here is what's in the file:             <br>             <br>             <b>             <?                 $handle = fopen($_FILES['userfile']['tmp_name'],                   "r");                 while (!feof($handle)){                 .                 .                 .                 }             ?>             </b>         </center>     </body> </html>

You can read the current line of text with the PHP fgets (file get string) function, and echo that line to the browser like this:

 <html>     <head>         <title>Reading uploaded files</title>     </head>     <body>         <center>             <h1>Reading uploaded files</h1>             <br>             Here is what's in the file:             <br>             <br>             <b>             <?                 $handle = fopen($_FILES['userfile']['tmp_name'],                   "r");                 while (!feof($handle)){                     $text = fgets($handle);                     echo $text, "<BR>";                 }             ?>             </b>         </center>     </body> </html>

When you’re done, you can close the file with the fclose function this way:

 <html>     <head>         <title>Reading uploaded files</title>     </head>     <body>         <center>             <h1>Reading uploaded files</h1>             <br>             Here is what's in the file:             <br>             <br>             <b>             <?                 $handle = fopen($_FILES['userfile']['tmp_name'],                   "r");                 while (!feof($handle)){                     $text = fgets($handle);                     echo $text, "<BR>";                 }                   fclose($handle);             ?>             </b>         </center>     </body> </html>

That’s all you need. The file.html Web page is shown in Figure 13.18, where you can use the Browse button to locate the file you want to upload.

image from book
Figure 13.18: The file.html Web page

When the user clicks the Upload File button, the file uploads and its contents appear, as shown in Figure 13.19.

image from book
Figure 13.19: The file.php application at work



Ajax Bible
Ajax Bible
ISBN: 0470102632
EAN: 2147483647
Year: 2004
Pages: 169

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