第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

在 Codeigniter 3 中上傳時如何顯示不允許的文件類型錯誤消息?

在 Codeigniter 3 中上傳時如何顯示不允許的文件類型錯誤消息?

PHP
拉丁的傳說 2022-07-16 10:07:53
我正在使用Codeigniter 3.1.8和Bootstrap 4開發(fā)一個基本的博客應(yīng)用程序。當(dāng)然,這些帖子有主要圖像。如果用戶沒有上傳圖像,則有默認(rèn)圖像,但如果上傳圖像,則只允許 3 種類型:jpg、jpeg 和 png。想要警告用戶以防她/他嘗試上傳其他文件格式,我在Posts控制器中執(zhí)行了此操作:// Upload image$config['upload_path'] = './assets/img/posts';$config['allowed_types'] = 'jpg|jpeg|png';$config['max_size'] = '2048';$this->load->library('upload', $config);if(!$this->upload->do_upload()){    $data['uerrors'] = $this->upload->display_errors();    if ($data['uerrors']) {        $this->load->view('partials/header', $data);        $this->load->view('dashboard/create-post');        $this->load->view('partials/footer');    } else {        $post_image = 'default.jpg';    }} else {    $data = array('upload_data' => $this->upload->data());    $post_image = $_FILES['userfile']['name'];}在我看來:<?php foreach ($uerrors as $uerror): ?>  <span><?php echo $uerror; ?></span><?php endforeach; ?>然而,我得到一個未定義的變量:uerrors錯誤。這是整個create()方法:public function create() {    // Only logged in users can create posts    if (!$this->session->userdata('is_logged_in')) {        redirect('login');    }    $data = $this->get_data();    $data['tagline'] = "Add New Post";    if ($data['categories']) {        foreach ($data['categories'] as &$category) {            $category->posts_count = $this->Posts_model->count_posts_in_category($category->id);        }    }    $this->form_validation->set_rules('title', 'Title', 'required');    $this->form_validation->set_rules('desc', 'Short description', 'required');    $this->form_validation->set_rules('body', 'Body', 'required');    $this->form_validation->set_error_delimiters('<p class="error-message">', '</p>');    if($this->form_validation->run() === FALSE){        $this->load->view('partials/header', $data);        $this->load->view('dashboard/create-post');        $this->load->view('partials/footer');    }     我的錯誤在哪里?
查看完整描述

3 回答

?
慕桂英4014372

TA貢獻(xiàn)1871條經(jīng)驗 獲得超13個贊

通過修改這種方式,我已成功顯示上傳錯誤(如果嘗試上傳,否則使用默認(rèn)圖像):create()


public function create() {


    // Only logged in users can create posts

    if (!$this->session->userdata('is_logged_in')) {

        redirect('login');

    }


    $data = $this->get_data();

    $data['tagline'] = "Add New Post";


    if ($data['categories']) {

        foreach ($data['categories'] as &$category) {

            $category->posts_count = $this->Posts_model->count_posts_in_category($category->id);

        }

    }


    $this->form_validation->set_rules('title', 'Title', 'required');

    $this->form_validation->set_rules('desc', 'Short description', 'required');

    $this->form_validation->set_rules('body', 'Body', 'required');

    $this->form_validation->set_error_delimiters('<p class="error-message">', '</p>');


    if($this->form_validation->run() === FALSE){

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

        $this->load->view('dashboard/create-post');

        $this->load->view('partials/footer');

    } else {

        // Create slug (from title)

        $slug = url_title(convert_accented_characters($this->input->post('title')), 'dash', TRUE);

        $slugcount = $this->Posts_model->slug_count($slug, null);

        if ($slugcount > 0) {

            $slug = $slug."-".$slugcount;

        }


        // Upload image

        $config['upload_path'] = './assets/img/posts';

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

        $config['max_size'] = '2048';


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


        if(!$this->upload->do_upload()){


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


            // Display upload validation errors 

            // only if a file is uploaded and there are errors

            if (empty($_FILES['userfile']['name'])) {

                $errors = [];

            }


            if (empty($errors)) {

                $post_image = 'default.jpg';

            } else {

                $data['upload_errors'] = $errors;

            }


        } else {

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

            $post_image = $_FILES['userfile']['name'];

        }


        if (empty($errors)) {

            $this->Posts_model->create_post($post_image, $slug);

            $this->session->set_flashdata('post_created', 'Your post has been created');

            redirect('/');

        } else {

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

            $this->load->view('dashboard/create-post');

            $this->load->view('partials/footer');

        }

    }

}

在create-post.php我看來:


<?php if(isset($upload_errors)){

   foreach ($upload_errors as $upload_error) {

    echo $upload_error;

   }

 }?>


查看完整回答
反對 回復(fù) 2022-07-16
?
jeck貓

TA貢獻(xiàn)1909條經(jīng)驗 獲得超7個贊

我在這里撿到了三樣?xùn)|西


1) 如前所述,沒有將 $data 傳遞給正確的視圖


2) 在視圖上期望數(shù)組而不是字符串,即錯誤的數(shù)據(jù)類型


3) 最后函數(shù) do_upload() 需要參數(shù)字符串 $field。缺少這就是為什么您只有 no upload selected 錯誤。如果設(shè)置了這個參數(shù),codeigniter 真的會拋出錯誤的文件類型錯誤。我這樣做是為了測試


在我看來


<form action="http://localhost:8000/welcome/create" method="post" enctype="multipart/form-data">

          <input type="file" name="lname" ><br>

          <input type="submit" value="Submit">

        </form>

然后在我的控制器中


if(!$this->upload->do_upload("lname")){

上傳錯誤的文件類型以測試此錯誤。您可能需要額外的長度來檢測實際上傳文件的文件類型。


查看完整回答
反對 回復(fù) 2022-07-16
?
慕碼人2483693

TA貢獻(xiàn)1860條經(jīng)驗 獲得超9個贊

您的上傳代碼看起來不錯,但您需要更新以下這些更改。


將數(shù)據(jù)傳遞給您的'dashboard/create-post'視圖,就像您傳遞給您的'partials/header'視圖一樣。您的'dashboard/create-post'視圖沒有收到任何上傳錯誤消息,所以它說'Undefined variable: uerrors'. 所以,你的上傳代碼應(yīng)該是這樣的 -

if(!$this->upload->do_upload()){

    $data['uerrors'] = $this->upload->display_errors();

    if ($data['uerrors']) {

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

        $this->load->view('dashboard/create-post', $data);

        $this->load->view('partials/footer');

    } else {

        $post_image = 'default.jpg';

    }

} else {

    $post_image = $this->upload->data('file_name');

}

正如CodeIgniter 文檔所說,'display_errors()' 返回字符串,而不是數(shù)組,您不必遍歷錯誤。只是在你的'dashboard/create-post'觀點(diǎn)上呼應(yīng)它。

為了您的方便,請以不同的方法制作您的上傳任務(wù),以便您也可以在更新方法中重新使用它。例如——


private function uploadFile(){

    if ($_FILES['userfile']['name'] === '') {

        return array(

            'status' => TRUE,

            'message' => 'No file selected.',

            'file_name' => 'default.jpg'

        );

    }


    // Upload image

    $config['upload_path'] = './assets/img/posts';

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

    $config['max_size'] = '2048';


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


    if(!$this->upload->do_upload('userfile')){

        return array(

            'status' => FALSE,

            'message' => $this->upload->display_errors('<p class="text-danger ">', '</p>'),

            'file_name' => ''

        );

    }else{

        return array(

            'status' => TRUE,

            'message' => 'File uploaded successfully',

            'file_name' => $this->upload->data('file_name')

        );

    }

}

然后你的整個創(chuàng)建方法應(yīng)該是這樣的 -


public function create() {

    // Only logged in users can create posts

    if (!$this->session->userdata('is_logged_in')) {

        redirect('login');

    }


    $data = $this->get_data();

    $data['tagline'] = "Add New Post";


    if ($data['categories']) {

        foreach ($data['categories'] as &$category) {

            $category->posts_count = $this->Posts_model->count_posts_in_category($category->id);

        }

    }


    $this->form_validation->set_rules('title', 'Title', 'required');

    $this->form_validation->set_rules('desc', 'Short description', 'required');

    $this->form_validation->set_rules('body', 'Body', 'required');

    $this->form_validation->set_error_delimiters('<p class="error-message">', '</p>');


    if($this->form_validation->run() === FALSE){

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

        $this->load->view('dashboard/create-post');

        $this->load->view('partials/footer');

    } else {

        $upload = $this->uploadFile();


        if($upload['status'] === FALSE){

            $data['upload_error'] = $upload['message'];


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

            $this->load->view('dashboard/create-post', $data);

            $this->load->view('partials/footer');

        }else{

            // Create slug (from title)

            $slug = url_title(convert_accented_characters($this->input->post('title')), 'dash', TRUE);

            $slugcount = $this->Posts_model->slug_count($slug, null);

            if ($slugcount > 0) {

                $slug = $slug."-".$slugcount;

            }


            $this->Posts_model->create_post($upload['file_name'], $slug);

            $this->session->set_flashdata('post_created', 'Your post has been created');

            redirect('/');

        }

    }

}

最后在您的視圖文件中添加這行代碼'dashboard/create-post',就在文件輸入按鈕之后。


<?php if(isset($upload_error)) echo $upload_error; ?>

我認(rèn)為一切都應(yīng)該工作。


查看完整回答
反對 回復(fù) 2022-07-16
  • 3 回答
  • 0 關(guān)注
  • 136 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號