Php advanced perfect multi-File Upload Program-PHP source code

Source: Internet
Author: User
Ec (2); & lt ;? Php $ action $ _ GET [action]; require_once (auc. main. class. inc. php); $ aucnewauc (); if ($ actionuploadfile) {& nbsp; $ aucnewauc (); & nbsp; $ result $ auc-& gt; upload (& script ec (2); script

$ Action = $ _ GET ['action'];
Require_once ('auc. main. class. inc. php ');

$ Auc = new auc ();

If ($ action = 'uploadfile '){
$ Auc = new auc ();

$ Result = $ auc-> upload ("file ");
If (is_array ($ result )){
Echo 'something Went Wrong ';
Echo'

';
var_dump($result);
echo '
';
} Else {
Echo 'all OK ';
}
} Else {
?>




Advanced Upload Class-Demo





Class File

Class auc {
Public $ errors = array (); // array used to store any errors that occur.
Public $ upload_dir = ''; // the upload_dir being used by the script
Public $ make_safe = false; // default don't modify the file name to safe version
Public $ max_file_size = 1048576; // Max File Size in Bytes, 1 MB
Public $ overwrite = false; // default don't overwrite files that already exsist
Public $ check_file_type = false; // don't check for file type by default but can check for allowed and denied files.
Public $ allowed_mime_types = array ('image/jpeg ', 'image/png', 'image/gif', 'image/tiff '); // array of allowed mime types used when check_file_type is set to allowed
Public $ denied_mime_types = array ('application/x-php', 'text/html'); // array of denied mime types used when check_file_type is set to denied

/**
* Check if the upload dir is valid, if it is not valid attempt to make the dir, if dir is succesfully created chmod it to 0777.
* If any elments fail return false else set upload_dir and return true.
* @ Param string $ dir
* @ Param boolean $ mkdir
* @ Return true or false
*/
Public function upload_dir ($ dir, $ mkdir = false ){
$ Errors = & $ this-> errors;
$ Status = true;

If (! Is_dir ($ dir )){
If ($ mkdir ){
If (! Mkdir ($ dir )){
$ Status = false;
} Else {
If (! Chmod ($ dir, 0777) $ status = false;
}
} Else {
$ Status = false;
}
}

If ($ status ){
$ This-> upload_dir = $ dir;
Return true;
} Else {
$ Errors ['normal'] [] = 'upload Dir is Not Valid and/or a dir cocould not be created/chmod .';
Return false;
}
}

/**
* Check that the upload dir is valid and that it is writeable
*
* @ Param string $ dir
* @ Return true or false
*/
Public function check_dir ($ dir ){
If (! Is_dir ($ dir) |! Is_writable ($ dir) return false;

Return true;
}

/**
* Make the uploaded file name safe
*
* @ Param string $ file_name
* @ Return safe file name
*/
Public function make_safe ($ file_name ){
Return str_replace ('', '_', $ file_name );
}

/**
* Check the Attemted Uploads for errors etc if everything goes good move the file, to the upload_dir.
*
* @ Param array $ object
* @ Return unknown
*/
Public function upload ($ object ){
$ Errors = & $ this-> errors;

If (empty ($ errors ['General ']) {
If (empty ($ this-> upload_dir) $ this-> upload_dir = dirname (_ FILE __). '/'; // if no default upload_dir has been specified used the current dir.

If ($ this-> check_dir ($ this-> upload_dir )){
$ Files = $ _ FILES [$ object];
$ Count = count ($ files ['name'])-1;

Echo'

';
var_dump($files);
echo '
';

For ($ current = 0; $ current <= $ count; $ current ++ ){
$ Error = '';
Try {
// Check for $ _ FILES Errors
Switch ($ files ['error'] [$ current]) {
Case 0: break;
Case 1: $ error = $ files ['name'] [$ current]. 'exceeds the upload_max_filesize directive in php. ini '; break;
Case 2: $ error = $ files ['name'] [$ current]. 'exceeds the MAX_FILE_SIZE directive that was specified in the HTML form'; break;
Case 3: $ error = $ files ['name'] [$ current]. 'was only partially uploaded'; break;
Case 4: $ error = 'no file was uploaded'; break;
Case 6: $ error = 'missing a temporary folder'; break;
Case 7: $ error = 'failed'. $ files ['name'] [$ current]. 'To disk'; break;
Case 8: $ error = $ files ['name'] [$ current]. 'stopped by extension'; break;
Default: $ error = 'unidentified Error, caused by '. $ files ['name'] [$ current]; break;
}
If ($ error)
Throw new TrigerErrorException ($ error, $ files ['name'] [$ current]);

// Check that the file is not empty
If ($ files ['SIZE'] [$ current] <= 0)
Throw new TrigerErrorException ($ files ['name'] [$ current]. 'Is empty', $ files ['name'] [$ current]);

// Check that the file does not exceed the defined max_file_size
If ($ this-> max_file_size ){
If ($ files ['SIZE'] [$ current] >=$ this-> max_file_size)
Throw new TrigerErrorException ($ files ['name'] [$ current]. 'exceeds defined max_file_size ', $ files ['name'] [$ current]);
}

If ($ this-> check_file_type = 'allowed '&&! In_array ($ files ['type'] [$ current], $ this-> allowed_mime_types )){
Throw new TrigerErrorException ($ files ['name'] [$ current]. 'Is not an allowed type', $ files ['name'] [$ current]);
} Elseif ($ this-> check_file_type = 'denied '& in_array ($ files ['type'] [$ current], $ this-> denied_mime_types )){
Throw new TrigerErrorException ($ files ['name'] [$ current]. 'is a denied type', $ files ['name'] [$ current]);
}

// If make_safe is true call make safe function
If ($ this-> make_safe)
$ Files ['name'] [$ current] = $ this-> make_safe ($ files ['name'] [$ current]);

// If overwrite is false and the file exists error
If (! $ This-> overwrite & file_exists ($ this-> upload_dir. $ files ['name'] [$ current])
Throw new TrigerErrorException ($ files ['name'] [$ current]. 'Already exsists ', $ files ['name'] [$ current]);

// Move the uploaded file, error if anything goes wrong.
If (! Move_uploaded_file ($ files ['tmp _ name'] [$ current], $ this-> upload_dir. $ files ['name'] [$ current])
Throw new TrigerErrorException ($ files ['name'] [$ current]. 'Could not be moved', $ files ['name'] [$ current]);
} Catch (TrigerErrorException $ e ){
$ Errors [$ files ['name'] [$ current] [] = $ e-> Message ();
}
}

If (empty ($ errors )){
// Return true if there where no errors
Return true;
} Else {
// Return the errors array if there where any errros
Return $ errors;
}
} Else {
// Return false as dir is not valid
$ Errors ['normal'] [] = "The Specified Dir is Not Valid or is Not Writeable ";
Return false;
}
}
}
}

/**
* Handle the Exceptions trigered by errors within upload code.
*
*/
Class TrigerErrorException extends Exception {
Protected $ file = "";
Public function _ construct ($ message, $ file = "", $ code = 0 ){
$ This-> file = $ file;
Parent: :__ construct ($ message, $ code );
}

Public function Message (){
Return "{$ this-> message }";
}
}
?>

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.