Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Codeigniter上载文件不工作 函数提交文章(){ $this->load->helper(数组('form','url'); $this->load->library('form_validation'); $this->form\u validation->set\u error\u分隔符(“”,“”); $my_rules=数组( 排列( “字段”=>“标题”, “标签”=>“标题”, “规则”=>“所需|最小长度[5]|最大长度[20]| xss_清洁” ), 排列( '字段'=>'附加上载', “标签”=>“附加上传”, '规则'=>'回调\u是\u图像' ) ); $this->form\u validation->set\u rules($my\u rules); 如果($this->form\u validation->run()==FALSE){ //错误 $data['title']=ucfirst('submit Article'); $this->load->view('templates/header',$data); $this->load->view('submit_article',$data); $this->load->view('templates/footer',$data); }否则{ //成功 $data['title']=ucfirst(“提交的文章”); $this->load->view('templates/header',$data); $this->load->view('forms\u view/submit\u article\u success',$data); $this->load->view('templates/footer',$data); } } 函数为图像($value){ $config['upload_path']='./public/uploads/'; $config['allowed_types']='gif | jpg | png | pdf | tiff'; $config['max_size']='2048'; $config['max_width']='0'; $config['max_height']='0'; $config['remove_spaces']=true; $this->load->library('upload',$config); 如果(!$this->upload->do_upload()){ $this->form\u validation->set\u message('is\u image',$this->upload->display\u errors(','); 返回FALSE; }否则{ $this->upload->data(); 返回TRUE; } }_Php_Codeigniter_File Upload_Image Uploading - Fatal编程技术网

Php Codeigniter上载文件不工作 函数提交文章(){ $this->load->helper(数组('form','url'); $this->load->library('form_validation'); $this->form\u validation->set\u error\u分隔符(“”,“”); $my_rules=数组( 排列( “字段”=>“标题”, “标签”=>“标题”, “规则”=>“所需|最小长度[5]|最大长度[20]| xss_清洁” ), 排列( '字段'=>'附加上载', “标签”=>“附加上传”, '规则'=>'回调\u是\u图像' ) ); $this->form\u validation->set\u rules($my\u rules); 如果($this->form\u validation->run()==FALSE){ //错误 $data['title']=ucfirst('submit Article'); $this->load->view('templates/header',$data); $this->load->view('submit_article',$data); $this->load->view('templates/footer',$data); }否则{ //成功 $data['title']=ucfirst(“提交的文章”); $this->load->view('templates/header',$data); $this->load->view('forms\u view/submit\u article\u success',$data); $this->load->view('templates/footer',$data); } } 函数为图像($value){ $config['upload_path']='./public/uploads/'; $config['allowed_types']='gif | jpg | png | pdf | tiff'; $config['max_size']='2048'; $config['max_width']='0'; $config['max_height']='0'; $config['remove_spaces']=true; $this->load->library('upload',$config); 如果(!$this->upload->do_upload()){ $this->form\u validation->set\u message('is\u image',$this->upload->display\u errors(','); 返回FALSE; }否则{ $this->upload->data(); 返回TRUE; } }

Php Codeigniter上载文件不工作 函数提交文章(){ $this->load->helper(数组('form','url'); $this->load->library('form_validation'); $this->form\u validation->set\u error\u分隔符(“”,“”); $my_rules=数组( 排列( “字段”=>“标题”, “标签”=>“标题”, “规则”=>“所需|最小长度[5]|最大长度[20]| xss_清洁” ), 排列( '字段'=>'附加上载', “标签”=>“附加上传”, '规则'=>'回调\u是\u图像' ) ); $this->form\u validation->set\u rules($my\u rules); 如果($this->form\u validation->run()==FALSE){ //错误 $data['title']=ucfirst('submit Article'); $this->load->view('templates/header',$data); $this->load->view('submit_article',$data); $this->load->view('templates/footer',$data); }否则{ //成功 $data['title']=ucfirst(“提交的文章”); $this->load->view('templates/header',$data); $this->load->view('forms\u view/submit\u article\u success',$data); $this->load->view('templates/footer',$data); } } 函数为图像($value){ $config['upload_path']='./public/uploads/'; $config['allowed_types']='gif | jpg | png | pdf | tiff'; $config['max_size']='2048'; $config['max_width']='0'; $config['max_height']='0'; $config['remove_spaces']=true; $this->load->library('upload',$config); 如果(!$this->upload->do_upload()){ $this->form\u validation->set\u message('is\u image',$this->upload->display\u errors(','); 返回FALSE; }否则{ $this->upload->data(); 返回TRUE; } },php,codeigniter,file-upload,image-uploading,Php,Codeigniter,File Upload,Image Uploading,大家好,这是我在codeigniter中处理多部分表单数据的控制器功能代码,实际上additionalUpload字段不是必填字段,但如果用户在文件类型的additionalUpload字段中上载文件,我希望对其进行验证,当我运行上述代码并单击“提交”按钮而不选择任何文件时,它会向我显示错误“您没有选择要上载的文件”。我不希望出现此错误,因为这不是必填字段,这是我的第一个问题 第二个问题是,当我选择一个文件并点击提交按钮时,它再次向我显示“你没有选择要上传的文件” 注意:我刚刚在这里显示了我表单

大家好,这是我在codeigniter中处理多部分表单数据的控制器功能代码,实际上additionalUpload字段不是必填字段,但如果用户在文件类型的additionalUpload字段中上载文件,我希望对其进行验证,当我运行上述代码并单击“提交”按钮而不选择任何文件时,它会向我显示错误“您没有选择要上载的文件”。我不希望出现此错误,因为这不是必填字段,这是我的第一个问题

第二个问题是,当我选择一个文件并点击提交按钮时,它再次向我显示“你没有选择要上传的文件”

注意:我刚刚在这里显示了我表单中的两个字段,即标题、附加上传,但我总共有9个字段


提前感谢,请帮助任何人。

首先要输入文件字段的名称

function submit_article() {
    $this->load->helper(array('form', 'url'));
    $this->load->library('form_validation');
    $this->form_validation->set_error_delimiters('<p style="color:red">', '<br/></p>');

    $my_rules = array(
        array(
            'field' => 'title',
            'label' => 'Title',
            'rules' => 'required|min_length[5]|max_length[20]|xss_clean'
        ),
        array(
            'field' => 'additionalUpload',
            'label' => 'Additional Upload',
            'rules' => 'callback_is_image'
        )
    );

    $this->form_validation->set_rules($my_rules);

    if ($this->form_validation->run() == FALSE) {
        //ERROR
        $data['title'] = ucfirst('submit Article');
        $this->load->view('templates/header', $data);
        $this->load->view('submit_article', $data);
        $this->load->view('templates/footer', $data);
    } else {
        //SUCCESS
        $data['title'] = ucfirst('article Submitted');
        $this->load->view('templates/header', $data);
        $this->load->view('forms_view/submit_article_success', $data);
        $this->load->view('templates/footer', $data);
    }
}

function is_image($value) {

    $config['upload_path'] = './public/uploads/';
    $config['allowed_types'] = 'gif|jpg|png|pdf|tiff';
    $config['max_size'] = '2048';
    $config['max_width'] = '0';
    $config['max_height'] = '0';
    $config['remove_spaces'] = true;

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

    if (!$this->upload->do_upload()) {
        $this->form_validation->set_message('is_image', $this->upload->display_errors('<p style="color:red">', '<br/></p>'));
        return FALSE;
    } else {
        $this->upload->data();
        return TRUE;
    }
}
先试试,然后看看

对于验证字段文件:

$config['max_width']    = '2048';
$config['max_height']   = '2048';

问候语

尝试此方法检查
$\u文件
是否为空,然后执行验证,否则不执行任何操作

if($_FILES['you_field_name']['tmp_name']){

       //your code  
}
在上载函数中,您需要指定字段名

 $my_rules = array(
        array(
            'field' => 'title',
            'label' => 'Title',
            'rules' => 'required|min_length[5]|max_length[20]|xss_clean'
        )
    );

if(!empty($_FILES)){

     $my_rules[]= array(
            'field' => 'additionalUpload',
            'label' => 'Additional Upload',
            'rules' => 'callback_is_image'
        )
}

$this->form_validation->set_rules($my_rules);

$this->upload->do_upload('your_field_name')
感谢它为我所做的工作……这解决了我的第二个问题..但我的第一个问题仍然没有解决..请帮助我@dianuj sir谢谢你的宝贵时间。。但我们可以将宽度和高度设置为0,以实现无限制。。现在我的问题是,这个字段不是必需的,正如名称所示,这是一个附加字段。。因此,当用户未选择任何文件时,它不应显示任何错误消息。。但在我的情况下,这是一个错误,我不想要这个……你有什么建议吗?非常感谢@webyseo先生
 $my_rules = array(
        array(
            'field' => 'title',
            'label' => 'Title',
            'rules' => 'required|min_length[5]|max_length[20]|xss_clean'
        )
    );

if(!empty($_FILES)){

     $my_rules[]= array(
            'field' => 'additionalUpload',
            'label' => 'Additional Upload',
            'rules' => 'callback_is_image'
        )
}

$this->form_validation->set_rules($my_rules);
$this->upload->do_upload('your_field_name')