PHP upload file enhancement function

  • 2020-03-31 20:56:03
  • OfStack

The file object in the form, such as $_FILES['doc'], $path for the saved path, and $type for the restricted upload type (it is an array, such as the restricted upload of doc and docx and format, this parameter can be filled in array(" doc ", ".docx "), and the extension ". "is not required).
 
<?php 
function upload($file,$path,$type){ 
$state = array(); 
$state['error'] = "true"; 
$alltype = ""; //All types that can be uploaded, connected by "/"
$path = trim($path); 
//I'm going to put "/" at the end of $path
if(strlen(strrchr($path, ' / ' )) <= 1){ 
$path .= "/"; 
} 
//Add a "to the type."
//Combine all types of strings and connect them with "/". Span id = "more - 78"> </ span>
foreach($type as $key=>$typeone){ 
$type[$key] = $typeone = trim($typeone); 
if(strlen(strchr($typeone,".")) != strlen($typeone)){ 
$type[$key] = ".".$typeone;} 
$alltype .= $typeone. ' / ' ; 
} 
$alltype = substr($alltype,0,strlen($alltype)-1); //Remove the last "/"
if(empty($file['name'])){ 
$state['error'] = " No upload {$alltype} Type file! "; 
$state['errorid'] = 8; 
return $state; 
} else { 
if (!$file['error']) { 
$state['name'] = $file['name']; 
$state['type'] = strrchr($state['name'], ' . ' ); 
if(in_array($state['type'],$type)){ 
$time = date("U"); 
$state['upname'] = $time.rand(1000,9999).$state['type']; //The file name
if(copy($file['tmp_name'],$path.$state['upname'])){ 
$state['time'] = date("U"); //Upload time
$state['error'] = false; 
return $state; 
} else { 
switch($file( ' error ' )){ 
case 1: $state['error'] = $state['name']." Upload failed, file size exceeded the size of the server space! ";$state['errorid'] = 1;return $state; 
case 2: $state['error'] = $state['name']." Upload failed, the file size to upload exceeded the browser limit! ";$state['errorid'] = 2;return $state; 
case 3: $state['error'] = $state['name']." Upload failed, file only partially uploaded! ";$state['errorid'] = 3;return $state; 
case 4: $state['error'] = $state['name']." Upload failed, did not find the file to upload! ";$state['errorid'] = 4;return $state; 
case 5: $state['error'] = $state['name']." Upload failed, server temporary folder lost! ";$state['errorid'] = 5;return $state; 
case 6: $state['error'] = $state['name']." Upload failed, file written to temporary folder error! ";$state['errorid'] = 6;return $state; 
default: $state['error'] = $state['name']." Upload failed, location error! ";$state['errorid'] = 10;return $state; 
} 
} 
} else { 
$state['error'] = $state['name']." Upload failed! Does not match the type of file to be uploaded! ({$alltype})"; 
$state['errorid'] = 10; 
return $state; 
} 
} 
} 
?> 

The return value is an array
$key value
Error details
Errorid errorid
Type file extension (with "before")
Upname is the file name uploaded
Time to upload (seconds since Unix era (1970-01-01 00:00))

Wrong type
Errorid error
False // upload successful
Upload failed, the file size exceeded the space size of the server!
Upload failed, the file size to upload exceeded the browser limit!
Upload failed, the file is only partially uploaded!
Upload failed, did not find the file to upload!
Upload failed, server temporary folder lost!
Upload failed, file written to temporary folder error!
Did not upload this type of file! // the file object in the form is empty
Upload failed, location error!

Related articles: