我有一个表单,在表单的操作函数中,我使用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()
,因为我必须将错误消息传递到上传页面。我怎样才能做到这一点。有人能帮我做这个吗?
提前谢谢。
试试这个
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. </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);
}