All pastes #902312 Raw Edit

Andy

public php v1 · immutable
·published 2008-02-13 11:08 UTC
rendered paste body
<?PHP	function UploadImage($imagenumber,$prefix=''){		global $database;			if(isset($_FILES[$prefix . "uploadimage" .$imagenumber])){			if(isset($_POST[$prefix . "subdir" .$imagenumber]) && !empty($_POST[$prefix . "subdir" . $imagenumber])){				$subdir = $_POST[$prefix . "subdir" . $imagenumber];			}			if(is_uploaded_file($_FILES[$prefix . "uploadimage" . $imagenumber]['tmp_name'])) {		$uploaddir = IMAGE_UPLOAD_DIR;		$lastchar = substr($subdir,-1,1);		if($lastchar!=='/'){			$subdir .= '/';		}		if($subdir=='/'){			$subdir = '';		}		if(MakeDirectory($uploaddir . $subdir)){			$uploadfile = basename($_FILES[$prefix . "uploadimage" . $imagenumber]['name']);			if (move_uploaded_file($_FILES[$prefix . "uploadimage" . $imagenumber]['tmp_name'], $uploaddir . $subdir . $uploadfile)) {				$badchars = array(' ','(',')','[',']','{','}','!','`','+','=');				$goodchars = array('_','','','','','','','','','','');				$cleanname = str_replace($badchars,$goodchars,$uploadfile);				if($cleanname!==$uploadfile){					if(!rename($uploaddir . $subdir . $uploadfile,$uploaddir . $subdir . $cleanname)){						//$this->errors[] = "Error renaming file from $uploadfile to $cleanname";						return;					}				}					//add to database;				$q = "INSERT INTO `images` (`path`) VALUES ('$subdir$cleanname');";				$q2 = "SELECT `id` FROM `images` WHERE `path`='$subdir$cleanname'";				$database->query($q);				$result = $database->query($q2);				if($result){					$newid = $database->dbResult($result,0,'id');					$database->dbFreeResult($result);				}				else{					$newid = -2; //error adding to db				}			}			else{				$newid = -1; //error moving file			}		}		else{			$newid = -3; //error creating subdirectory		}	}	else{		//$this->errors[] = "Not a valid file uploaded";	}}else{	$newid=0; //no file uploaded}return($newid);}?>