파일 업로드: 파일 업로드: 파일 업로드: 파일 업로드:

 >  기사  >  백엔드 개발  >  Thinkphp3.2는 여러 파일을 업로드할 때 하나의 파일만 업로드하는 문제를 해결합니다.

Thinkphp3.2는 여러 파일을 업로드할 때 하나의 파일만 업로드하는 문제를 해결합니다.

一个新手
一个新手원래의
2017-10-03 05:58:141440검색

html 단순 페이지:

index.html 코드:


<form action="{:U(&#39;index/upload&#39;)}" 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>

Controller IndexCo ntroller.class.php 코드:


<?php
namespace Home\Controller;use Think\Controller;class IndexController extends Controller {    
    public function index(){        
    $this->display();
    }    
    public function upload(){        
        if(IS_POST){            
        $config = array(                
              &#39;maxSize&#39;    =>    3145728,
                &#39;rootPath&#39;   =>    &#39;./Uploads/&#39;,
                &#39;savePath&#39;   =>    &#39;&#39;,
                &#39;saveName&#39;   =>    array(&#39;uniqid&#39;, mt_rand(1,999999).&#39;_&#39;.md5(uniqid())),
                &#39;exts&#39;       =>    array(&#39;jpg&#39;, &#39;gif&#39;, &#39;png&#39;, &#39;jpeg&#39;),
                &#39;autoSub&#39;    =>    true,
                &#39;subName&#39;    =>    array(&#39;date&#39;,&#39;Ymd&#39;),
            );            
            $upload = new \Think\Upload($config);// 实例化上传类
                  $info   =   $upload->upload();            
                  if(!$info) {                
                  $this->error($upload->getError());
            }else{                
                foreach($info as $file){                    
                echo $file[&#39;savepath&#39;].$file[&#39;savename&#39;];
                }
            }
        }else{            
            $this->display();
        }
    }
}

업로드 결과 표시:

여기에는 많은 사람들이 있습니다 여러 파일을 업로드하다가 결국 한 개만 업로드된 것을 발견했습니다. 이름이 동일해서 결국 사진이 한 장 남았습니다.

$config = array(                
                &#39;maxSize&#39;    =>    3145728,
                &#39;rootPath&#39;   =>    &#39;./Uploads/&#39;,
                &#39;exts&#39;       =>    array(&#39;jpg&#39;, &#39;gif&#39;, &#39;png&#39;, &#39;jpeg&#39;),
                &#39;autoSub&#39;    =>    true,
                &#39;subName&#39;    =>    array(&#39;date&#39;,&#39;Ymd&#39;),
                &#39;saveRule&#39;   => &#39;&#39;,
            );

비워두세요 $ 구성의 saveRule, 업로드 후 이름은 다음과 같습니다: 59c8d38cdb968.jpg


이 이름 지정이 신뢰할 수 없다고 생각되면 두 번째 방법을 사용할 수 있습니다:

$config = array(                
                &#39;maxSize&#39;    =>    3145728,
                &#39;rootPath&#39;   =>    &#39;./Uploads/&#39;,
                &#39;saveName&#39;   =>    array(&#39;uniqid&#39;, mt_rand(1,999999).&#39;_&#39;.md5(uniqid())),
                &#39;exts&#39;       =>    array(&#39;jpg&#39;, &#39;gif&#39;, &#39;png&#39;, &#39;jpeg&#39;),
                &#39;autoSub&#39;    =>    true,
                &#39;subName&#39;    =>    array(&#39;date&#39;,&#39;Ymd&#39;),
            );

$에 설정 config: 'saveName' => ; array('uniqid', mt_rand(1,999999).'_'.md5(uniqid())),

최종 결과는 다음과 유사합니다: 672563_30ad4d8a2aafc832363de8edc1940b5c59c8d44a303f9.jpg


중 물론 이름은 필요에 따라 수정할 수 있습니다. 여러 파일을 업로드하는 방법은 다양합니다. 여기서는 간단하고 편리한 방법을 소개합니다.

위 내용은 Thinkphp3.2는 여러 파일을 업로드할 때 하나의 파일만 업로드하는 문제를 해결합니다.의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.