Codeigniter :: alert用javascript通知max_size

时间:2017-03-02 03:51:15

标签: php codeigniter

如果图像尺寸>我发出警报使用javascript进行max_size。但为什么总是出现警惕"太大的画面"当max_size下的图像大小。我忘记了什么?

我的观点:

<?php echo form_open_multipart('#', array('id' => 'form-produk'));?>
<input type="text" name="nama" class="form-control" id="nama" placeholder="Nama produk" required>
<input type="file" name="gambar" class="form-control" id="gambar" required>
<input type="file" name="gambar_tambah" class="form-control" id="gambar" required>
<button type="submit" style="background-color:#1c2d3f;" class="simpan_produk btn btn-primary">Simpan Produk</button>
<?php echo form_close();?>

我的javascript:

$('.simpan_produk').click(function(){
    var UrlToPass = $("#form-produk").serialize();
    $.ajax({ // Send the credential values to another checker.php using Ajax in POST menthod
        type : "POST",
        data : UrlToPass,   
        url  : baseURL + "trueaccon2194/produk/proses_tambah_produk",
    beforeSend: function(){
        $('.simpan_produk').prop('disabled', true);
        $('.simpan_produk').html('sedang menyimpan...'); //Loading button text 
    },
    success : function(write){
        $('.simpan_produk').prop('disabled', false);
        $('.simpan_produk').html('Simpan'); //reset button text to original text
            if (write=="datasuccesswrite"){
                alert('data is saving!');
                window.location.href = "../";
            }else if(write=="imageistoolarge"){
                alert('too big picture!');
                location.reload();
            }
        }
    });
return false;
});

我的控制员:

function proses_tambah_produk(){
    $config['upload_path']          = 'assets/img/produk';
    $config['allowed_types']        = 'gif|jpg|png|jpeg';
    $config['max_size']             = 300;
    $config['overwrite']            = TRUE;
    //$config['maintain_ratio']         = TRUE;
    //$config['create_thumb']           = TRUE;
    //$config['max_width']            = 75;
    //$config['max_height']           = 50;
    $this->load->library('upload', $config);

    if($_FILES['gambar']['size'] > 300){
         log_helper("produk", "Gagal Menambah produk baru");
         echo"imageistoolarge";
         //redirect('trueaccon2194/produk/tambah_produk');
    }else if(!$this->upload->do_upload('gambar_tambah')){
         log_helper("produk", "Gagal Menambah produk baru");
         echo"imageistoolarge";
         //redirect('trueaccon2194/produk/tambah_produk');
    }else{

    $result=array();
    $files = $_FILES;
    $count = count($_FILES['gambar_tambah']['name']);
    for($i=0; $i<$count; $i++)
            {
            $_FILES['gambar_tambah']['name']= $files['gambar_tambah']['name'][$i];
            $_FILES['gambar_tambah']['type']= $files['gambar_tambah']['type'][$i];
            $_FILES['gambar_tambah']['tmp_name']= $files['gambar_tambah']['tmp_name'][$i];
            $_FILES['gambar_tambah']['error']= $files['gambar_tambah']['error'][$i];
            $_FILES['gambar_tambah']['size']= $files['gambar_tambah']['size'][$i];
            $this->upload->do_upload('gambar_tambah');
            $upload_data = $this->upload->data();
            $name_array[] = $upload_data['file_name'];
            $fileName = $upload_data['file_name'];
            $images[] = $fileName;
            $result[] = $files['gambar_tambah']['name'][$i];
            }

        $target = $this->input->post('nama');                   
            $this->upload->do_upload('gambar');
            $gambar = $_FILES['gambar']['name'];
            $data = $this->input->post();
            $data['id'] = $this->data['id'];
            $this->produk_adm->add($data, $gambar, $result);
            log_helper("produk", "Menambah Produk ".$target."");
            echo "datasuccesswrite";
            //redirect('trueaccon2194/produk');

    }
}

2 个答案:

答案 0 :(得分:0)

在您看来,id必须对每个元素都是唯一的。因此,idid="gambar_tambah"

<?php echo form_open_multipart('#', array('id' => 'form-produk'));?>
<input type="text" name="nama" class="form-control" id="nama" placeholder="Nama produk" required>
<input type="file" name="gambar" class="form-control" id="gambar" required>
<input type="file" name="gambar_tambah" class="form-control" id="gambar_tambah" required>
<button type="submit" style="background-color:#1c2d3f;" class="simpan_produk btn btn-primary">Simpan Produk</button>
<?php echo form_close();?>

答案 1 :(得分:0)

id对于任何元素都应该是唯一的。

<input type="file" name="gambar" class="form-control" id="gambar" required>
<input type="file" name="gambar_tambah" class="form-control" id="gambar_tambah" required>

您没有检查上传的两个文件的大小。

if($_FILES['gambar']['size'] > 300){
     log_helper("produk", "Gagal Menambah produk baru");
     echo"imageistoolarge";
} else {
 // upload files 
}

if($_FILES['gambar_tambah']['size'] > 300){
     log_helper("produk", "Gagal Menambah produk baru");
     echo"imageistoolarge";
} else {
 // upload files 
}