Php5+utf8 multiple file Upload class _php skills

Source: Internet
Author: User
Tags file upload rar

Some functions are not added, such as automatic renaming, image processing and so on. Can be added as needed.


Use:


$up = new Upfile (root_path. ' data/'. Date ("Ym", Time ()), array (' gif ', ' jpg ', ' jpeg '), true);


$fileimg = $up->upload ($_files[' img '))//Returns an array of file names after uploading, $_files[' img '] for uploaded files


You can use $up->log to view information when uploading.


<?php


//====================================================


FileName:upfile.class.php


Summary: File Upload class


Author:millken (lost Lincoln)


Lastmodifed:2008-6-4


Copyright (c) 2008 millken@gmail.com


//====================================================


if (!defined (' OK ')) exit (__file__. ') Access Denied ');


Class Upfile {


Public $ExtensionFileFormat = Array ();


Public $returninfo = Array ();


Private $ImageFileFormat = Array (' GIF ', ' bmp ', ' jpg ', ' jpe ', ' jpeg ', ' png ');


Private $OtherFileFormat = Array (' Zip ', ' pdf ', ' rar ', ' xls ', ' Doc ', ' ppt ', ' csv ');


Private $savePath;


Private $attachment _path = './upfiles/';


Private $overwrite = false; # whether to overwrite with the same name


Private $maxSize = 0; # file maximum byte, no limit size when 0


Private $ext;


Private $errno = 0;


/* Constructor


* (String) $savePath file save path, default to $attachment_path


* (array) $extensionFileFormat the extension of the custom upload file, not set to $imagefileformat | | $OtherFileFormat


* (BOOL) $overwrite whether to overwrite files with the same name


*/


Public function __construct ($savePath = ', $extensionFileFormat = Array (), $overwrite = False) {


$this->savepath = Empty ($savePath)? $this->attachment_pathsavepath. ' /';


$this->extensionfileformat = Is_array ($extensionFileFormat) $extensionFileFormat: Array ();


$this->overwrite = Is_bool ($overwrite)? $overwrite: false;


}


/* Upload function


* (array) $files an array of files to be uploaded $_files[' attach ']


* (number) $maxSize file maximum bytes, default to 0 unlimited upload size


*/


Public function upload ($files, $maxSize =0) {


$this->maxsize = is_numeric ($maxSize) $maxSize: 0;


if (Isset ($files) && Is_array ($files)) {


if (Is_array ($files [' name '])) {


foreach ($files as $key => $var) {


foreach ($var as $id => $val) {


$attachments [$id] [$key] = $val;


}


}


} else {


$attachments [] = $files;


}


}


Self::check_file_type ($attachments);


if (Empty ($this->filelist)) {


$this->log. = "The list of files to be uploaded is empty. \ n ";


return Array ();


}


if (!self::makedirectory () | |! @is_writable ($this->savepath)) {


$this->log. = $this->savepath. "cannot be created or its permissions are not writable." \ n ";


return Array ();


}


$filearray = Array ();


foreach ($this->filelist as $k => $f) {


if ($this->maxsize && $f [' Size ']> $this->maxsize) {


$this->log. = $f [' name ']. "Its size exceeds the set value:". $this->maxsize. " \ n ";


}elseif ($this->overwrite = = False && file_exists ($this->savepath. $f [' name '])} {


$this->log. = $f [' name ']. "already exists in directory:". $this->savepath. "\ n";


}else{


@unlink ($this->savepath. $f [' name ']);


if (@move_uploaded_file ($f [' Tmp_name '], $this->savepath. mb_convert_encoding ($f [' name '], ' GBK ', ' Utf-8 '))} {// If you do not encode the conversion, Chinese will not be able to support


$this->log. = $f [' name ']. "Successfully uploaded to directory:". $this->savepath. " \ n ";


$filearray [$k] = $this->savepath. $f [' name '];


}else{


$this->log. = $f [' name ']. "Upload failed. \ n ";


}


}


}


return $filearray;


}


/* The type of the detection file


* (array) $files Arrays of files


*/


Private Function Check_file_type ($files) {


$this->filelist = Array ();


foreach ($files as $key => $file) {


if ($file [' error '] = = 0) {


$ext = Strtolower (substr ($file [' name '], Strrpos ($file [' name '], '. ') + 1));


$str = @file_get_contents ($file [' tmp_name '],false,null,0,20);


if (In_array ($ext, array (' jpg ', ' jpeg ')) && substr ($str, 0, 3)!== "\xff\xd8\xff") | | ($ext = = ' gif ' && substr ($str, 0, 4)!== ' GIF8 ') | | ($ext = = ' png ' && substr ($str, 0, 8)!== "\x89\x50\x4e\x47\x0d\x0a\x1a\x0a") | | ($ext = = ' bmp ' && substr ($str, 0, 2)!== ' BM ') | | ($ext = = ' swf ' && (substr ($str, 0, 3)!== ' CWS ' | | substr ($STR, 0, 3)!== ' FWS ') | | ($ext = = ' Zip ' && substr ($str, 0, 4)!== "pk\x03\x04") | | ($ext = = ' rar ' && substr ($str, 0, 4)!== ' rar! ') || ($ext = = ' pdf ' && substr ($str, 0, 4)!== "\x25pdf") | | ($ext = = ' chm ' && substr ($str, 0, 4)!== ' ITSF ') | | ($ext = = ' rm ' && substr ($str, 0, 4)!== "\X2ERMF") | | ($ext = = ' exe ' && substr ($str, 0, 2)!== "MZ") | | (In_array ($ext, Array (' Doc ', ' xls ', ' ppt ')) && substr ($str, 0, 4)!== "\xd0\xcf\x11\xe0") {


$this->log. = $file [' name ']. "The file type does not match the file content. \ n ";


}elseif (!empty ($this->extensionfileformat) && In_array ($ext, $this->extensionfileformat)) | | (Empty ($this->extensionfileformat) && (In_array ($ext, $this->imagefileformat) | | In_array ($EXT, $this- >otherfileformat))) {


$this->filelist[$key] = $file;


}else{


$this->log. = $file [' name ']. "does not conform to the type of upload file. \ n ";


@unlink ($file [' tmp_name ']);


}


}


}


}


/* Generate upload Directory


*


*/


Private Function Makedirectory () {


$directoryName = Str_replace ("\", "/", $this->savepath);


$dirNames = explode ('/', $directoryName);


$total = count ($dirNames);


$temp = ';


for ($i =0; $i < $total; $i + +)


{


$temp. = $dirNames [$i]. ' /';


if (!is_dir ($temp))


{


$oldmask = @umask (0);


if (! @mkdir ($temp, 0777)) return false;


@umask ($oldmask);


}


};


if (Is_dir ($this->savepath)) {


return true;


} else {


return false;


};


}


}


?>


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.