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