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


<?php
require_once "HTTP/Upload.php";
class Files
{
	function upload($field_name,$dest)
	{
		$upload =& new HTTP_Upload("en");
		$file = $upload->getFiles($field_name);</p>
		if ($file->isValid())
		{
			$moved = $file->moveTo("$dest/");
			if (PEAR::isError($moved))
			{
				require_once('ConnectSmarty.class.php');
				// create an object of the class included above
				$smarty = new ConnectSmarty;</p>
				$error =& $moved->getMessage();</p>
				// assign error message
				$smarty->assign('errmessage',$error);

				// display error message and exit
				$smarty->display('error.tpl');
				exit();
			}
		}
	}
}
?>

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


<?php
require_once('classes/ConnectSmarty.class.php');
require_once ('classes/files.class.php');

// create an object of the class included above
$smarty = new ConnectSmarty;

if(isset($_POST['submit']))
{
	$post = $_POST;
	$keys = "";</p>
	$type = $_FILES['logo']['type'];
	// Only allowing jpeg and gif image to be uploaded
	if($type == "image/jpeg" || $type == "image/gif")
	{
	/**
	* file upload part
	* creating an object of the class Files
	* calling the upload function of the class Files
	* the destination directory is to be created at first
	* in this case, the destination directory is created and named 'uploads'
	*/
	$file =& new Files;
	$fieldName = "logo";
	$dest = "uploads";
	$file->upload($fieldName,$dest);

	// assign success message
	$smarty->assign('success','File Uploaded Successfully');
	}
	else
	{
		// assign error message
		$smarty->assign('error','Only jpge and gif image are supported.');

		// display error message and exit
		$smarty->display('index.tpl');
		exit();
	}
}

// display the content
$smarty->display('index.tpl');
?>

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.


// assign success message
$smarty->assign('success','File Uploaded Successfully');

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


// assign error message
$smarty->assign('error','Only jpge and gif image are supported.');

// display error message and exit
$smarty->display('index.tpl');

exit();

index.tpl


<html>

<head>
<title> Homepage : File Upload</title>
</head>

<body>

<font color="green">{$success}</font>
<font color="red">{$error}</font>

<h2>Upload Logo</h2>
<table>
<form action="index.php" method="post" enctype="multipart/form-data">
<tr><td>Logo</td><td><input type="file" name="logo" ></td></tr>
<tr><td></td><td><input type="submit" name="submit" value="Submit" /></td></tr>
</form>
</table>

</body>

</html>

error.tpl


<div align="center">
<font color="red">
{$errmessage}
</font>
</div>

Download source code