2017-03-02 126 views
0

如果圖像大小超過max_size,我會發出警告。但爲什麼當max_size下的圖片大小時總是出現警告「太大圖片」。有什麼我忘了?Codeigniter :: alert用javascript通知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'); 

    } 
} 

回答

0

在你看來id必須爲每個element.So唯一使第二id文件id="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();?> 
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 
}