PHP
  Home arrow PHP arrow Page 4 - Retrieving Information on Selected Files with a PHP 5 File Uploader
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Retrieving Information on Selected Files with a PHP 5 File Uploader
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 5
    2008-03-26


    Table of Contents:
  • Retrieving Information on Selected Files with a PHP 5 File Uploader
  • Reintroducing a previous hands-on example
  • Retrieving useful information on the uploaded file
  • Listing the complete source code of the improved file uploading application

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Retrieving Information on Selected Files with a PHP 5 File Uploader - Listing the complete source code of the improved file uploading application
    ( Page 4 of 4 )

    In consonance with the concepts deployed in the prior section, below I listed the two source files that make up this PHP-driven file uploading application, including the pertinent web form and the script that I modified earlier.

    That being said, here’s how these source files look:


    (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="2000000" />

    <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!<br />';

    echo 'Name of uploaded file: '.$_FILES['userfile']['name'].'.<br />';

    echo 'MIME type of uploaded file: '.$_FILES['userfile']['type'].'.<br />';

    echo 'Size of uploaded file: '.$_FILES['userfile']['size'].' bytes.<br />';

    echo 'Temporary name of uploaded file: '.$_FILES['userfile']['tmp_name'].'<br />';

    }

    else{

    echo 'Error uploading target file!';

    }

    }


    There you have it. With the two source files listed above at your disposal, you shouldn’t have major problems developing your own PHP file upload application, even though its current state is quite primitive.

    However, if you’re learning the basic concepts of how to implement a simple file uploading mechanism with PHP, the entirety of the code samples included in this tutorial should be good enough to fit your needs for now.

    Final thoughts

    In this second chapter of the series, you learned how to use the $_FILES superglobal array included with PHP in order to create a simple file uploading application that is also capable of displaying relevant information about an uploaded file.

    As you saw earlier, utilizing this useful array isn’t rocket science. So in consequence, you shouldn’t have major problems understanding how to use it when building your own file upload applications.

    In the next tutorial of the series, things will become more interesting because I’m going to teach you how to incorporate an effective error handling module to the previous PHP script, which will come in handy when tracking the eventual failures that might occur during a particular file uploading process.

    Now that you’re aware of the topics that will be discussed in the upcoming article, you won’t want to miss it!



     
     
    >>> More PHP Articles          >>> More By Alejandro Gervasio
     

       

    PHP ARTICLES

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    Stay green...Green IT