Home >Backend Development >PHP Tutorial >PHP multiple input file file upload_PHP tutorial

PHP multiple input file file upload_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:02:291211browse

PHP multi-input file upload

Front-end html jquery code Back-end PHP processing

Front desk html



** Pay attention to how to write name, use name[] method, and use the same name
** Pay attention to the form with file. The form must add: enctype="multipart/form-data"

jquery submit form

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

PHP background processing

<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>

The PHP codes are very simple, so I won’t explain them one by one. Welcome to guide!

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/970256.htmlTechArticlePHP Multi-input file file upload front-end html jquery code background PHP processing front-end html ** Pay attention to the writing of name, both Use the name[] method, and use the same name ** Pay attention to the form with file...
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