A perfect php file Upload class Usage Example _php tutorial

Source: Internet
Author: User
Tags php file upload
This file upload class can customize the upload file size and upload file type and file save address, use up also daxing very method, there is a text is if the Chinese file name is upload will be garbled, so everyone solution is to save the page to Uft8 can be solved.

Today is finally able to update the Paperen website, uh ... Because I write a paper is tired, so I published an article about the upload file class East Bar, this class is also written by themselves, do not know if you have any use, if you think you can take it. But in the code above still did not do well, oneself is in the UTF8 page to submit the data, so the resulting file if you keep the original Chinese name in the folder to see the name is garbled, if you do not want to appear garbled words need to be encoded conversion.

The code is as follows Copy Code

Define (' No_file ', ' no upload file ');
Define (' Not_allow_ext ', ' file type is not within allowable range ');
Define (' Not_allow_size ', ' file size is not within allowable range ');
Define (' Has_the_file ', ' The file already exists ');
Define (' upload_failed ', ' upload failed ');
Define (' upload_success ', ' upload success ');

Class File_uploader
{

var $_file;
var $_filesize;
var $_fileext;
var $_filedir;
var $_filename;
var $_filetmpname;

var $allowsize;
var $allowext;

var $neednewname;
var $newname;
var $syslang;

var $report;

function Ready ($filedir = ", $file, $allowsize =", $allowext = ", $neednewname = false, $report = 0) {
$this->_filedir = Is_dir ($filedir)? $filedir: ";
if (Empty ($file) | | |!isset ($file [' size ']) | | $file [' size '] = = 0) $this->error (no_file);
$this->_filesize = $file [' Size '];
$this->_filename = $file [' name '];
$this->_filetmpname = $file [' Tmp_name '];

$this->allowsize = $allowsize;
$this->allowext = $allowext;

$this->neednewname = ($neednewname)? True:false;
$this->newname = ";

$this->report = $report;
}

function Do_upload () {
if (!is_uploaded_file ($this->_filetmpname)) $this->error (no_file);
if ($this->chk_ext ()) {
$this->error (Not_allow_ext);
Return ';
}
if ($this->chk_size ()) {
$this->error (not_allow_size);
Return ';
}
if ($this->neednewname) $this->newname = $this->generate_name (). ".". $this->get_fileext ();
if ($this->chk_hasfile ()) {
$this->error (Has_the_file);
Return ';
}
$filename = Empty ($this->newname)? @iconv (' Utf-8 ', ' gb2312 ', $this->_filename): $this->newname;
@chmod ($this->_filedir. $filename, 0777);
if (Move_uploaded_file ($this->_filetmpname, $this->_filedir. $filename)) {
return $this->result ();
}else{
$this->error (upload_failed);
Return ';
}
}

function Chk_ext () {
if (Empty ($this->allowext) | | | In_array ($this->get_fileext (), Explode ("|", $this->allowext))) return false;
return true;
}

function Chk_size () {
if (Empty ($this->allowsize) | | | get_filesize <= $this->allowsize*1024*1024) return false;
return true;
}

function Get_filesize () {
return $this->_filesize;
}

function Get_fileext () {
Return substr ($this->_filename,strrpos ($this->_filename, ".") +1);
}

function Generate_name () {
Return substr (MD5 (Time ()), 26);
}

function Chk_hasfile () {
Return Is_file ($this->_filedir. $this->_filename);
}

Function error ($tip) {
Echo $tip;
}

function result () {
if ($this->report) {
$filename = Empty ($this->newname)? $this->_filename: $this->newname;
$arr = Array (' filename ' = = $filename, ' filesize ' = + $this->_filesize, ' tip ' = upload_success);
return $arr;
}else{
return upload_success;
}
}
}

/*** use method and parameter description ***/
/***
The first parameter, $dir, is the path where the uploaded file is stored
The second parameter is $_files for you. Upload file variable
The third parameter allows the file size unit to be MB
The fourth parameter allows a file type format of Jpg|png|gif
Whether the fifth parameter requires a new file name to be generated
The sixth parameter returns the hint format 0 for the direct prompt upload correctly 1 then returns an array of arrays (' filename ' = = $filename, ' filesize ' + $this->_filesize, ' tip ' = = upload_success);
***/

Require ("class file");
$u = new File_uploader;
$u->ready ($dir, $_files[' Upload_file '), False, False, true, 0);
echo $u->do_upload ();

?>

Just say this class how to call it, the method is very simple?

The code is as follows Copy Code

$dir = ' upload/';
Require ("upload_class.php");
$u = new File_uploader;
$u->ready ($dir, $_files[' Upload_file '), False, False, true, 0);
echo $u->do_upload ();

Like the one that was said by the hair-dyed ad n years ago, "get left!" It's so easy to dye black hair! "Like, the upload file here is also" upload the file is so simple! ”。 Of course that $_files[' Upload_file ' is the name of the control that corresponds to the file type in the form. and need not after uploading after the output prompt to see yourself, about the parameters in the code that has detailed instructions, but here is also put out.

Description of the parameters of the Ready method:

The first parameter, $dir, is the path where the uploaded file is stored
The second parameter is $_files for you. Upload file variable
The third parameter allows the file size unit to be MB
The fourth parameter allows a file type format of Jpg|png|gif
Whether the fifth parameter requires a new file name to be generated
The sixth parameter is the returned hint format 0 for the direct prompt upload correctly 1 then returns an array of arrays (' filename ' = + uploaded file name, ' filesize ' = file size, ' tip ' = + successfully uploaded);

Very good! This completes an article, but also spent a lot of time ...?

http://www.bkjia.com/PHPjc/632737.html www.bkjia.com true http://www.bkjia.com/PHPjc/632737.html techarticle This file upload class can customize the upload file size and upload file type and file save address, use up also daxing very method, there is a text is if the Chinese file name is on ...

  • 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.