dtdvbf37193 2018-10-12 05:20
浏览 95

上传文件大小限制PHP(代码点火器)

I have some problem in validating limitation of file size on uploading files in codeigniter.

Here's my code

public function upload_docu_by_type() {
    ini_set('max_execution_time', 300);
    $this->load->helper(array('form', 'url'));
    $F = array();
    $prop_id = sanitize($this->input->post('prop_id'));
    $file = sanitize($this->input->post('file'));

    $config = array();
    $config['upload_path'] = './/assets//img//documents//';
    $config['allowed_types'] = 'jpeg|jpg|png|pdf';
    $config['max_size']      = '5000';
    $config['overwrite']     = TRUE;

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

    $files = $_FILES;
    for($i=0; $i< count($files['images']['name']); $i++) {   
        $config['file_name']     = $prop_id."_".$file."_".$i;        
        // $_FILES['images']['name']= time()."_".$files['images']['name'][$i];
        $_FILES['images']['name']= time()."_".preg_replace('/[()]/','',$files['images']['name'][$i]);
        // $_FILES['images']['name']= $prop_id."_".$file;
        $_FILES['images']['type']= $files['images']['type'][$i];
        $_FILES['images']['tmp_name']= $files['images']['tmp_name'][$i];
        $_FILES['images']['error']= $files['images']['error'][$i];
        $_FILES['images']['size']= $files['images']['size'][$i];    

            $this->upload->initialize($config);
            if (! $this->upload->do_upload('images')){ //if not 
                $errors = array('error' => $this->upload->display_errors());
                $data = array('success' => 0, 'errors' => $errors);
            }
            else if($_FILES['images']['size'] >= 5000){
                $data = array("success" => "1", 'message' => "You've reached the limit");   

            }else{
                $upload_data = $this->upload->data();
                $file_name =  $upload_data['file_name']; 
                $file_type =  $upload_data['file_type']; //this will be the name of your file
                $img = array('uploader_file' => $file_name); 
                $this->Applicant_model_w->upload_docu_by_type($prop_id,$file_name,$file_type,$file);
                $data = array("success" => "1", 'message' => "Successfully uploaded file/s.");
            }
    }

    generate_json($data);
}

I tried

else if($_FILES['images']['size'] >= 5000){
  $data = array("success" => "1", 'message' => "You've reached the limit");
}  

but it doesn't work. I'm just new in Codeigniter.

What would be the proper way to validate file size limitation? I'm using Code Igniter

  • 写回答

2条回答 默认 最新

  • douhan0562 2018-10-12 05:32
    关注

    You can add limit to size in image uploading for height and width like below

    $image_info = getimagesize($_FILES["images"]["tmp_name"]);
    $image_width = $image_info[0];
    $image_height = $image_info[1];
    
    if ($image_width >= 1900 && $image_height >= 600)
    {
        $this->upload->initialize($config);
        if (! $this->upload->do_upload('images')){ //if not 
            $errors = array('error' => $this->upload->display_errors());
            $data = array('success' => 0, 'errors' => $errors);
        }
    }
    

    And if you want to just limit by size

    if ($image_info >= 5000)
    {
        $this->upload->initialize($config);
        if (! $this->upload->do_upload('images')){ //if not 
            $errors = array('error' => $this->upload->display_errors());
            $data = array('success' => 0, 'errors' => $errors);
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线