Miscellaneous

  Home arrow Miscellaneous arrow Page 2 - Uploading Files with Forms and PHP
MISCELLANEOUS

Uploading Files with Forms and PHP
By: bluephoenix
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 5
    2003-09-28

    Table of Contents:
  • Uploading Files with Forms and PHP
  • PHP Code
  • Finishing it up

  •  
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement

    Uploading Files with Forms and PHP - PHP Code


    (Page 2 of 3 )

    The file is available by referencing the superglobal $_FILES array. The first index is the form's input name and the second index can be either "name", "type", "size", "tmp_name" or "error".

  • $_FILES["file"]["name"] holds the name of the file uploaded from the client
  • $_FILES["file"]["type"] holds the mimetype of the uploaded file
  • $_FILES["file"]["size"] holds the size in bytes of the uploaded file
  • $_FILES["file"]["tmp_name"] holds the name of the temporary copy of the file stored on the server
  • $_FILES["file"]["error"] holds any error code resulting from the file transfer

    For a listing of possible error codes, see www.php.net/manual/en/features.file-upload.errors.php.

    Example code using $_FILES might look like this:

    <?php
    echo "Return Code: " $_FILES["file"]["error"] . "&lt;br /&gt;";
    echo 
    "Uploading " $_FILES["file"]["name"];
    echo 
    " (" $_FILES["file"]["type"] . ", ";
    echo 
    ceil($_FILES["file"]["size"] / 1024) . " Kb).&lt;br /&gt;";
    echo 
    "File is temporarily stored as " $_FILES["file"]["tmp_name"];
    ?>

    However, you will want to keep in mind some of the potential hazards of allowing files to be uploaded to your server... be sure to use type, size and error in determining how to process the file.

    <?php
    if (($_FILES["file"]["type"] == "image/gif") &amp;&amp;
    (
    $_FILES["file"]["size"] &lt15000)) {
      echo 
    "Return Code: " $_FILES["file"]["error"] . "&lt;br /&gt;";
      echo 
    "Uploading " $_FILES["file"]["name"];
      echo 
    " (" $_FILES["file"]["type"] . ", ";
      echo 
    ceil($_FILES["file"]["size"] / 1024) . " Kb).&lt;br /&gt;";
      echo 
    "File is temporarily stored as "$_FILES["file"]["tmp_name"];

    } else
      echo 
    "Sorry, we only accept .GIF images under 15Kb for upload.";
    ?>

    Since the temporary file will be deleted once the script is done processing and the HTTP connection closes, our next task is to copy the temporary file to a safe location. For this we have the function move_uploaded_file.

    More Miscellaneous Articles
    More By bluephoenix

    blog comments powered by Disqus
  • MISCELLANEOUS ARTICLES

    - Oracle Database XE: Indexes and Sequences
    - Modifying Tables in Oracle Database XE
    - Oracle Database XE: Tables and Constraints
    - More on Oracle Databases and Datatypes
    - Oracle Database XE Datatypes: Datetime and L...
    - Oracle Database XE Datatypes: Character and ...
    - From Databases to Datatypes
    - Firefox 3.6.6 Released with Improved Plug-in...
    - Attention Bloggers: WordPress 3.0 Now Releas...
    - Reflection in PHP 5
    - Inheritance and Other Advanced OOP Features
    - Advanced OOP Features
    - Linux from Scratch V.6.6 Review
    - Linux Gaining in Strength
    - Install Slackware on Your Old PC


    © 2003-2012 by Developer Shed. All rights reserved. DS Cluster 3 - Follow our Sitemap