The file object in the form, such as $_files[' Doc ', $path for the saved path, $type to restrict the type of upload (he is an array, such as restricting the upload of doc and docx and format, that parameter can be filled in with an array ("Doc", ". docx"), And there is no "." Before the extension. Not required).
Copy the Code code as follows:
function upload ($file, $path, $type) {
$state = Array ();
$state [' error '] = "true";
$alltype = ""; All types that can be uploaded, connected with "/".
$path = Trim ($path);
Add "/" to the end of $path
if (strlen (STRRCHR ($path, '/')) <= 1) {
$path. = "/";
}
Add the beginning "." to the type.
Combine all types of strings and connect them with "/"
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 ']; File naming
if (copy ($file [' Tmp_name '], $path. $state [' Upname '])) {
$state [' time '] = date ("U"); Time of uploading
$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 exceeds the browser limit! "; $state [' errorid '] = 2;return $state;
Case 3: $state [' error '] = $state [' name ']. " Upload failed, the file is 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 temp folder is missing! "; $state [' errorid '] = 5;return $state;
Case 6: $state [' error '] = $state [' name ']. " Upload failed, file write to Temp 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 you want to upload! ({$alltype}) ";
$state [' errorid '] = 10;
return $state;
}
}
}
?>
Returns a value of an array
$key value
Error details
ErrorID Error ID (value only if an error occurs)
Type file extension (with the extension ".") )
Upname uploaded file name
Time uploaded (the number of seconds since the Unix era (1970-01-01 00:00))
Type of error
ErrorID Error
False//Upload succeeded
1 upload failed, file size exceeded the size of the server space!
2 upload failed, the file size to upload exceeds the browser limit!
3 upload failed, the file is only partially uploaded!
4 upload failed, did not find the file to upload!
5 upload failed, Server temp folder is missing!
6 upload failed, file write to Temp folder Error!
8 did not upload this type of file! The file object in the form is empty
10 upload failed, location error!
The above describes the two computers how to file php upload file enhancement function, including the two computers how to file the content, I hope the PHP tutorial interested in a friend helpful.