A very useful file Upload class in PHP _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
A very useful file Upload class in PHP .? PhpclassFileUpload {private $ filepath; set the file upload path private $ allowtypearray (jpg, jpeg, gif, bmp); default file type private $ maxsize2000000; default file
Class FileUpload {

Private $ filepath; // set the path of the uploaded file
Private $ allowtype = array ('jpg ', 'jpeg', 'GIF', 'bmp '); // Default file type
Private $ maxsize = 2000000; // Default file size
Private $ israndname = true; // whether to use random file names
Private $ originName; // source file name
Private $ tmpFileName; // temporary file name
Private $ fileType; // file type
Private $ fileSize; // file size
Private $ newFileName; // name of the new file
Private $ errorNum; // error code
Private $ errorMess = ""; // used to prompt error reports
// Functions used for file initialization
Function _ construct ($ options = array ()){
Foreach ($ options as $ key => $ val ){
$ Key = strtolower ($ key); // Set the attribute names to lowercase.
// Get_class_vars (get_class ($ this ))
// Obtain all attributes of the current class
If (! In_array ($ key, get_class_vars (get_class ($ this )))){
Continue;
}
Else
{
$ This-> setOption ($ key, $ val );
}
}
}

// Define different error levels
Private function getError (){
$ Str = "an error occurred while uploading the file {$ this-> originName :";


Switch ($ this-> errorNum ){
Case 4:
$ Str. = "The file is not uploaded ";
Break;
Case 3:
$ Str. = "only part of the file is uploaded ";
Break;
Case 2:
$ Str. = "the uploaded file exceeds the value of the MAX_FILE_SIZE option specified in the HTML form ";
Break;
Case 1:
$ Str. = "the upload_max_filesize option value in php. ini has been quarrelled during file uploading ";
Break;
Case-1:
$ Str. = "unsupported type ";
Break;
Case-2:
$ Str. = "the uploaded file is too large to exceed {$ this-> maxSize} bytes ";
Break;
Case-3:
$ Str. = "Upload failed ";
Break;
Case-4:
$ Str. = "an error occurred while creating the upload Directory. please specify the upload Directory Again ";
Break;
Case-5:
$ Str. = "the path of the file to be uploaded must be specified ";
Break;
Default:
$ Str. = "Unknown error ";

}

Return $ str ."
";
}

// Function for assigning values to member attributes
Private function setOption ($ key, $ val ){
$ This-> $ key = $ val;
}
// Check the path of the uploaded file
Private function checkFilePath (){
// If the file path is empty
If (empty ($ this-> filepath )){
$ This-> setOption ('errornum',-5 );
Return false;
}
// Determine whether the path exists and is writable
If (! File_exists ($ this-> filepath) |! Is_writable ($ this-> filepath )){
// @ Is the error blocker @ is to ignore the error prompt and make it wrong
// The error message is not displayed in the program.
If (! @ Mkdir ($ this-& gt; filepath, 0755 )){
$ This-> setOption ('errornum',-4 );
Return false;
}
}
Return true;
}

// Check the file size function
Private function checkFileSize (){
If ($ this-> fileSize> $ this-> maxsize ){
$ This-> setOption ("errorNum",-2 );
Return false;
}
Else
{
Return true;
}

}

// Check the type of the uploaded file
Private function checkFileType (){
If (in_array (strtolower ($ this-> fileType), $ this-> allowtype ))
{
Return true;
}
Else
{
$ This-> setOption ("errorNum",-1 );
Return false;
}
}
// Upload an object
Function uploadFile ($ fileField ){
$ Return = true;
$ Name = $ _ FILES [$ fileField] ["name"]; // name submitted by post
$ Tmp_name = $ _ FILES [$ fileField] ["tmp_name"]; // temporary file name during Upload
$ Size = $ _ FILES [$ fileField] ["size"];
$ Error = $ _ FILES [$ fileField] ["error"];
If ($ this-> setFiles ($ name, $ tmp_name, $ size, $ error )){
If ($ this-> checkFileSize () & $ this-> checkFileType ()){
$ This-> setNewFileName ();
If ($ this-> copyFile ())
{
$ Return = true;
}
Else
{
$ Return = false;
}
}
Else
{
$ Return = false;
}
}
Else
{
$ Return = false;
}
// Check whether the file path is incorrect
If (! $ This-> checkFilePath ()){
$ This-> errorMess = $ this-> getError ();
Return false;
}

If (! $ Return)
{
$ This-> errorMess = $ this-> getError ();
}
Return $ return;
}
// Set content related to $ _ FILES
Private function setFiles ($ name = "", $ tmp_name = "", $ size = 0, $ error = 0 ){
$ This-> setOption ("errorNum", $ error );
If ($ this-> errorNum ){
Return false;
}
$ Arrstr = explode (".", $ name );
$ This-> setOption ("fileType", strtolower ($ arrstr [count ($ arrstr)-1]);
$ This-> setOption ("originName", $ name );
$ This-> setOption ("tmpFileName", $ tmp_name );
$ This-> setOption ("fileSize", $ size );
Return true;
}

// Obtain the name of the uploaded file
Function getNewFileName (){
Return $ this-> newFileName. 'Upload successful! ';
}


// The function that displays the error message when the file fails to be uploaded
Function getErrorMsg (){
Return $ this-> errorMess;
}
Private function proRandName (){
$ Filename = date ("YmdHis"). rand (100,999 );
Return $ filename. ".". $ this-> fileType;
}

// Set the name of the uploaded file
Private function setNewFileName (){
If ($ this-> israndname ){
$ This-> setOption ('newfilename', $ this-> proRandName ());
}
Else
{
$ This-> setOption ('newfilename', $ this-> originName );
}
}

// Copy an object
Private function copyFile (){
If (! $ This-> errorNum ){
$ Filepath = rtrim ($ this-> filepath ,'/')."/";
$ Filepath. = $ this-> newFileName;
// Copy the object to the specified path
If (@ move_uploaded_file ($ this-> tmpFileName, $ filepath )){
Return true;
}
Else
{
$ This-> setOption ('errornum',-3 );
Return false;
}
}
}


}

?>


Http://www.bkjia.com/PHPjc/477198.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/477198.htmlTechArticle? Php class FileUpload {private $ filepath; // set the path of the uploaded file to private $ allowtype = array (jpg, jpeg, gif, bmp ); // Default file type private $ maxsize = 2000000; // Default file type...

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.