【问题标题】:Codeigniter loading view for error conditions错误条件的 Codeigniter 加载视图
【发布时间】:2014-04-28 05:06:02
【问题描述】:

我有一个表单,在表单的操作函数中,我正在使用 CI 表单验证并检查文件类型验证。代码分享如下:

function index()
    {       
        $data['dropdown'] = lang_dropdown();  // multilanguage dropdown list
        $data['language']=$this->session->userdata('site_language');
        $data['page']='video/upload';

        $this->form_validation->set_rules('name', 'Name', 'required|trim|xss_clean|max_length[255]');           
        $this->form_validation->set_rules('video_type', 'Video genre', 'required');         
        $this->form_validation->set_rules('mlanguage', 'Language', 'required');     
        $this->form_validation->set_rules('length', 'Length', 'required|is_numeric');           
        $this->form_validation->set_rules('trailer', 'Trailer', 'is_numeric');          

        if (empty($_FILES['video']['name']))
        {
            $this->form_validation->set_rules('video', 'Video', 'required');
        }       
        else 
        {
            $extn = end(explode(".", $_FILES['video']['name']));

            if($extn!='mpeg' || $extn!='mp4' || $extn!='mpg' || $extn!='mpe' || $extn!='qt' || $extn!='mov' || $extn!='avi' || $extn!='movie' || $extn!='wmv' || $extn!='flv' || $extn!='3gp' || $extn!='mkv' || $extn!='dv' || $extn!='m4u' || $extn!='m4v' || $extn!='mxu' || $extn!='rv' || $extn!='ogv')
            { 
                $data['videomsg']='<span class="error">Please upload a video of supported format</span>';
                $this->load->view('layout/template',$data);  // here not getting redirected even the control comes to this condition
            }
        }

        $this->form_validation->set_rules('link', 'Link', 'max_length[255]');   

        if (empty($_FILES['thumb_image']['name']))
        {
            $this->form_validation->set_rules('thumb_image', 'Thumb Image', 'required|max_length[255]');    
        }
        else 
        {
            $imgextn = end(explode(".", $_FILES['thumb_image']['name']));

            if($imgextn!='jpg' || $imgextn!='png' || $imgextn!='jpeg' || $imgextn!='gif' || $imgextn!='bmp' || $imgextn!='jpe' || $imgextn!='tiff' || $imgextn!='tif')
            {
                $data['imgerror']='<span class="error">Please upload an image of supported format</span>';
                $this->load->view('layout/template',$data);
            }
        }
        ..........................................      
        $this->form_validation->set_rules('description', 'Description', 'required|xss_clean|max_length[500]');          
        $this->form_validation->set_rules('keywords', 'Keywords', '');
        $this->form_validation->set_rules('accept_terms', '...', 'callback_accept_terms');
        $this->form_validation->set_error_delimiters('<span class="error">', '</span>');

        ..............................


        if ($this->form_validation->run() == FALSE) // validation hasn't been passed
        {
            $this->load->view('layout/template',$data);
        }
        else // passed validation proceed to post success logic
        {
            ...................................
            ...................................
            redirect('video/upload');
        }
    }

如果满足任何错误条件,那么我想重定向到 上传 页面并带有适当的消息。重定向在这种情况下不起作用。问题在于,在这些行中,

if (empty($_FILES['video']['name']))
        {
            $this->form_validation->set_rules('video', 'Video', 'required');
        }       
        else 
        {
            $extn = end(explode(".", $_FILES['video']['name']));

            if($extn!='mpeg' || $extn!='mp4' || $extn!='mpg' || $extn!='mpe' || $extn!='qt' || $extn!='mov' || $extn!='avi' || $extn!='movie' || $extn!='wmv' || $extn!='flv' || $extn!='3gp' || $extn!='mkv' || $extn!='dv' || $extn!='m4u' || $extn!='m4v' || $extn!='mxu' || $extn!='rv' || $extn!='ogv')
            { 
                $data['videomsg']='<span class="error">Please upload a video of supported format</span>';
                $this->load->view('layout/template',$data);
            }
        }

如果上传的文件不是视频文件,控件进入条件,但不会重定向到上传页面。现在发生的是,它使用Successfully uploaded the video ! 消息重定向到上传页面,该消息会将数据插入数据库并创建文件夹。

我该如何解决这个问题?我不需要redirect(),因为我必须将错误消息传递到 upload 页面。我怎样才能做到这一点。谁能帮我做这件事?

提前致谢。

【问题讨论】:

  • 这里的代码太多了。请编辑与问题无关的代码。如果您可以格式化代码,这样我们就不必水平滚动太多(尤其是长 if 语句),那也很好。
  • 那么长的 if 语句对 OR 造成了困扰......如果第一个表达式为真,则不会检查其余部分
  • 但该条件运行良好..其中没有任何问题..
  • 如果他们上传一个文件……那代码不会被命中
  • 我现在面临的唯一问题是重定向不起作用..直到那条线它工作正常..

标签: php codeigniter validation


【解决方案1】:

试试这个

if (!empty($_FILES))
            {
                if ($_FILES["video"]["error"] > 0)
                {
                  echo "<div style='float: left;color:red; padding: 10px; class='file_upload_error'>Error: " . $_FILES["video"]["error"]."</div>";
                  return FALSE;
                }
                $file = $_FILES['video']['tmp_name'];
                // Validate the file type
                $fileTypes = array('avi','mp4','mpg','mov'); // File extensions
                $fileParts = pathinfo(strtolower($_FILES['video']['name']));
                if (!in_array($fileParts['extension'],$fileTypes)) {
                    $data['error'] = '<div style="float: left;color:red; padding: 10px;">Invalid file type. &nbsp;&nbsp;&nbsp;</div>';
                    $this->load->view('layout/template',$data);
                }
                else{
                    $ext = pathinfo($_FILES['video']['name'], PATHINFO_EXTENSION);
                    move_uploaded_file($_FILES['video']['tmp_name'], $uploaddir.$filename.".".$ext);
                }

            }else{
                $data['error'] = '<div style="float: left;color:red; padding: 10px;" class="file_upload_error">Something went wrong, the file was not uploaded.<br />Please try again later..</div>';
                $this->load->view('layout/template',$data);
            }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-29
    • 1970-01-01
    • 1970-01-01
    • 2011-10-07
    • 2017-10-23
    • 2014-10-18
    • 2013-01-10
    • 1970-01-01
    相关资源
    最近更新 更多