源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

Thinkphp3.2简单解决多文件上传只上传一张的问题

  • 时间:2022-04-24 11:12 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:Thinkphp3.2简单解决多文件上传只上传一张的问题
[b]html简单页面:[/b] [img]http://files.jb51.net/file_images/article/201709/2017092609033424.png[/img] [b]index.html代码:[/b]
<form action="{:U('index/upload')}" method="post" enctype="multipart/form-data">
 文件上传:<input type="file" name = "test[]">
 文件上传:<input type="file" name = "test[]">
 文件上传:<input type="file" name = "test[]">
 文件上传:<input type="file" name = "test[]">
 文件上传:<input type="file" name = "test[]">
 文件上传:<input type="file" name = "test[]">
 文件上传:<input type="file" name = "test[]">
 文件上传:<input type="file" name = "test[]">
 文件上传:<input type="file" name = "test[]">
 文件上传:<input type="file" name = "test[]">
 文件上传:<input type="file" name = "test[]">
 文件上传:<input type="file" name = "test[]">
 <input type="submit" value = "提交">
</form>
[b]控制器IndexController.class.php代码:[/b]
<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
 public function index(){
  $this->display();
 }
 public function upload(){
  if(IS_POST){
   $config = array(
    'maxSize' => 3145728,
    'rootPath' => './Uploads/',
    'savePath' => '',
    'saveName' => array('uniqid', mt_rand(1,999999).'_'.md5(uniqid())),
    'exts'  => array('jpg', 'gif', 'png', 'jpeg'),
    'autoSub' => true,
    'subName' => array('date','Ymd'),
   );
   $upload = new \Think\Upload($config);// 实例化上传类
   $info = $upload->upload();
   if(!$info) {
    $this->error($upload->getError());
   }else{
    foreach($info as $file){
     echo $file['savepath'].$file['savename'];
    }
   }
  }else{
   $this->display();
  }
 }
}
[b]上传结果显示:[/b] [img]http://files.jb51.net/file_images/article/201709/2017092609033425.png[/img] [img]http://files.jb51.net/file_images/article/201709/2017092609033426.png[/img] 好多人在进行多文件上传的时候,最后发现只是上传了一张,主要就是命名所致,因为是同样的名字,所以最后就剩一张图片 解决方法:[b]第一种:[/b]
$config = array(
    'maxSize' => 3145728,
    'rootPath' => './Uploads/',
    'exts'  => array('jpg', 'gif', 'png', 'jpeg'),
    'autoSub' => true,
    'subName' => array('date','Ymd'),
    'saveRule' => '',
   );
置空$config里面的saveRule,上传后的名称为:59c8d38cdb968.jpg [img]http://files.jb51.net/file_images/article/201709/2017092609033427.png[/img] 若是感觉这种命名不可靠,[b]可采取第二种方法:[/b]
$config = array(
    'maxSize' => 3145728,
    'rootPath' => './Uploads/',
    'saveName' => array('uniqid', mt_rand(1,999999).'_'.md5(uniqid())),
    'exts'  => array('jpg', 'gif', 'png', 'jpeg'),
    'autoSub' => true,
    'subName' => array('date','Ymd'),
   );
设置$config中: 'saveName' => array('uniqid', mt_rand(1,999999).'_'.md5(uniqid())), 其最后的结果类似于:672563_30ad4d8a2aafc832363de8edc1940b5c59c8d44a303f9.jpg [img]http://files.jb51.net/file_images/article/201709/2017092609033428.png[/img] 然,命名可根据需要自行修改,多文件上传方法很多,这里只是提供个简单便捷的方法! 以上这篇Thinkphp3.2简单解决多文件上传只上传一张的问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持编程素材网。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部