The enhancement function for PHP file Upload becomes a dumb first, but it does not affect the super-powerful function if it is not simplified. In addition, the powerful debugging function is added, of course you can turn it into a class method! And "." before the extension is not required ).
The code is as follows:
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 "/"
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!