Location: PHPKode > projects > Katyshop > katyshop_ro_0.3.2.1/classes/common/UploadFile.php
<?php

/**
 * Product: Katyshop
 * @version 0.3.2.1
 * @author Catalin Hulea - hide@address.com
 * @copyright Copyright (C) 2007 Catalin Hulea
 * @license GNU General Public License version 3
 * 			You can find a copy of GNU GPL v3 at this path: /docs/LICENSE
 * @link https://sourceforge.net/projects/katyshop
 * 
 * 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.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

require_once(dirname(__FILE__) . "/BaseObject.php");

/**
 * not a very smart class, but helps you write less code...
 * bleah...
 *
 */
class UploadFile extends BaseObject
{
	var $inputName = "";
	var $tmpPrefix = "tmp";
	var $prefix = "";
	var $tmpName = "";
	var $newFilename = "";
	var $TEMP_DIR = "";
	var $FINAL_DIR = "";

	var $errors = array();
	var $messages = array();

	function UploadFile($htmlFormInputName, $argFinalDir, $argTempDir = "")
	{
		parent::BaseObject();
		$this->inputName = $htmlFormInputName;
		$this->FINAL_DIR = $argFinalDir;
		$this->TEMP_DIR = $argTempDir;
		$this->setTmpPrefix("");
		$this->setPrefix("");
	}

	function isEmpty()
	{
		return (empty($_FILES) || empty($_FILES[$this->inputName]));
	}

	function setTmpPrefix($newTmpPrefix)
	{
		$this->tmpPrefix = $newTmpPrefix;
		$this->tmpName = $this->tmpPrefix . @$_FILES[$this->inputName]["name"];
	}

	function setPrefix($argPrefix)
	{
		$this->prefix = $argPrefix;
		$this->newFilename = $this->prefix . @$_FILES[$this->inputName]["name"];
	}

	function validateInput()
	{
		if (empty($_FILES[$this->inputName]["name"]))
			$this->errors[] = "There was an error while trying to upload the file";
		if(strlen($this->tmpName) < 3 || strlen($this->tmpName) > 255)
			$this->errors[] = "Temporary filename must have between 3 and 255 characters";
		if(strlen($this->newFilename) < 3 || strlen($this->newFilename) > 255)
			$this->errors[] = "New filename must have between 3 and 255 characters";
		if(!is_dir($this->FINAL_DIR))
			$this->errors[] = "Could not find FINAL_DIR as a directory to use it";
		if(!empty($this->TEMP_DIR) && !is_dir($this->TEMP_DIR))
			$this->errors[] = "Could not find TEMP_DIR as a directory to use it";
	}

	function validateImage()
	{
		if (!Tools::isImage(@$_FILES[$this->inputName]["name"]))
			$this->errors[] = "The file you uploaded is not an image, please try again";
	}

	function upload($toFinalDir = true)
	{
		if($this->hasErrrors())
			return false;

		if($toFinalDir)
		{
			$filename = $this->FINAL_DIR . "/" . $this->tmpName;
		}
		else
		{
			if(empty($this->TEMP_DIR))
			{
				$this->errors[] = "Cannot use temporary upload method because TEMP_DIR is not set";
				return false;
			}
			$filename = $this->TEMP_DIR . "/" . $this->tmpName;
		}

		if(move_uploaded_file($_FILES[$this->inputName]["tmp_name"], $filename))
		{
			return true;
		}
		else
		{
			$this->errors[] = "There was an error while trying to upload the file";
			return false;
		}
	}

	function resize_limitwh($w = 100, $h = 100)
	{
		if($this->hasErrrors())
			return false;

		$im = new RESIZEIMAGE();
		$im->setImage($this->TEMP_DIR . "/" . $this->tmpName);
		$im->resize_limitwh($w, $h, $this->TEMP_DIR . "/" . $this->newFilename);

		if(is_file($this->TEMP_DIR . "/" . $this->newFilename))
		{
			return true;
		}
		else
		{
			$this->errors[] = "Could not process the image you uploaded";
			return false;
		}
	}

	/**
	 * If you uploaded the image to a temporary directory
	 * and processed it there, call commit() in the end to clean up temporary directory
	 * and move the file to the final directory
	 *
	 * If you set a filename for $deleteExistingFile, this function will try to delete
	 * that file from $this->FINAL_DIR, before moving uploaded file
	 */
	function commit($deleteExistingFile = "")
	{
		if(empty($this->TEMP_DIR))
		{
			$this->errors[] = "Cannot use commit() because TEMP_DIR is not set";
			return false;
		}

		if(is_file($this->TEMP_DIR . "/" . $this->newFilename))
		{
			if(!empty($deleteExistingFile))
				@unlink($this->FINAL_DIR . "/" . $deleteExistingFile);
			@rename($this->TEMP_DIR . "/" . $this->newFilename, $this->FINAL_DIR . "/" . $this->newFilename);
		}

		// delete from temporary directory
		if(is_file($this->TEMP_DIR . "/" . $this->newFilename))
			@unlink($this->TEMP_DIR . "/" . $this->newFilename);
		if(is_file($this->TEMP_DIR . "/" . $this->tmpName))
			@unlink($this->TEMP_DIR . "/" . $this->tmpName);

		if($this->hasErrrors())
			return false;
		else
			return true;
	}

	function hasErrrors()
	{
		return (count($this->errors) > 0);
	}

	function clearErrors()
	{
		$this->errors = array();
	}

	function clearMessages()
	{
		$this->messages = array();
	}

}

?>
Return current item: Katyshop