【问题标题】:check the allowed types in codeigniter file upload检查 codeigniter 文件上传中允许的类型
【发布时间】:2021-01-01 18:33:21
【问题描述】:

我想在 codeigniter 中为我的文件创建多重上传。现在我在检查要上传的文件的允许类型时遇到了麻烦。在这里,我的控制器中有一个代码来检查数组中的文件是否是图像。

if(!empty($_FILES['requirement_files']['name'])) {
    $allowed_types = ['image/jpeg', 'image/jpg', 'image/png'];
    if(in_array($_FILES['requirement_files']['type'], $allowed_types)) {
        $input_name = "requirement_file";
        $upload_path = "../ci_capcims/assets/img/upload/requirements";
        $pref = "".date("YmdHis").".";
        $paths = multiple_upload($input_name, $upload_path, $pref);
        print_r($paths);
    }else {
        $response['error'] = 'File type not allowed';
        $response['success']='false';
        header('Content-Type: application/json');   
    }
}else {
    $response['error'] = 'No File/s selected';
    $response['success']='false';
    header('Content-Type: application/json');
}
echo json_encode($response);

我正在选择一个 PNG 文件,但我被困在代码的if(in_array($_FILES['requirement_files']['type'], $allowed_types)) 行,它返回不允许的文件类型。

这是我的观点/形式:

<form method='post' name="frm_attachments" id="frm_attachments_id" enctype="multipart/form-data">
    <input type="hidden" name="req_id" id="pre_adopt_child_req_id">
    <div class="form-group files">
        <label>Upload Your File </label>
        <input type="file" class="form-control" name="requirement_files[]" id="file_input" multiple="" required>
    </div>

    <div class="form-group">
        <label for="first_name">Remarks</label>
        <textarea class="form-control" rows="4" id="remarks_id" name="remarks" placeholder="Please input remarks here..." style="resize:none;" required></textarea>
    </div>
    <button type="button" class="btn btn-primary" id="submit_attached_files">Save</button>
</form>

这是我的 javascript:

$(document).on('click', '#submit_attached_files', function(event){ 
    event.preventDefault();
    $(":submit").attr("disabled", true);
        var fileInput = $('#file_input')[0];            
        if( fileInput.files.length > 0 ){
            var formData = new FormData();
            $.each(fileInput.files, function(k,file){   
                formData.append('requirement_files[]', file);
            });

            var other_data = $('#frm_attachments_id').serializeArray();
            $.each(other_data,function(key,input){
                formData.append(input.name,input.value);
            });

            $.ajax({
                method: 'post',
                url: baseurl+'user/post/attach_file_in_req',
                data: formData,
                dataType: 'json',
                contentType: false,
                processData: false,
                success: function(response){
                    console.log(response);
                }
            });
        }else{
            console.log('No Files Selected');
        }
 
});

【问题讨论】:

    标签: codeigniter file-upload multipartform-data jquery-file-upload


    【解决方案1】:
    $this->load->library('upload');
    $config=array();
    $config['upload_path'] = FCPATH.$save_dir;
    $config['allowed_types'] = 'gif|jpg|png|csv';
    $config['max_size'] = $this->config->item("max_file_size");
    $config['overwrite'] = true;
    $config['remove_spaces'] = true;
    $uploaded_files=array();
    $_FILES['image_profile']['name']=$CurrentUserID.'.jpg';
    foreach ($_FILES as $key => $value)
    {
      if(strlen($value['name'])>0)
      {                                  
         $this->upload->initialize($config);
         if (!$this->upload->do_upload($key))
         {
            $uploaded_files[$key]=array("status"=>false,"message"=>$value['name'].': '.$this->upload->display_errors());
         }
         else
         {
            $uploaded_files[$key]=array("status"=>true,"info"=>$this->upload->data());
         }
      }
    }
    return $uploaded_files;
    

    【讨论】:

    • 您好,先生,谢谢您的回答。你能定义 FCPATH.$save_dir;
    • 当您想要包含根文件夹中的内容时使用 FCPATH = C:\xampp\htdocs\your_root_folder\ 当您想要包含应用程序文件夹中的内容时使用 APPPATH = C:\xampp\htdocs\ your_root_folder\application\ BASEPATH = C:\xampp\htdocs\your_root_folder\system\
    猜你喜欢
    • 1970-01-01
    • 2013-04-04
    • 2017-08-18
    • 2012-04-06
    • 2011-11-21
    • 2023-04-07
    • 1970-01-01
    • 2015-05-11
    • 2012-04-22
    相关资源
    最近更新 更多