亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用多個輸入字段上傳多個文件

使用多個輸入字段上傳多個文件

PHP
呼喚遠方 2023-08-19 17:46:14
我認為有幾個輸入字段是這樣的<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/>';只能獲取一個文件路徑。如何獲取文件路徑并重命名上傳的文件?
查看完整描述

2 回答

?
慕虎7371278

TA貢獻1802條經驗 獲得超4個贊

使用 $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'];


查看完整回答
反對 回復 2023-08-19
?
忽然笑

TA貢獻1806條經驗 獲得超5個贊

事實證明它并不是那么簡單。下面的代碼:

  • 僅上傳圖像文件和 pdf,僅允許上傳圖像和 pdf

  • 重命名上傳的文件

  • 刪除上傳文件中的所有空格

public function normal_upload(){

    $uploadLocation = "uploads/";

    $uploadMainTo = null;

    if(isset($_FILES['MainImage'])){

        

    $path = $_FILES['MainImage']['name']; // file means your input type file name

    $ext = pathinfo($path, PATHINFO_EXTENSION);

    

    if ($ext=="jpg" OR $ext=="jpeg" OR $ext=="gif" OR $ext=="png" OR $ext=="pdf") {

        echo "Upload successful";

    }else{

        // your invalid code here like...

        echo "Invalid image format. Only upload JPG or JPEG or GIF or PNG";

        header('Location: http://localhost/upload_app/info/nu');

    exit;

    }

    

    

      $main_image_name = $_FILES['MainImage']['name'];

      $main_image_size = $_FILES['MainImage']['size'];

      $main_image_tmp = $_FILES['MainImage']['tmp_name'];

      $uploadMainTo = $uploadLocation.sa(time().mt_rand(43,672882).bin2hex(random_bytes(16)).trim($main_image_name));

      $moveMain = move_uploaded_file($main_image_tmp,$uploadMainTo);

      

      

      echo $uploadMainTo.'<br/>';

    }

    

    $uploadSecondTo = null;

    if(isset($_FILES['SecondImage'])){

        

    $path = $_FILES['SecondImage']['name']; // file means your input type file name

    $ext = pathinfo($path, PATHINFO_EXTENSION);

    

    if ($ext=="jpg" OR $ext=="jpeg" OR $ext=="gif" OR $ext=="png" OR $ext=="pdf") {

        // your code here like...

        echo "Upload successful";

    }else{

        // your invalid code here like...

        echo "Invalid image format. Only upload JPG or JPEG or GIF or PNG";

        header('Location: http://localhost/upload_app/info/nu');

    exit;

    }

    

    

      $second_image_name = $_FILES['SecondImage']['name'];

      $second_image_size = $_FILES['SecondImage']['size'];

      $second_image_tmp = $_FILES['SecondImage']['tmp_name'];

      $uploadMainTo = $uploadLocation.sa(time().mt_rand(43,672882).bin2hex(random_bytes(16)).trim($main_image_name));

      $moveSecond = move_uploaded_file($second_image_tmp,$uploadSecondTo);

      echo $uploadSecondTo.'<br/>';

    }

    

    $uploadPdfTo = null;

    if(isset($_FILES['PDF'])){

    

    $path = $_FILES['PDF']['name']; // file means your input type file name

    $ext = pathinfo($path, PATHINFO_EXTENSION);

    

    if ($ext=="jpg" OR $ext=="jpeg" OR $ext=="gif" OR $ext=="png" OR $ext=="pdf") {

        // your code here like...

        echo "Upload successful";

    }else{

        // your invalid code here like...

        echo "Invalid image format. Only upload JPG or JPEG or GIF or PNG";

        header('Location: http://localhost/upload_app/info/nu');

    exit;

    }

    

    

      $pdf_name = $_FILES['PDF']['name'];

      $pdf_size = $_FILES['PDF']['size'];

      $pdf_tmp = $_FILES['PDF']['tmp_name'];

      $uploadMainTo = $uploadLocation.sa(time().mt_rand(43,672882).bin2hex(random_bytes(16)).trim($main_image_name));

      $movepdf = move_uploaded_file($pdf_tmp,$uploadPdfTo);

      echo $uploadPdfTo.'<br/>';

    }

    

    }

定義一個助手sa_helper.php


<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');


if ( ! function_exists('sa')){

function sa($string){

    return str_replace(' ', '', $string);

}

   }

?>

這是你的觀點


<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">

    $(document).ready(function() {

        $("#form_img").submit(function(e){

            e.preventDefault();

            var formData = new FormData($("#form_img")[0]);


            $.ajax({

                url : $("#form_img").attr('action'),

                type : 'POST',

                data : formData,

                contentType : false,

                processData : false,

                success: function(resp) {

                    console.log(resp);                        

                }

            });

        });

    });


</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">

  <button type="submit">Submit </button>

</form>


查看完整回答
反對 回復 2023-08-19
  • 2 回答
  • 0 關注
  • 178 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號