Home  >  Article  >  php教程  >  PHP 多input file文件上传

PHP 多input file文件上传

WBOY
WBOYOriginal
2016-06-13 09:11:06850browse

PHP 多input file文件上传

前台html jquery代码 后台PHP处理

前台html



** 注意name的写法,都要使用name[]方式,并且用同一个name
** 注意带有file的表单,form一定要加:enctype=”multipart/form-data”

jquery 提交form表单

<code class="hljs" javascript="">$(#form).form(&#39;submit&#39;,{
  url:url,
  success:function(data){
    //处理返回数据
  }
});</code>

PHP 后台处理

<code class="hljs" php="">//接收处理文件
$fileArray = $_FILES[&#39;imagesUpload&#39;];//根据请求的name获取文件
$upload_dir = public_path() . /upload/carPic/;
$userID = Session::get(&#39;userID&#39;);
$nowTime = date(YmdHis, time());
$i = 0;
$successName = array();
foreach ($fileArray[&#39;error&#39;] as $key => $error){  //遍历处理文件
  if ( $error == UPLOAD_ERR_OK ) {
    $temp_name = $fileArray[&#39;tmp_name&#39;][$key];
    $file_name = $userID.&#39;-&#39;.$nowTime.$i.$fileArray[&#39;name&#39;][$key];
    move_uploaded_file($temp_name, $upload_dir.$file_name);
    array_push($successName, $file_name);//把上传成功的文件名称加入数组
  }else{
    return &#39;{flag:0,flagmsg:上传[文件&#39;.$key.&#39;]失败!
!}&#39;;
  }
  $i++;
}
$flag = array(&#39;flag&#39;=>1,&#39;flagmsg&#39;=>&#39;文件上传成功!&#39;);
$names = array(&#39;names&#39;=>$successName);
return json_encode(
  array_merge($flag,$names)
);//返回上传结果,并返回上传成功后的所有文件的名称</code>

PHP代码都很简单,就不一一解释了。欢迎指导!
 

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn