Single file upload class

Source: Internet
Author: User

<? PHP
/**
* Page function: File Upload class. If a file with the same name exists, replace it
* Author: Happy with the wind
* Creation Time: 2006-3-6
* Other Instructions:
* 1. In the form, <input name = "up" type = "file">, the name parameter must be "up"
* 2. Check whether the file has been uploaded before use: $ _ FILES ['up'] ['SIZE']! = 0? Option: no option;
*/

Class class_ftp
{
Public $ file_ream; // returns the extension.
Public $ err; // returns the upload information.
Public $ off = false; // process switch/whether the upload is successful or not

/*
Parameter settings: $ file_max maximum size of uploaded files (KB)
$ File_dir upload path
$ File_type: supported types, for example, $ file_type = array ("image/pjpeg", "image/GIF ","**","**","**");
$ File_name rename the name after uploading (excluding the extension)
*/
Function _ construct ($ file_max, $ file_dir, $ file_type, $ file_name)
{
$ File_size = $ file_max * 1024; // convert it to the default byte of the system.
$ Num = sizeof ($ file_type); // obtain the number of supported types

// Check the file type
For ($ I = 0; $ I <$ num; $ I ++)
{
If ($ file_type [$ I] === _ FILES ['up'] ['type'])
{
$ This-> off = true;
Break;
}
}
If ($ this-> off = false) $ this-> err = "this file format is not supported! ";

// Check the file size
If ($ this-> off)
{
If ($ file_size <$ _ FILES ['up'] ['SIZE'])
{
$ This-> off = false;
$ This-> err = "the file size cannot exceed". $ file_max. "K! ";
}
}

// Check the copy file to the specified directory
If ($ this-> off)
{
// Get the extension
Switch ($ _ FILES ['up'] ['type'])
{
Case "image/pjpeg": $ this-> file_ream = ". jpg"; break;

Case "image/GIF": $ this-> file_ream = ". GIF"; break;

Case "text/plain": $ this-> file_ream = ". txt"; break;

Case "application/MSWord": $ this-> file_ream = ". Doc"; break;

Case "application/X-Shockwave-flash": $ this-> file_ream = ". SWF"; break;

Case "application/octet-stream": $ this-> file_ream = ". fla"; break;

Case "application/octet-stream": $ this-> file_ream = ". rar"; break;

Case "application/X-zip-compressed": $ this-> file_ream = ". Zip"; break;

Case "application/octet-stream": $ this-> file_ream = ". PSD"; break;
}

// Copy and upload files
Move_uploaded_file ($ _ FILES ['up'] ['tmp _ name'], $ file_dir. $ file_name. $ this-> file_ream );
If ($ _ FILES ['up'] ['error'] = 0)
{
$ This-> err = "the file is uploaded successfully! ";
}
Else
{
$ This-> off = false;
$ This-> err = "File Upload Failed. Please try again! ";
}
}
}
} // Class end
?>

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.