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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog