PHP Single File upload principle and upload function encapsulation

Source: Internet
Author: User
Tags array exit file upload html form http post return client

 -->The server (temporary file)--> specifies the directory, and when the file enters the server it is a temporary file, in which case the temporary file name Tmp_name.
Setting limits on the client to upload files (file type and size) is unsafe because the customer can modify the limit through the source code, so set the limit here on the server.
Set the encoding to UTF-8 to avoid the Chinese garbled header (' content-type:text/html;charset=utf-8 ');
Receive uploaded information through $_files $fileInfo = $_files[' MyFile ']; function UploadFile ($fileInfo, $uploadPath = ' uploads ', $flag =true, $allowExt =array (' jpeg ', ' jpg ', ' png ', ' gif '), $ MaxSize = 2097152 {//Judge error number, only 0 or UPLOAD_ERR_OK, no error occurred, upload success if ($fileInfo [' Error ']>0) {//Note!
				Error message not 5 switch ($fileInfo [' ERROR ']) {Case 1: $mes = ' upload file exceeds the value of upload_max_filesize option in PHP configuration file ';
			Break
				Case 2: $mes = ' exceeds the size of the HTML form max_file_size limit ';
			Break
				Case 3: $mes = ' file part uploaded ';
			Break
				Case 4: $mes = ' no upload file selected ';
			Break
				Case 6: $mes = ' No temporary directory found ';
			Break
				Case 7: $mes = ' file write Failed ';
			Break
				Case 8: $mes = ' uploaded file interrupted by php extender ';
				
		Break
	Exit ($mes);
	$ext =pathinfo ($fileInfo [' name '],pathinfo_extension);
	
	$ALLOWEXT =array (' jpeg ', ' jpg ', ' png ', ' gif '); Detects the type of uploaded file if (in_array($ext, $ALLOWEXT))	
	{exit (' illegal file type ');	
	//Check upload text size is compliant/$maxSize = 2097152;//2M if ($fileInfo [' Size ']> $maxSize) {exit (' upload file too large ');
	///Detect if the picture is a true picture type//$flag =true;	
		if ($flag) {if (!getimagesize ($fileInfo [' tmp_name '])) {exit (' not a real picture type ');	
	}//detection is uploaded via HTTP post if (!is_uploaded_file ($fileInfo [' tmp_name ']) {exit (' File not uploaded via HTTP POST) ');
	}//$uploadPath = ' uploads ';
		If you do not have this folder, then create an if (!file_exists ($uploadPath)) {mkdir ($uploadPath, 0777, true);
	chmod ($uploadPath, 0777); //new filename Unique $uniName = MD5 (Uniqid (Microtime (True), true).
	$ext; $destination = $uploadPath. '
	/'. $uniName;	
	The @ symbol is to not allow the customer to see the error message if (! @move_uploaded_file ($fileInfo [' Tmp_name '], $destination)) {exit (' file move failed ');
	}//echo ' File upload success ';
	return Array (//' NewName ' => $destination,//' size ' => $fileInfo [' size '],//' type ' => $fileInfo [' type ']//);
return $destination;
 }?>


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.