【问题标题】:Make image and video upload optional使图像和视频上传可选
【发布时间】:2012-09-07 09:46:21
【问题描述】:

我在 codeigniter 中有一个小应用程序。有一个包含 3 个文本字段的表单,名称、电子邮件、位置。这足以提交。但是,有两个可选字段可供所有用户提交图像或视频或两者。我让它工作,直到上传的文件出现错误。当我希望它停止整个提交直到错误被修复时,它仍然会提交文本数据。

这是来自我的控制器的代码。任何帮助都会很棒。谢谢!

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

class Form extends MY_Controller {

public function __construct()
{
    parent::__construct();
    $this->load->helper(array('form', 'url'));
}

function index()
    {
        $this->load->helper(array('form', 'url'));

        $this->load->library('form_validation');

        $this->form_validation->set_rules('nominee', 'Nominee', 'required');
        $this->form_validation->set_rules('email', 'Email', 'required|valid_email');
        $this->form_validation->set_rules('location', 'Location', 'required');

        if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('form');
        }
        else
        {
             // Load the library - no config specified here
                $this->load->library('upload');

                // Check if there was a file uploaded - there are other ways to
                // check this such as checking the 'error' for the file - if error
                // is 0, you are good to code
                if (!empty($_FILES['imageupload']['name']))
                {
                    // Specify configuration for File 1
                    $config['upload_path'] = 'uploads/images';
                    $config['allowed_types'] = 'gif|jpg|png';
                    $config['max_size'] = '100';
                    $config['max_width']  = '1024';
                    $config['max_height']  = '768';       

                    // Initialize config for File 1
                    $this->upload->initialize($config);

                    // Upload file 1
                    if ($this->upload->do_upload('imageupload'))
                    {
                        $data = $this->upload->data();
                    }
                    else
                    {
                        echo $this->upload->display_errors();
                    }

                }

                // Do we have a second file?
                if (!empty($_FILES['videoupload']['name']))
                {
                    // Config for File 2 - can be completely different to file 1's config
                    // or if you want to stick with config for file 1, do nothing!
                    $config['upload_path'] = 'uploads/videos/';
                    $config['allowed_types'] = 'gif|jpg|png';
                    $config['max_size'] = '100';
                    $config['max_width']  = '1024';
                    $config['max_height']  = '768';

                    // Initialize the new config
                    $this->upload->initialize($config);

                    // Upload the second file
                    if ($this->upload->do_upload('videoupload'))
                    {
                        $data = $this->upload->data();
                    }
                    else
                    {
                        echo $this->upload->display_errors();
                    }

                }

            $this->load->view('thankyou');
            //Run code to add into the database
        }
    }

}

【问题讨论】:

    标签: php codeigniter file-upload controller


    【解决方案1】:

    将所有上传数据逻辑放入custom validation callback

    然后,如果验证失败(包括上传),页面将返回表单,并预先填写数据 - 您的用户可以修复文件上传。

    那么你的控制器逻辑也被简化了;

        $this->load->library('form_validation');
    
        $this->form_validation->set_rules('nominee', 'Nominee', 'required');
        $this->form_validation->set_rules('email', 'Email', 'required|valid_email');
        $this->form_validation->set_rules('location', 'Location', 'required');
        $this->form_validation->set_rules('imageupload', 'Image Upload', 'callback__image_upload');
        $this->form_validation->set_rules('videoupload', 'Video Upload', 'callback__video_upload');
    
        if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('form');
        }
        else
        {
            //Run code to add into the database
            $this->load->view('thankyou');
        }
    

    function _video_upload()
    {
       //put upload logic here and check if its valid
    }
    
    function _image_upload()
    {
       //put upload logic here and check if its valid
    }
    

    【讨论】:

    • 太棒了!所以要清楚。上传图片的脚本在 _video_upload() 函数中?
    • 是的——你“检查”上传是否成功——如果成功则返回真。如果没有,设置验证错误信息并返回false
    • 好的,它几乎可以正常工作。我遇到的唯一问题是当我为两个输入选择一个文件时,似乎总是说第二个文件太大。是把它们加在一起吗?
    • 我已经整理好了。是不是我傻了。最后一个问题。如何获取已上传回顶部的文件名的值,以便将其插入到表中的字段中?
    猜你喜欢
    • 1970-01-01
    • 2015-01-27
    • 2020-02-25
    • 2010-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-19
    • 2011-02-15
    相关资源
    最近更新 更多