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;
}
}
?>