使用多个输入字段上传多个文件

我认为有几个输入字段是这样的


<head>

<title>Upload Form</title>

</head>

<body>

<?php echo form_open_multipart('info/normal_upload');?>


<input type="file" name="one" size="20" />

<input type="file" name="two" size="30" />

<input type="file" name="three" size="40" />


<br /><br />

<input type="submit" value="upload" />


</form>


</body>

</html>

我正在像这样上传文件


        $config['upload_path'] = realpath(FCPATH.'uploads');

        $config['allowed_types']= "gif|jpg|png|jpeg|pdf";

        $config['overwrite'] = TRUE;

        $config['max_size'] = "2048000"; 

        $config['max_height'] = "5000";

        $config['max_width'] = "5000";

        $this->load->library('upload', $config);

        


        if ( ! $this->upload->do_upload('one'))

        {

                $error = array('error' => $this->upload->display_errors());


                $this->load->view('normal_upload', $error);

        }

         if ( ! $this->upload->do_upload('two'))

        {

                $error = array('error' => $this->upload->display_errors());


                //$this->load->view('normal_upload', $error);

        }

         if ( ! $this->upload->do_upload('three'))

        {

                $error = array('error' => $this->upload->display_errors());


                //$this->load->view('normal_upload', $error);

        }

        else

        {

          $data = array('upload_data' => $this->upload->data());

          echo $data['upload_data']['full_path'].'<br/>';


          //$this->load->view('normal_upload', $data);

                

        }

我想获取所有上传文件的文件名、路径。如果我在所有表单字段中选择文件,文件将被上传,但是:


echo $data['upload_data']['full_path'].'<br/>';

只能获取一个文件路径。如何获取文件路径并重命名上传的文件?


呼唤远方
浏览 136回答 2
2回答

慕虎7371278

使用 $this->load->library('upload', $config); 加载库;当您只上传一个文件时有效。但是,当您上传多个文件时,您需要使用配置重新初始化库。就像这样:$this->load->library('upload');$this->upload->initialize($config);if ( ! $this->upload->do_upload('one')){        $error = array('error' => $this->upload->display_errors());        $this->load->view('normal_upload', $error);}$data['one'] = array('upload_data' => $this->upload->data());$this->upload->initialize($config); if ( ! $this->upload->do_upload('two')){        $error = array('error' => $this->upload->display_errors());        //$this->load->view('normal_upload', $error);} $data['two'] = array('upload_data' => $this->upload->data()); $this->upload->initialize($config); if ( ! $this->upload->do_upload('three')){        $error = array('error' => $this->upload->display_errors());        //$this->load->view('normal_upload', $error);}$data['three'] = array('upload_data' => $this->upload->data());然后您可以使用以下命令访问文件数据:$data['one']['upload_data']['full_path'];$data['two']['upload_data']['full_path'];$data['three']['upload_data']['full_path'];

忽然笑

事实证明它并不是那么简单。下面的代码:仅上传图像文件和 pdf,仅允许上传图像和 pdf重命名上传的文件删除上传文件中的所有空格public function normal_upload(){&nbsp; &nbsp; $uploadLocation = "uploads/";&nbsp; &nbsp; $uploadMainTo = null;&nbsp; &nbsp; if(isset($_FILES['MainImage'])){&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; $path = $_FILES['MainImage']['name']; // file means your input type file name&nbsp; &nbsp; $ext = pathinfo($path, PATHINFO_EXTENSION);&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; if ($ext=="jpg" OR $ext=="jpeg" OR $ext=="gif" OR $ext=="png" OR $ext=="pdf") {&nbsp; &nbsp; &nbsp; &nbsp; echo "Upload successful";&nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; // your invalid code here like...&nbsp; &nbsp; &nbsp; &nbsp; echo "Invalid image format. Only upload JPG or JPEG or GIF or PNG";&nbsp; &nbsp; &nbsp; &nbsp; header('Location: http://localhost/upload_app/info/nu');&nbsp; &nbsp; exit;&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; $main_image_name = $_FILES['MainImage']['name'];&nbsp; &nbsp; &nbsp; $main_image_size = $_FILES['MainImage']['size'];&nbsp; &nbsp; &nbsp; $main_image_tmp = $_FILES['MainImage']['tmp_name'];&nbsp; &nbsp; &nbsp; $uploadMainTo = $uploadLocation.sa(time().mt_rand(43,672882).bin2hex(random_bytes(16)).trim($main_image_name));&nbsp; &nbsp; &nbsp; $moveMain = move_uploaded_file($main_image_tmp,$uploadMainTo);&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; echo $uploadMainTo.'<br/>';&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; $uploadSecondTo = null;&nbsp; &nbsp; if(isset($_FILES['SecondImage'])){&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; $path = $_FILES['SecondImage']['name']; // file means your input type file name&nbsp; &nbsp; $ext = pathinfo($path, PATHINFO_EXTENSION);&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; if ($ext=="jpg" OR $ext=="jpeg" OR $ext=="gif" OR $ext=="png" OR $ext=="pdf") {&nbsp; &nbsp; &nbsp; &nbsp; // your code here like...&nbsp; &nbsp; &nbsp; &nbsp; echo "Upload successful";&nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; // your invalid code here like...&nbsp; &nbsp; &nbsp; &nbsp; echo "Invalid image format. Only upload JPG or JPEG or GIF or PNG";&nbsp; &nbsp; &nbsp; &nbsp; header('Location: http://localhost/upload_app/info/nu');&nbsp; &nbsp; exit;&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; $second_image_name = $_FILES['SecondImage']['name'];&nbsp; &nbsp; &nbsp; $second_image_size = $_FILES['SecondImage']['size'];&nbsp; &nbsp; &nbsp; $second_image_tmp = $_FILES['SecondImage']['tmp_name'];&nbsp; &nbsp; &nbsp; $uploadMainTo = $uploadLocation.sa(time().mt_rand(43,672882).bin2hex(random_bytes(16)).trim($main_image_name));&nbsp; &nbsp; &nbsp; $moveSecond = move_uploaded_file($second_image_tmp,$uploadSecondTo);&nbsp; &nbsp; &nbsp; echo $uploadSecondTo.'<br/>';&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; $uploadPdfTo = null;&nbsp; &nbsp; if(isset($_FILES['PDF'])){&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; $path = $_FILES['PDF']['name']; // file means your input type file name&nbsp; &nbsp; $ext = pathinfo($path, PATHINFO_EXTENSION);&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; if ($ext=="jpg" OR $ext=="jpeg" OR $ext=="gif" OR $ext=="png" OR $ext=="pdf") {&nbsp; &nbsp; &nbsp; &nbsp; // your code here like...&nbsp; &nbsp; &nbsp; &nbsp; echo "Upload successful";&nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; // your invalid code here like...&nbsp; &nbsp; &nbsp; &nbsp; echo "Invalid image format. Only upload JPG or JPEG or GIF or PNG";&nbsp; &nbsp; &nbsp; &nbsp; header('Location: http://localhost/upload_app/info/nu');&nbsp; &nbsp; exit;&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; $pdf_name = $_FILES['PDF']['name'];&nbsp; &nbsp; &nbsp; $pdf_size = $_FILES['PDF']['size'];&nbsp; &nbsp; &nbsp; $pdf_tmp = $_FILES['PDF']['tmp_name'];&nbsp; &nbsp; &nbsp; $uploadMainTo = $uploadLocation.sa(time().mt_rand(43,672882).bin2hex(random_bytes(16)).trim($main_image_name));&nbsp; &nbsp; &nbsp; $movepdf = move_uploaded_file($pdf_tmp,$uploadPdfTo);&nbsp; &nbsp; &nbsp; echo $uploadPdfTo.'<br/>';&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; }定义一个助手sa_helper.php<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');if ( ! function_exists('sa')){function sa($string){&nbsp; &nbsp; return str_replace(' ', '', $string);}&nbsp; &nbsp;}?>这是你的观点<head><title>Upload Form</title></head><body><?php echo form_open_multipart('info/normal_upload');?><input type="file" name="PDF"><input type="file" name="MainImage"><input type="file" name="SecondImage"><br /><br /><input type="submit" value="upload" /></form></body></html>要使用 jquery 上传文件,请使用此表单,它也有效<script type="text/javascript" src="https://code.jquery.com/jquery-3.2.1.min.js"></script><script type="text/javascript">&nbsp; &nbsp; $(document).ready(function() {&nbsp; &nbsp; &nbsp; &nbsp; $("#form_img").submit(function(e){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.preventDefault();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var formData = new FormData($("#form_img")[0]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $.ajax({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; url : $("#form_img").attr('action'),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; type : 'POST',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data : formData,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; contentType : false,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; processData : false,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; success: function(resp) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(resp);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; });</script><form action="http://localhost/upload_app/info/normal_upload" id="form_img" method="GET" role="form" enctype="multipart/form-data"><input type="file" name="PDF"><input type="file" name="MainImage"><input type="file" name="SecondImage">&nbsp; <button type="submit">Submit </button></form>
打开App,查看更多内容
随时随地看视频慕课网APP