Location: PHPKode > scripts > PHP Simple Upload > php-simple-upload/CLS_UPLOAD.php
<?php
define (POSTVAR_ERR, 'POST VARIABLE NOT SET');
define (UPLOAD_ERR, 'No file or image uploaded');
define (FILEMAX_SIZE, MAX_FILE_SIZE);
define (FILESIZE_ERR, 'File size is limited to '.(FILEMAX_SIZE/1000).' kb');
define (COPY_ERR, 'Copy Error');
define (WRITE_ERR, 'Error writing to file');
define (READ_ERR, 'Error reading file');
define (INVALID_EXT, 'Invalid File Extension');
define (INVALID_FILE_TYPE, "Invalid File Type");
define (INVALID_EXT_DEFINE, 'Extensions should be in array');
define (UPLOAD_SUCC, "Your file has been uploaded");
define (UPLOAD_FAILED, "There was an error uploading the file.");
define (NO_FILE_ERR, "No file uploaded.");

class CLS_UPLOAD {
	var $maxsize = FILEMAX_SIZE;
	var $ext = array();

	var $name = '';
	var $savepath = '';
	var $errmsg = '';
	var $trynewdir = '';
	var $newfilename = '';
	var $newpath = '';
	var $extension = '';
	var $fileType='';
	var $origfileN = '';
	var $error = '';
	var $size = '';
	var $tmpname = '';
	var $fileExtensions = array("application/msword", "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
	var $imgExtensions = array("image/gif","image/pjpeg", "image/jpg","image/jpeg", "image/png");

	
	function CLS_UPLOAD($postname, $uploaddir, $newfilename='', $validext){
		$this->name = $postname;
		$this->acceptExt($validext);
		$this->savepath = $savepath;
		$this->trynewdir = $uploaddir;
		$this->fileType = $_FILES["$postname"]['type'];
		$this->origfileN = strtolower($_FILES["$postname"]['name']);
		$this->newfilename = ($newfilename!='')?str_replace(" ",'',$newfilename):str_replace(" ", '', $this->origfileN);
		$this->error = $_FILES["$postname"]['error'];
		$this->size = $_FILES["$postname"]['size'];
		$this->tmpname = $_FILES["$postname"]['tmp_name'];
		return;
	}
	
	function setFileNewFileN($strnewfn){
	
		$this->newfilename = $strnewfn;
	
	}
	
	
	
	function acceptExt($extarr){
		if(is_array($extarr)) $this->ext = $extarr; 
		else $this->ext = ($validext=='image')?$this->imgExtensions:$this->fileExtensions;
		
		
	}

	function setUpload(){
		if (!isset($_FILES[$this->name])) { $this->errmsg = POSTVAR_ERR;  return false; }
		else { 
			if($this->checkUploaded()) return true; 
			else return false;
		}
		
	}
	
	function checkUploaded(){
		if($this->error==2 || $this->error==4){
		
			$this->errmsg = FILESIZE_ERR;
			return false;
		}
	
			if ($this->size>$this->maxsize) {
				$this->errmsg = FILESIZE_ERR; 
				
				return false; 
			}
			if (!($this->isTypeMatch())){ $this->errmsg=INVALID_FILE_TYPE; return false; }
			if (!$res = copy($this->tmpname, $this->tryNewDir() )) { $this->errmsg = COPY_ERR; return false; }
			else { 
				$this->errmsg = UPLOAD_SUCC;  
				
				return true;
			}
	}
	
	function isTypeMatch(){
		$extErr = 1;
		$ftype = strtolower($this->fileType);
		
		foreach($this->ext as $key=>$possibletypes){
		
			if ($ftype!=strtolower($possibletypes)) $extErr *= 1;
			else $extErr *= 0;
			
		}
		
		if ($extErr) return false;
		return true;	
	}
	
	function isFileExtValid(){
		$extErr = 1;
		foreach($this->ext as $key=>$possibletypes){

			if ($this->fileType!=strtolower($possibletypes)) $extErr *= 1;
			else $extErr *= 0; 
		}
		
		if ($extErr) { $this->errmsg = INVALID_EXT; return false;}
		$this->errmsg = '';
		return true;	
	}
	
	function getFileExt(){
		return $this->appendExt();
	}
	
	function appendExt(){
		switch ($this->fileType) {
			case "application/msword":
				return "doc";
				break;
			case "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
				return "docx";
				break;
			case "text/plain":
				return "txt";
				break;
			case "image/gif":
				return "gif";
				break;
			case "image/pjpeg":
				return "jpg";
				break;
			case "image/jpeg";
				return "jpg";
				break;
			case "image/png":
				return "png";
				break;
			default:
				return "html";
				break;
		}
	}
	
	function tryNewDir() {
		@mkdir($this->trynewdir, 0755);
					
		if(strpos($this->newfilename, ".")!==FALSE) $f = $this->newfilename;
		else $f = $this->newfilename.".".$this->appendExt();
		
		$this->newpath = $this->trynewdir."/".$f;
		$this->newfilename = $f;
		
		return str_replace(" ",'',$this->newpath);
	}
	
	
	
	function getNewFileN(){
		return $this->newfilename;
	}
	
	function getImgPath(){
		return $this->newpath; 
	}
	
	
	function converttohtml(){
	
		$filename = $this->tryNewDir();
		
		$fh = fopen($filename, "r");
		$contentstemp = fread($fh, filesize($filename));
		$stripped = htmlspecialchars($contentstemp);
		fclose($fh);
		

		$file = basename($filename, $this->appendExt());
		$fh = fopen(dirname($filename)."/".$file."html", "w");
		$contentstemp = fwrite($fh, $stripped);
		fclose($fh);
	}
}



class getFile extends CLS_UPLOAD{
	var $text = '';
	
	function getFile($trynewdir, $trynewfilename, $htmltexttowrite){
		$this->text = $htmltexttowrite;
		$this->trynewdir = $trynewdir;
		$this->newfilename = $trynewfilename;
		return;
	}
	
	function write() {
		
		$fh = fopen($this->tryNewDir(), "w");
		$this->errmsg = UPLOAD_SUCC;
		if (!fwrite($fh, $this->text)) { $this->errmsg = WRITE_ERR; return false; }
		fclose($fh);
		return true;
	}
	
	function read() {
		$filetoread = $this->tryNewDir();
		$fh = fopen($filetoread, "r");
		$this->errmsg = "";
		if (!( $contents = fread($fh,100000 ) )) { $this->errmsg = READ_ERR; return false; }
		fclose($fh);
		return $contents;
	}
	
	function getFilePath() {
		return $this->tryNewDir();
	}
	
}

/*class UploadFile extends CLS_UPLOAD {
	function uploadtextfile(){
		if (!$this->isFileExtValid()) { $this->errmsg = INVALID_EXT; return false;}
		if (isset($_FILES[$this->name]) && ($_FILES[$this->name]['error'] == UPLOAD_ERR_OK)) {
        	$newPath = $this->tryNewDir(); //$this->trynewdir . basename($_FILES[$this->name]['name']);
        		if (move_uploaded_file($_FILES[$this->name]['tmp_name'], $this->tryNewDir() )) {
            		$this->errmsg = UPLOAD_SUCC;
					$newlog = new CLS_LOGS($_SESSION['agent'], $_SESSION['loggedid']);
				
        		} else {
            		$this->errmsg = UPLOAD_FAILED;
        		}
    	} else {
       		$this->errmsg = NO_FILE_ERR;
    	}
	}
	
	
	function converttohtml($filename){
		$fh = fopen($filename, "r");
		$contentstemp = fread($fh, filesize($filename));
		$stripped = htmlspecialchars($contentstemp);
		fclose($fh);
		
		$fh = fopen($filename, "w");
		$contentstemp = fwrite($fh, $stripped);
		fclose($fh);
	}
	
}
*/
?>
Return current item: PHP Simple Upload