doue9730 2019-01-01 01:56 采纳率: 100%
浏览 49
已采纳

如何在codeigniter上重命名文件后修复错误循环

I created a new function to change the name of the file to be uploaded. Now I have successfully changed the file name. but in my code, before it loops through the files normally it gives an error.

I have 5 files inputs where the input is in 1 form.

There was no problems before adding my function, but after adding the rename function, my loop became messy, but my old code was working correctly.

here is my old code:

public function proses_upload()
{

    $gambar = array();
    $jumlah = count($_FILES['userfile']['name']);

    for ($i=0; $i < $jumlah; $i++) 
    { 
        $file_name = $_FILES['userfile']['name'][$i];
        $tmp_name = $_FILES['userfile']['tmp_name'][$i];        
        move_uploaded_file($tmp_name, "file/".$file_name);
        $gambar[$i] = $file_name;                 
    }

    $nama_file1 = $gambar[0];
    $format_p2ptm =1; 
    $tanggal = date("Y-m-d H:i:s");
    $jenis ='p2ptm';

    $nama_file2 = $gambar[1];
    $format_p2ptm2 =2; 
    $jenis ='p2ptm';

    $nama_file3 = $gambar[2];
    $format_p2ptm3 =3;
    $jenis ='p2ptm';

    $nama_file4 = $gambar[3];
    $format_p2ptm4 =4;
    $jenis ='p2ptm';

    $nama_file5 = $gambar[4];
    $format_p2ptm5 =5; 
    $jenis ='p2ptm';

    if (!$nama_file1 == "") {
        $this->app_model->insert_data('penyakit', $format_p2ptm, $tanggal,$nama_file1,$jenis, $this->session->userdata('id_puskesmas'));
    }
    if(!$nama_file2 == ""){
        $this->app_model->insert_data('penyakit', $format_p2ptm2, $tanggal,$nama_file2,$jenis,$this->session->userdata('id_puskesmas'));
    }
    if(!$nama_file3 == ""){
        $this->app_model->insert_data('penyakit', $format_p2ptm3, $tanggal,$nama_file3,$jenis,$this->session->userdata('id_puskesmas'));
    }
    if(!$nama_file4 == ""){
        $this->app_model->insert_data('penyakit', $format_p2ptm4, $tanggal,$nama_file4,$jenis,$this->session->userdata('id_puskesmas'));
    }
    if(!$nama_file5 == ""){
        $this->app_model->insert_data('penyakit', $format_p2ptm5, $tanggal,$nama_file5,$jenis,$this->session->userdata('id_puskesmas'));
    }

    redirect('puskesmas/view_puskesmas');

}

Here is my new code with errors:

public function proses_upload()
{

    $gambar = array();
    $jumlah = count($_FILES['userfile']['name']);

    for ($i=0; $i < $jumlah; $i++)
    { 

        $file_name = $_FILES['userfile']['name'][$i];
        $explode = explode('.',$file_name);
            // die(print_r($explode));
        $ekstensi = pathinfo($file_name, PATHINFO_EXTENSION);
        $bulan = date('M');
        $tahun = date('y') ;
        $kode  = $this->session->userdata('kode_puskesmas');
        $new_file_name = $explode[1].'_'.$kode.'_'.$bulan.$tahun.'.'.$ekstensi;
        $tmp_name = $_FILES['userfile']['tmp_name'][$i];        
        move_uploaded_file($tmp_name, "file/".$new_file_name);
        $gambar[$i] = $new_file_name;                 
    }

    $nama_file1 = $gambar[0];
    $format_p2ptm =1; 
    $tanggal = date("Y-m-d H:i:s");
    $jenis ='p2ptm';

    $nama_file2 = $gambar[1];
    $format_p2ptm2 =2; 
    $jenis ='p2ptm';

    $nama_file3 = $gambar[2];
    $format_p2ptm3 =3;
    $jenis ='p2ptm';

    $nama_file4 = $gambar[3];
    $format_p2ptm4 =4;
    $jenis ='p2ptm';

    $nama_file5 = $gambar[4];
    $format_p2ptm5 =5; 
    $jenis ='p2ptm';

    if (!$nama_file1 == "") {
        $this->app_model->insert_data('penyakit', $format_p2ptm, $tanggal,$nama_file1,$jenis, $this->session->userdata('id_puskesmas'));
    }
    if(!$nama_file2 == ""){
        $this->app_model->insert_data('penyakit', $format_p2ptm2, $tanggal,$nama_file2,$jenis,$this->session->userdata('id_puskesmas'));
    }
    if(!$nama_file3 == ""){
        $this->app_model->insert_data('penyakit', $format_p2ptm3, $tanggal,$nama_file3,$jenis,$this->session->userdata('id_puskesmas'));
    }
    if(!$nama_file4 == ""){
        $this->app_model->insert_data('penyakit', $format_p2ptm4, $tanggal,$nama_file4,$jenis,$this->session->userdata('id_puskesmas'));
    }
    if(!$nama_file5 == ""){
        $this->app_model->insert_data('penyakit', $format_p2ptm5, $tanggal,$nama_file5,$jenis,$this->session->userdata('id_puskesmas'));
    }

    redirect('puskesmas/view_puskesmas');
}

Here is my view form:

<?php echo form_open_multipart('puskesmas/proses_upload');?>

<br><br><br>
<p>
    <h4>1. Form Lap PTM</h4>
</p>
<p>
    <label>FORMAT : </label> 
    <input type="text" name="format"  style="width:230px; height:25px; border:2;  " 
    placeholder="Form Lap PTM" readonly>
</p>
<label >Input file : </label> 
<input type="file" name="userfile[]">
<br><br><br>



<p>
    <h4>2. Form Lap Posbindu</h4>
</p>
<p>
    <label>FORMAT : </label> 
    <input type="text" name="format2"  style="width:230px; height:25px; border:2;  "
    placeholder="Form Lap Posbindu " readonly =>

</p>

<label>Input file : </label> 
<input type="file" name="userfile[]">

<br><br><br>




<p>
    <h4>3. Form Lap IVA</h4>
</p>
<p>
    <label>FORMAT : </label> 
    <input type="text" name="format3"  style="width:230px; height:25px; border:2;  "
    placeholder="Form Lap IVA " readonly>

</p>

<label>Input file : </label> 
<input type="file" name="userfile[]">
<br><br><br>




<p>
    <h4>4. Form Lap Jiwa</h4>
</p>
<p>
    <label>FORMAT : </label> 
    <input type="text" name="format4"  style="width:230px; height:25px; border:2;  "
    placeholder="Form Lap Jiwa" readonly>

</p>

<label>Input file : </label> 
<input type="file" name="userfile[]">
<br><br><br>



<p>
    <h5>5. Form Lap Indera dan Gimul</h5>
</p>
<p>
    <label>FORMAT : </label> 
    <input type="text" name="format5"  style="width:230px; height:25px; border:2;  "
    placeholder="Form Lap Indera_dan Gimul" readonly>
</p>

<label>Input file : </label> 
<input type="file" name="userfile[]">
<br><br><br>




<!-- <button class="" type="submit">Upload</button> -->
<input class="btn btn-primary btn-lg" type="submit" value="Upload">
<?php echo form_close() ?>

if I upload files one of the 5 inputs that for the column I fill in the input is true, but why do the remaining 4 other inputs also enter data? even though I only input 1 input

its my phpmyadmin foto input

  • 写回答

1条回答 默认 最新

  • dsuxcxqep31023992 2019-01-01 03:16
    关注

    There will always be 5 files sent due to 5 inputs being sent, you need to verify whether or not they are empty or filled.

    $jumlah = count(array_filter($_FILES['userfile']['name']));

    Will give you the true count value of how many files have been uploaded, removing the blanks. You would ther need to update the rest of your code to accommodate for there only being x amount of array values.

    To keep your current code with minimal changes you can add an if statement to check if the name is blank

    $file_name = $_FILES['userfile']['name'][$i];
    $gambar[$i] = ''; //set it to blank by default for your checks further down
    if($file_name != ''){
        $explode = explode('.',$file_name);
            // die(print_r($explode));
        $ekstensi = pathinfo($file_name, PATHINFO_EXTENSION);
        $bulan = date('M');
        $tahun = date('y') ;
        $kode  = $this->session->userdata('kode_puskesmas');
        $new_file_name = $explode[1].'_'.$kode.'_'.$bulan.$tahun.'.'.$ekstensi;
        $tmp_name = $_FILES['userfile']['tmp_name'][$i];        
        move_uploaded_file($tmp_name, "file/".$new_file_name);
        $gambar[$i] = $new_file_name;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改