>  기사  >  백엔드 개발  >  Thinkphp3.2가 여러 파일을 업로드하고 하나의 파일만 업로드하는 문제를 단순히 해결한 문제에 대한 분석

Thinkphp3.2가 여러 파일을 업로드하고 하나의 파일만 업로드하는 문제를 단순히 해결한 문제에 대한 분석

不言
不言원래의
2018-06-08 13:42:191795검색

다음 글에서는 여러 파일을 업로드할 때 파일 하나만 업로드하는 문제를 Thinkphp3.2를 사용하여 간단하게 해결하는 글을 소개합니다. 내용이 꽤 괜찮아서 지금 공유해서 참고용으로 올려드리겠습니다.

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 IndexController.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;,
   );

$config의 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())),

최종 결과는 비슷합니다. to: 672563_30ad4d8a2aafc832363de8edc1940b5c59c8d44 a303f9.jpg


물론 여러 파일을 업로드하는 방법에는 여러 가지가 있지만 여기에는 간단하고 편리한 방법이 있습니다!

위 내용은 이 글의 전체 내용입니다. 모든 분들의 학습에 도움이 되었으면 좋겠습니다. 더 많은 관련 내용은 PHP 중국어 홈페이지를 주목해주세요!

관련 권장 사항:

thinkphp5에서 이미지를 업로드하고 썸네일을 생성하는 방법

ThinkPHP3.2.3 인증 코드 표시, 새로 고침 및 검증

THinkPHP에서 클라이언트 IP 및 IP 주소 쿼리를 얻는 방법

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

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