Return to Snippet

Revision: 15474
at July 7, 2009 05:25 by welly


Initial Code
KEEP FOR LATER USE!
$error = $this->handleFileUpload($importFile, $importFile['name']);

function handleFileUpload($fileData, $fileName) 
{ 
	$error = false; 
	//Get file type 
	$typeArr = explode('/', $fileData['type']); 

	print_r($typeArr);
	//If size is provided for validation check with that size. Else compare the size with INI file 
	if (($this->validateFile['size'] && $fileData['size'] > $this->validateFile['size']) || $fileData['error'] == UPLOAD_ERR_INI_SIZE) 
	{ 
		$error = 'File is too large to upload'; 
	} 
	elseif ($this->validateFile['type'] && (strpos($this->validateFile['type'], strtolower($typeArr[1])) === false))
	{ 
		//File type is not the one we are going to accept. Error!! 
		$error = 'Invalid file type'; 
	} 
		else 
	{ 
		//Data looks OK at this stage. Let's proceed. 
		if ($fileData['error'] == UPLOAD_ERR_OK) 
		{ 
			//Oops!! File size is zero. Error! 
			if ($fileData['size'] == 0) 
			{ 
				$error = 'Zero size file found.'; 
			} 
			else 
			{ 
				if (is_uploaded_file($fileData['tmp_name'])) 
				{ 
					//Finally we can upload file now. Let's do it and return without errors if success in moving. 
					if (!move_uploaded_file($fileData['tmp_name'], WWW_ROOT.'/files/'.$fileName)) 
					{ 
		 				$error = true; 
					} 
				} 
				else 
				{ 
					$error = true; 
				} 
			} 
		} 
	} 
	return $error; 
}

Initial URL


Initial Description


Initial Title
handleUpload function to deal with file uploads in CakePHP

Initial Tags
php, textmate, cakephp

Initial Language
PHP