php multi-file upload code to achieve php multi-file upload function

Source: Internet
Author: User
Keywords Network programming PHP tutorial
Tags array class code echo error examples file file type

php tutorial multi-file upload code to achieve multi-file upload php This article uses the php multi-file upload class to achieve, and examples of multiple instances of php upload files Oh, multi-file upload is the most important attribute on the file must be in the form of an array Use foreach or for also read one by one Move_uploaded_file file upload to the server This multi-file upload Oh.
* /
?>

<! doctype html public "- // w3c // dtd xhtml 1.0 transitional // en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns = "http://www.jzread.com/1999/xhtml">
<head>
<meta http-equiv = "content-type" content = "text / html; charset = gb2312" />
<title> php multi-file upload code </ title>
</ head>

<body>
<form method = "post" enctype = "multipart / form-data" action = "server.php">
<input type = "file" name = "spec []">
<input type = "file" name = "spec []">


<! - <input type = "file" name = "spec">
<input type = "file" name = "manual"> ->
<input type = "submit">
</ form>
</ body>
</ html>

server.php

<? php
// upload array files
include 'upload.class.php';
$ u = new upload ('../ uploads / product /', 'spec', 'group');
print_r ($ u-> getnewname ());
echo $ u-> geterror ();
/ ***********************
// upload single file
$ u = new upload ('../ www.jzread.com/product/','spec');
print_r ($ u-> getnewname ());
$ u = new upload ('../ mb.jzread.com/product/','manual');
print_r ($ u-> getnewname ());
echo $ u-> geterror ();
************************ /
?>
upload.class.php

<? php
class upload {
private $ savedir = "./";
private $ field = 'new';
private $ allowed = array ('. jpg', '. jpeg', '. gif', '. png', '. bmp', '. rar', '. zip', '. pdf', '. swf' );
private $ max = 2097152; / / unit (bytes)
public $ error = '';
public $ newname = array ();
function __construct ($ dir, $ fieldname, $ type = 'single') {
// var_dump ($ _ files);
if (! empty ($ fieldname)) {$ this-> field = $ fieldname;}
// $ c = count ($ _ files [$ this-> field] ['tmp_name']);
// echo $ c;
if (! empty ($ dir)) {$ this-> savedir = $ dir;}

if ($ type! = 'single') {$ this-> uploadbygroup ();
} else {
$ this-> uploadbysingle ();
}
// print_r ($ array);

}
private function uploadbygroup () {
$ array = array ();
foreach ($ _ files [$ this-> field] ['tmp_name'] as $ k => $ v) {
$ file_ext = strtolower (strrchr ($ _files [$ this-> field] ["name"] [$ k], "."));
$ file_size = $ _files [$ this-> field] ['size'] [$ k];
if ($ this-> isallowedtype ($ file_ext) && $ this-> issizeallowed ($ file_size)) {
$ file_name = time (). $ this-> field. $ k;
move_uploaded_file ($ _ files [$ this-> field] ["tmp_name"] [$ k], $ this-> savedir. $ file_name. $ file_ext);
$ array [] = $ file_name. $ file_ext;
} else {
$ this-> error = 'file type not allowed or file is too big!';
return false;
}
}
$ this-> newname = $ array;
}
private function uploadbysingle () {
// var_dump ($ _ files);
$ array = array ();

$ file_ext = strtolower (strrchr ($ _files [$ this-> field] ["name"], "."));
$ file_size = $ _files [$ this-> field] ['size'];
if ($ this-> isallowedtype ($ file_ext) && $ this-> issizeallowed ($ file_size)) {
// $ fileprename = substr ($ _ files [$ this-> field] ['name'] [$ k], 0, -strlen ($ file_ext)); // For example,
// $ file_name = time (). $ fileprename;
$ file_name = time (). $ this-> field;
move_uploaded_file ($ _ files [$ this-> field] ["tmp_name"], $ this-> savedir. $ file_name. $ file_ext);
$ array [] = $ file_name. $ file_ext;
} else {
$ this-> error = 'file type not allowed or file is too big!';
return false;
}

$ this-> newname = $ array;
// print_r ($ this-> newname);
}

function getnewname () {
return $ this-> newname;
}
function isallowedtype ($ type) {
return in_array ($ type, $ this-> allowed)? true: false;
}
function issizeallowed ($ size) {
return $ size <$ this-> max? true: false;
}
function geterror () {
return $ this-> error;
}
}
?>

Related Article

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.