PHP handleUpload函数用于处理CakePHP中的文件上传

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP handleUpload函数用于处理CakePHP中的文件上传相关的知识,希望对你有一定的参考价值。

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; 
}

以上是关于PHP handleUpload函数用于处理CakePHP中的文件上传的主要内容,如果未能解决你的问题,请参考以下文章

markdown 用于预处理字段的WordPress自定义PHP函数在Twig模板中使用之前

[编写用于处理另一个函数输出的PHP函数时使用哪种设计模式?

php文件处理

php常用字符串处理函数实例分析

PHP:“or die();”的现代版本用于错误处理

Php函数完整参考手册