Php implements Common File Upload classes,

Source: Internet
Author: User

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
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.