Php implements Common File Upload classes,
<? Php
/**
* File Upload
* @ Param _ path: Server File Storage path
* @ Param _ allowType: Specifies the object type that can be uploaded and the corresponding MIME.
* @ Param _ file: information of the uploaded file
*/
Class Upload {
Private $ _ path;
Private $ _ allowType;
Private $ _ file;
/**
* Constructor
* @ Param string: path for storing uploaded files on the server
*/
Function _ construct ($ path = '')
{
$ This-> _ path = $ path;
$ This-> _ allowType = array (
// Images
'Bmp '=> 'image/x-ms-bmp ',
'Jpg '=> 'image/jpeg ',
'Jpeg '=> 'image/jpeg ',
'Gif' => 'image/gif ',
'Png '=> 'image/png ',
'Tif '=> 'image/tiff ',
'Tiff '=> 'image/tiff ',
'Tga '=> 'image/x-targa ',
'Psd '=> 'image/vnd. adobe. photoshop ',
// Text
'Txt '=> 'text/plain ',
'Php' => 'text/x-php ',
'Html' => 'text/html ',
'Htm '=> 'text/html ',
'Js' => 'text/javascript ',
'Css '=> 'text/css ',
'Rtf '=> 'text/rtf ',
'Rtfs' => 'text/rtfs ',
'Py' => 'text/x-python ',
'Java' => 'text/x-java-source ',
'Rb' => 'text/x-ruby ',
'Sh' => 'text/x-shellscript ',
'Pl '=> 'text/x-perl ',
'SQL' => 'text/x-SQL ',
// Application
'Exe '=> 'application/octet-stream ',
'Doc' => 'application/vnd. ms-word ',
'Docx' => 'application/vnd. ms-word ',
'Xls '=> 'application/vnd. ms-excel ',
'Ppt '=> 'application/vnd. ms-powerpoint ',
'Pps '=> 'application/vnd. ms-powerpoint ',
'Pdf '=> 'application/pdf ',
'Xml' => 'application/xml ',
// Audio
'Mp3' => 'audio/mpeg ',
'Mid '=> 'audio/midi ',
'Ogc' => 'audio/ogc ',
'Mp4a '=> 'audio/mp4 ',
'Wav '=> 'audio/wav ',
'Wma' => 'audio/x-ms-wma ',
// Video
'Av' => 'video/x-msvideo ',
'Dv' => 'video/x-dv ',
'Mp4' => 'video/mp4 ',
'Mpeg '=> 'video/mpeg ',
'Mpg' => 'video/mpeg ',
'Mov' => 'video/quicktime ',
'Wm '=> 'video/x-ms-wmv ',
'Flv' => 'video/x-flv ',
'Mkv '=> 'video/x-matroska'
);
}
/**
* Upload function
* @ Param string: name value of the form Element
* @ Return [type]
*/
Public function upload ($ txtName = '')
{
$ This-> _ file = $ _ FILES [$ txtName];
If ($ this-> _ file ['error'] = 0 ){
$ FileType = end (explode ('.', $ this-> _ file ['name']);
$ AllowType = array ();
Foreach ($ this-> _ allowType as $ item => $ value ){
$ AllowType [] = $ item;
}
If (! In_array ($ fileType, $ allowType )){
Die ('the format of the uploaded file is incorrect! ');
} Else {
If (move_uploaded_file ($ this-> file ['tmp _ name'], ($ this-> path). $ this-> file ['name'])
{
Echo "<script> alert ('upload successful! ') </Script> ";
}
Else
{
Echo "<script> alert ('upload failed! '); </Script> ";
}
}
} Else {
// Incorrect upload
Switch ($ this-> file ['error']) {
Case 1:
Die ('file size exceeds system limit. ');
Break;
Case 2:
Die ('file size exceeds the predefined limit. ');
Break;
Case 3:
Die (the 'file is completely uploaded. ');
Break;
Case 4:
Die ('no files are uploaded. ');
Break;
Default:
Die ('upload error ');
Break;
}
}
}
// End upload
}