Php multi-file upload multi-image upload program code

Source: Internet
Author: User
Tags foreach file upload

File upload example

The code is as follows: Copy code

<? Php
Header ('content-type: text/html; charset = utf-8 ');
Require ('uploadfile. Php ');

If (isset ($ _ POST ['submit ']) {
$ Uploads = $ _ FILES ['file'];
$ Num_file = count ($ uploads ['name']);

$ Up = new UploadFile ($ uploads, 'uploads, 1024 );
$ Num = $ up-> upload ();

If ($ num = $ num_file ){
Echo 'all files uploaded successfully ';
Exit;
} Else {
Echo $ num, 'File uploaded successfully <br/> ';
Echo $ up-> showErrorInfo ();
Exit;
}
}

?>


<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.111cn.net/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = utf-8"/>
<Title> Untitled Document </title>
</Head>

<Body>
<Form action = "uup. php" method = "post" enctype = "multipart/form-data">
<P> <input name = "file []" type = "file"/> </p>
<P> <input name = "file []" type = "file"/> </p>
<P> <input name = "file []" type = "file"/> </p>
<P> <input name = "file []" type = "file"/> </p>
<Input name = "submit" type = "submit"/>
</Form>
</Body>
</Html>

File upload code

The code is as follows: Copy code

<? Php
/*------------*/
Class UploadFile
{
Var $ user_post_file = array ();
Var $ save_file_path = '';
Var $ max_file_size = '';
Var $ allow_type = array ('GIF', 'jpg ', 'PNG', 'Zip ', 'rar', 'txt ', 'Doc', 'PDF ');
Var $ final_file_path = '';
Var $ save_info = array ();
Var $ error_info = array ();

/**
* Constructor, used to initialize information.
*
* @ Param Array $ file
* @ Param String $ path
* @ Param Integer $ size
* @ Param Array $ type
*/
Function _ construct ($ file, $ path, $ size = 2097152, $ type = '')
{
$ This-> user_post_file = $ file;
$ This-> save_file_path = $ path;
$ This-> max_file_size = $ size;
If (! $ Type = ''){
$ This-> allow_type [] = $ type;
}
}

/**
*
*
* @ Access public
* @ Return int
*/
Function upload ()
{
For ($ I = 0; $ I <count ($ this-> user_post_file ['name']); $ I ++)
{
If ($ this-> user_post_file ['error'] [$ I] = 0) {// The status of the uploaded file is normal
// Obtain the current file name, temporary file name, size, type, and extension.
$ Name = $ this-> user_post_file ['name'] [$ I];
$ Tmp_name = $ this-> user_post_file ['tmp _ name'] [$ I];
$ Size = $ this-> user_post_file ['size'] [$ I];
$ Type = $ this-> user_post_file ['type'] [$ I];
$ Ext_name = $ this-> getExtName ($ name );

// File size
If (! $ This-> checkSize ($ size )){
$ This-> error_info [] = 'File you uploaded: '. $ name.' too large ';
Continue;
}
// Extension
If (! $ This-> checkType ($ ext_name )){
$ This-> error_info [] = 'File you uploaded: '. $ name.' invalid ';
Continue;
}
// Illegal Upload
If (! Is_uploaded_file ($ tmp_name )){
$ This-> error_info [] = 'File you uploaded: '. $ name.' illegal submission ';
Continue;
}

//
$ Basename = $ this-> getBaseName ($ name, ".". $ ext_name );
$ Final_filename = $ basename. '-'. time (). '-'. rand (1,10000). '.'. $ ext_name;
$ This-> final_file_path = $ this-> save_file_path. '/'. $ final_filename;

If (! Move_uploaded_file ($ tmp_name, $ this-> final_file_path )){
$ This-> error_info = $ this-> user_post_file ['error'] [$ I];
Continue;
}

//
$ This-> save_info [] = array (
"Name" => $ name,
"Ext_name" => $ ext_name,
"Type" => $ type,
"Size" => $ size,
"Final_filename" => $ final_filename,
"Path" => $ this-> final_file_path
);
}
}

Return count ($ this-> save_info );
}

/*
* Check whether the size of the uploaded file is valid.
 *
* @ Param Integer $ size
* @ Access private
* @ Return boolean
*/
Function checkSize ($ size)
{
If ($ size> $ this-> max_file_size ){
Return FALSE;
}

Return TRUE;
}

/*
* Check whether the object type uploaded by the user is valid.
 *
* @ Access private
* @ Return boolean
*/
Function checkType ($ extension)
{
Foreach ($ this-> allow_type as $ type ){
If (strcasecmp ($ extension, $ type) = 0 ){
Return TRUE;
}
}

Return FALSE;
}

/*
* Get the file extension
 *
* @ Param string $ filename
* @ Access private
* @ Return string
*/
Function getExtName ($ filename)
{
$ P = pathinfo ($ filename );

Return $ p ['extension'];
}

/*
* Get the file name (excluding the extension)
 *
* @ Param string $ filename
* @ Param string $ type
* @ Access private
* @ Return boolean
*/
Function getBaseName ($ filename, $ ext_name)
{
$ Basename = basename ($ filename, $ ext_name );

Return $ basename;
}

/*
 *
 *
 *
*/
Function showErrorInfo ()
{
If (count ($ this-> error_info )! = 0 ){
// Echo 'error... <br/> ';
Foreach ($ this-> error_info as $ k => $ v ){
Echo ($ k + 1), ':', $ v, '<br/> ';
}
}
}

Function getSaveInfo ()
{
Return $ this-> save_info;
}
}

// $ Upload = new UploadFile ('','');
// $ Upload = new UploadFile ();
// $ Upload-> showErrorInfo ();

?>

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.