Home » PEAR, Smarty21 July 2008

File Upload in PEAR and Smarty

For uploading files, you need to install a package of PEAR called ‘HTTP_Upload’ along with the installation of PEAR and Smarty.

HTTP_Upload is used for easy and secure managment of files submitted via HTML forms. This package provides an advanced system for managing uploads of files via HTML input type “file” fields.

Features include:

1) Handling of multiple file uploads at a time

2) Safe file copying/moving from temporary directories

3) Upload validaton mechanisms

4) Extensive information about uploaded files

5) Renaming uploaded files

6) Internationalized error messages

Two class files are included in index.php. One is ConnectSmarty.class.php which is used for connecting with Smarty. I have explained about it in my previous article. The other is files.class.php.

files.class.php

This class file includes the upload class which becomes available after installing the HTTP_Upload package. I have created a function named ‘upload’ inside the class ‘Files’. The upload() function requires two parameters: $field_name and $dest.

$field_name is the name of input file. $dest is the name of the destination directory where the file is to be uploaded.

The function getFiles() checks if a valid file was uploaded through the form. If the file is valid then it is moved to the destination directory.

If there is error while moving the file to the destination directory then the error message is fetched through the getMessage() function and displayed in a separate template file called error.tpl.

index.php

I have only allowed jpeg and gif image to be uploaded. If the file is successfully uploaded then a success message is assigned and displayed.

If the file is not of jpeg or gif type then an error message is assigned and displayed.

index.tpl

error.tpl

Download source code

Smarty

Get New Post by Email

Find me on

FacebookTwitterGoogle+LinkedInRSS Feed
  • Ghifari

    Excuse me, I wanted to ask, how to make rotate image in PHP or HTML?
    please reply :(

  • Gaurav Sharma

    Hi,

    From where we can get the HTTP/Upload.php file, As we are using your mentioned source code it shows the following errors:

    Warning: require_once(HTTP/Upload.php) [function.require-once]: failed to open stream: No such file or directory in C:xampphtdocsSmartyFileUploadclassesfiles.class.php on line 2Fatal error: require_once() [function.require]: Failed opening required ‘HTTP/Upload.php’ (include_path=’.;C:xamppphpPEAR’) in C:xampphtdocsSmartyFileUploadclassesfiles.class.php on line 2

    Can you let us know, where we are wrong?

    Thanks