php批量上传五[带预览]
分类:威尼斯手机平台

$path=$_SERVER[DOCUMENT_ROOT];//服务器路线 $i = 0; foreach ($_FILES[pictures][error] as $key = $error) { if ($error == UPLOAD_ERR_OK) { $upload_file=$_FILES['pictures']['tmp_name'][$key];//文件被上传后在服务端积攒的一时文件名 $imgtype = strtolower(strrchr($_FILES['pictures']['name'][$key],.)); $name_1 = strtolower(substr($_FILES[pictures][name][$key],0,2)); $upload_file_name= lanmayi_.time().$name_1.$imgtype;//以字母及系统时间的秒来做文件名 $store_dir = $path./up_img/;// 上传文件的累积地点 $file_size_max = 100000000000;// 1M约束文件上传最大体积(bytes) $accept_overwrite = 0;//是或不是同意覆盖相仿文件 move_uploaded_file($upload_file,$store_dir.$upload_file_name); $new_img_name = $upload_file_name; $new_img_type = substr($imgtype,1,3); $new_img_path = $store_dir; $suo_name = Makesuo ($new_img_path,$new_img_name,$new_img_type); $sh_time = date('Y-m-d H:i:s'); $name = $_POST['name']; $pid = $id; $sql = insert into lanmayi_img_show(pid,sh_name,sh_url,sh_suo,sh_time) values (.$pid.,'.$name[$i].','.$upload_file_name.','.$suo_name.','.$sh_time.'); $res = mysqli_query($conn,$sql); $i++; }}//*批量上传甘休********************************************************?script language=javascriptalert(图片上传成功!State of Qatar/script?php}if($_POST['fanhui']){?script language=javascriptwindow.location='pro_list.php?id=?php echo $id?';/script?php}?

    真正补助单文件和多文本上传类代码,改善了$_FILES[$field]['name']中的$田野同志不可能用变量只可以和表单中的文件名name="userfile"一致的弱点$_FILES['userfile']['name'],这里<input type="file" name="userfile"> 中的文件名能够随便取。

//index.htm
1、单文件上传 <form method="post" action="./upload.php" name="frmUpload" enctype="multipart/form-data" >
<input type="file" name="userfile" style="WIDTH: 282px">
<input type="submit" align="center" name="upfiles" value="确定"></form>
2、多文件上传 <form method="post" action="./upload.php" name="frmUpload" enctype="multipart/form-data" >
<input type="file" name="userfile[]" style="WIDTH: 282px">
<input type="file" name="userfile[]" style="WIDTH: 282px">
<input type="file" name="userfile[]" style="WIDTH: 282px">
<input type="submit" align="center" name="upfiles" value="确定">

</form>

//upload.php
<?php

class File_upload{
public $upload_path='./upload/';//上传文件的门路
威尼斯手机平台,public $allow_type=array(卡塔尔;//允许上传的文件类型
public $max_size='20480';//允许的最大文件大小
public $overwrite=false;//是否设置成覆盖情势
public $renamed=false;//是不是直接使用上传文件的名目,依旧系统自动命名

/**
* 私有变量
*/
private $upload_file=array(卡塔尔;//保存上传成功文件的新闻
private $upload_file_num=0;//上传成功文件的多少
private $succ_upload_file=array(卡塔尔(قطر‎;//成功保存的公文音讯
/**
* 构造器
*
* @param string $upload_path
* @param string $allow_type
* @param string $max_size
*/
public function __construct($upload_path='./upload/',$allow_type='jpg|bmp|png|gif|jpeg',$max_size='204800')
{
$this->set_upload_path($upload_path);
$this->set_allow_type($allow_type);
$this->max_size=$max_size;
$this->get_upload_files();
}
/**
* 设置上传路径,并认清
*
* @param string $path
*/
public function set_upload_path($path)
{
if(file_exists($path)){
if(is_writeable($path)){
$this->upload_path=$path;
}else{
if(@chmod($path,'0666'))
$this->upload_path=$path;
}
}else{
if(@mkdir($path,'0666')){
$this->upload_path=$path;
}
}
}
//设置上传文件类型
public function set_allow_type($types){
$this->allow_type=explode("|",$types);
}
//上传文件
public function get_upload_files()
{
foreach ($_FILES AS $key=>$field)
{
$this->get_upload_files_detial($key);
}
}
//上传文件数量存放到数组中
public function get_upload_files_detial($field){
if(is_array($_FILES["$field"]['name']))
{
for($i=0;$i<count($_FILES[$field]['name']);$i++)
{
if(0==$_FILES[$field]['error'][$i])
{
$this->upload_file[$this->upload_file_num]['name']=$_FILES[$field]['name'][$i];
$this->upload_file[$this->upload_file_num]['type']=$_FILES[$field]['type'][$i];
$this->upload_file[$this->upload_file_num]['size']=$_FILES[$field]['size'][$i];
$this->upload_file[$this->upload_file_num]['tmp_name']=$_FILES[$field]['tmp_name'][$i];
$this->upload_file[$this->upload_file_num]['error']=$_FILES[$field]['error'][$i];
$this->upload_file_num++;
}
}
}
else {
if(0==$_FILES["$field"]['error'])
{
$this->upload_file[$this->upload_file_num]['name']=$_FILES["$field"]['name'];
$this->upload_file[$this->upload_file_num]['type']=$_FILES["$field"]['type'];
$this->upload_file[$this->upload_file_num]['size']=$_FILES["$field"]['size'];
$this->upload_file[$this->upload_file_num]['tmp_name']=$_FILES["$field"]['tmp_name'];
$this->upload_file[$this->upload_file_num]['error']=$_FILES["$field"]['error'];
$this->upload_file_num++;
}
}
}
/**
* 检查上传文件是构满足钦定条件
*
*/
public function check($i)
{
if(!empty($this->upload_file[$i]['name'])){
//检查文件大小
if($this->upload_file[$i]['size']>$this->max_size*1024)$this->upload_file[$i]['error']=2;
//设置私下认可服务端文件名
$this->upload_file[$i]['filename']=$this->upload_path.$this->upload_file[$i]['name'];
//获取文件路线音信
$file_info=pathinfo($this->upload_file[$i]['name']);
//获取文件扩展名
$file_ext=$file_info['extension'];
//检查文件类型
if(!in_array($file_ext,$this->allow_type))$this->upload_file[$i]['error']=5;
//须求重命名的
if($this->renamed){
list($usec, $sec) = explode(" ",microtime());
$this->upload_file[$i]['filename']=$sec.substr($usec,2).'.'.$file_ext;
unset($usec);
unset($sec);
}
//检查文件是或不是留存
if(file_exists($this->upload_file[$i]['filename'])){
if($this->overwrite){
@unlink($this->upload_file[$i]['filename']);
}else{
$j=0;
do{
$j++;
$temp_file=str_replace('.'.$file_ext,'('.$j.').'.$file_ext,$this->upload_file[$i]['filename']);
}while (file_exists($temp_file));
$this->upload_file[$i]['filename']=$temp_file;
unset($temp_file);
unset($j);
}
}
//检查停止
} else $this->upload_file[$i]['error']=6;
}
/**
* 上传文件
*
* @return true
*/
public function upload()
{
$upload_msg='';
for($i=0;$i<$this->upload_file_num;$i++)
{
if(!empty($this->upload_file[$i]['name']))
{
//检查文件
$this->check($i);
if (0==$this->upload_file[$i]['error'])
{
//上传文件
if([!@move_uploaded_file($this->upload_file[$i]['tmp_name'],$this->upload_file[$i]['filename'](mailto:!@move_uploaded_file($this-%3Eupload_file%5B$i%5D%5B)]))
{
$upload_msg.='上传文件'.$this->upload_file[$i]['name'].' 出错:'.$this->error($this->upload_file[$i]['error']).'!<br>';
}else
{
$this->succ_upload_file[]=$this->upload_file[$i]['filename'];
$upload_msg.='上传文件'.$this->upload_file[$i]['name'].' 成功了<br>';
}
}else $upload_msg.='上传文件'.$this->upload_file[$i]['name'].' 出错:'.$this->error($this->upload_file[$i]['error']).'!<br>';
}
}
echo $upload_msg;
}
//错误新闻
public function error($error)
{
switch ($error) {
case 1:
return '文件大小超越php.ini 中 upload_max_filesize 选项约束的值';
break;
case 2:
return '文件的轻重超越了 HTML 表单中 MAX_FILE_SIZE 选项钦赐的值';
break;
case 3:
return '文件唯有局地被上传';
break;
case 4:
return '未有公文被上传';
break;
case 5:
return '这么些文件分化意被上传';
break;
case 6:
return '文件名称叫空';
break;
default:
return '出错';
break;
}
}
//获取成功的多寡新闻为数组(备用State of Qatar
public function get_succ_file(){
return $this->succ_upload_file;
}
}
$upload=new File_upload('./upload/','jpg|bmp|png|gif|jpeg');
$upload->upload();
$t=$upload->get_succ_file();
print_r($t);

?>

本文由威尼斯在线注册平台发布于威尼斯手机平台,转载请注明出处:php批量上传五[带预览]

上一篇:没有了 下一篇:没有了
猜你喜欢
热门排行
精彩图文