PHP Upload Class

Sun, 3 Oct 2010

Yet another simple class for the Open Source lab area. This one is a PHP Upload Class. As always, see the @usage documentation for a quick example. There will be newer versions time to time of these files but I won't make a blog announcement of it every time :P

To download visit the Open Source Area.

<?php
/**
 * Upload 0.1
 *
 * @author		JREAM
 * @link		http://www.jream.com
 * @copyright	2010 Jesse Boyer (contact@jream.com)
 * @license		GNU General Public License 3 (http://www.gnu.org/licenses/)
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details:
 * http://www.gnu.org/licenses/
 *
 * @uses
 *
	You need your html form
	<form enctype="multipart/form-data" method="post" action="url_here.php">
	<input type="file" name="fieldname" />

	The PHP usage:

 		$Upload = new Upload('fieldname');
		if ($Upload->Result == true)
		{
			echo 'Tell them it was good!';
		}

	A specific directory:
		$Upload = new Upload('fieldname', 'myfolder/');


 */

class Upload
{

	/**
	 * @var  Retrieve the result, default is set to false.
	 */
	public $Result = 0;
	
	/**
	 * @var  The name of the file used for outside operations (optional)
	 */
	public $Name;

	/**
	 *
	 * @var  Name of the HTML form field
	 */
	private $_Field;

	/**
	 * @var  The directory to place the file in
	 */
	private $_Dir;
	
	/**
	 * @desc This WILL overwrite files that exist with the same name.
	 * 
	 * @param  $Field The name of the HTML input field for the file
	 * @param  $dir the directory to place the file
	 */
	public function __construct($Field, $Dir = NULL)
	{

		/** Set the name of the field for use with the other methods */
		$this->_Field = $Field;

		/** Append a slash if there is not one on the incoming Directory */
		if (isset($Dir))
		{
			if (substr($Dir, -1) != '/')
			$Dir .= '/';
		}
		$this->_Dir = $Dir;

		/** The actual file name used for placement and retrieving outside the object */
		$this->Name = $_FILES[$this->_Field]['name'];		


		/** Process the File */
		if ($this->_File_Errors() == false)
		$this->_Move();

		/** If there are errors we can them */
		else
		echo $this->_File_Errors();
	
	}

	/**
	 * @desc Checks the incoming file for errors and whether it exists or not.
	 * 
	 * @return  Return the warning, otherwise return false.
	 */
	private function _File_Errors()
	{
		switch ($_FILES[$this->_Field]['error'])
		{

			case 1:
				return 'UPLOAD_ERR_INI_SIZE; The uploaded file exceeds the upload_max_filesize directive in php.ini.';
				break;

			case 2:
				return 'UPLOAD_ERR_FORM_SIZE; The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.';
				break;

			case 3:
				return 'UPLOAD_ERR_PARTIAL; The uploaded file was only partially uploaded.';
				break;

			case 4:
				return 'UPLOAD_ERR_NO_FILE; No file was uploaded.';
				break;

			case 6:
				return 'UPLOAD_ERR_NO_TMP_DIR; Missing a temporary folder.';
				break;

			case 7:
				return 'UPLOAD_ERR_CANT_WRITE; Failed to write file to disk.';
				break;

			case 8:
				return 'UPLOAD_ERR_EXTENSION; A PHP extension stopped the file upload. PHP does not provide a way to ascertain which extension caused the file upload to stop; examining the list of loaded extensions with phpinfo() may help.';
				break;

			default:
				return false;
				break;
		}

	}

	/**
	 * @desc Attempts to move the file into the user specific directory.
	 *		 If anything goes wrong PHP will issue a warning.
	 */
	private function _Move()
	{
		 if (move_uploaded_file($_FILES[$this->_Field]["tmp_name"], $this->_Dir . $this->Name) == true)
		 $this->Result = true;
	}


}