And "." before the extension is not required ).
Copy codeThe Code is as follows: <? Php
Function upload ($ file, $ path, $ type ){
$ State = array ();
$ State ['error'] = "true ";
$ Alltype = ""; // all types that can be uploaded, connected "/"
$ Path = trim ($ path );
// Add "/" to the end of $ path "/"
If (strlen (strrchr ($ path, '/') <= 1 ){
$ Path. = "/";
}
// Add the start "." To the type
// Synthesize 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'] = "{$ alltype} type file not uploaded! ";
$ 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 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. The file size exceeds the server space! "; $ State ['errorid'] = 1; return $ state;
Case 2: $ state ['error'] = $ state ['name']. "Upload Failed. The file size to be uploaded exceeds the browser limit! "; $ State ['errorid'] = 2; return $ state;
Case 3: $ state ['error'] = $ state ['name']. "Upload Failed. only part of the file is uploaded! "; $ State ['errorid'] = 3; return $ state;
Case 4: $ state ['error'] = $ state ['name']. "Upload Failed. The file to be uploaded is not found! "; $ State ['errorid'] = 4; return $ state;
Case 5: $ state ['error'] = $ state ['name']. "Upload Failed. The temporary folder on the server is lost! "; $ State ['errorid'] = 5; return $ state;
Case 6: $ state ['error'] = $ state ['name']. "Upload Failed. An error occurred while writing the file to the Temporary Folder! "; $ 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 file type to be uploaded! ({$ Alltype })";
$ State ['errorid'] = 10;
Return $ state;
}
}
}
?>
Returns an array.
$ Key value
Error details
Errorid error id (value only when an error occurs)
Type file extension (prefixed with ".")
File Name Uploaded By upname
Time the upload time (the number of seconds since the Unix epoch)
Error Type
Errorid error
False // upload successful
1. Upload Failed. The file size exceeds the server space!
2. Upload Failed. The file size to be uploaded exceeds the browser size!
3. Upload Failed. only part of the file is uploaded!
4. Upload Failed. The file to be uploaded is not found!
5. Upload Failed. The temporary folder on the server is lost!
6. Upload Failed. An error occurred while writing the file to the Temporary Folder!
8. This type of file is not uploaded! // The file object in the form is empty.
10 Upload Failed, location error!