Thinkphp3.2에서 여러 파일을 업로드할 때 하나의 파일만 업로드하는 문제 해결

黄舟
풀어 주다: 2023-03-16 11:26:01
원래의
1330명이 탐색했습니다.

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 중국어 웹사이트의 기타 관련 기사를 참조하세요!

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!