上传文件大小限制PHP(Code Igniter)

时间:2018-10-12 05:20:30

标签: php codeigniter

在验证在codeigniter中上传文件时文件大小的限制时,我遇到了一些问题。

这是我的代码

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);
}

我尝试过

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

但是它不起作用。我只是Codeigniter的新手。

验证文件大小限制的正确方法是什么? 我正在使用代码点火器

2 个答案:

答案 0 :(得分:0)

您可以在图像上传中为大小增加限制,如下所示:

$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);
    }
}

如果您只想限制大小

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);
    }
}

答案 1 :(得分:0)

下面是CodeIgniter代码的工作原理:

<?php

$config['upload_path'] = $this->path . "images/";
$config['allowed_types'] = 'gif|jpg|jpeg|png|GIF|JPG|JPEG|PNG';
$config['max_size'] = 0; //3 * 1024 * 1024; //3Mb; 0=unlimited
$config['max_width']  = 0;//"3850"; //'1024';
$config['max_height']  = 0;//"2180"; //'768';
$config['max_filename'] = 30; //0=no limit in length or size
$config['encrypt_name'] = TRUE; //TRUE=means file name will be converted to a random encrypted string
$config['remove_spaces'] = TRUE; //TRUE=means  spaces in the file name will be converted to underscores
$this->load->library('upload', $config);
if(!$this->upload->do_upload("img")){
    $error = array('error' => $this->upload->display_errors());
        echo json_encode(array('status'=>'error', 'message'=>$error));
    }else{
        $data = array('upload_data' => $this->upload->data());
        echo json_encode(array(
            "status" => "success",
            "url" => $this->path . "images/" . $data["upload_data"]["file_name"],
            "width" => $data["upload_data"]["image_width"],
            "height" => $data["upload_data"]["image_height"],
            "data" => $data["upload_data"],
            "message" => 'You have successfully uploaded the image.'
        ));
    }
?>