Upload File in Edit Multiple Data Form - php

I'm trying to create an edit multiple data form like this using Laravel 8. In the form, you could upload only one file for each row. I could update the data without uploading file, but when I tried to update the file, it redirect me back to the form and no file uploaded. I already add enctype to my form. This is my code:
View:
<div class="custom-file" style="margin-left:10px; margin-right:10px;">
<input type="file" name="bukti[]" class="custom-file-input">
<label class="custom-file-label"></label>
</div>
Controller:
if($request->file('bukti') !=null) {
//upload file
$bukti=array();
if($files=$request->file('bukti')) {
foreach($files as $file) {
$name=$file->getClientOriginalName();
$path=$file->storeAs('blabla',$name);
$bukti[]=$name;
}
}
//Update multiple data
if(count($request->id) > 0) {
foreach($request->id as $item => $v) {
$data = array(
'id_laporan' => $laporan_indikators->id,
'id_pertanyaan' => $request->id_pertanyaan[$item],
'jumlah' => $request->jumlah[$item],
'keterangan' => $request->keterangan[$item],
'bukti' => $bukti[$item],
);
$data_laporans = DataLaporan::where('id',$request->id[$item])->first();
$data_laporans->update($data);
}
}
I already tried to fix it but I still didn't get a solution. Please help me because I'm still a beginner. I'm sorry for my bad English. Thank you.
UPDATE: I already tried to update the code to something like this:
Controller:
//upload file
if($files=$request->file('bukti')) {
foreach($files as $file) {
$name=$file->hashName();
$path=$file->storeAs('blabla',$name);
$bukti=$name;
}
}
//Update multiple data
if(count($request->id) > 0) {
foreach($request->id as $item => $v) {
$data = array(
'id_laporan' => $laporan_indikators->id,
'id_pertanyaan' => $request->id_pertanyaan[$item],
'jumlah' => $request->jumlah[$item],
'keterangan' => $request->keterangan[$item],
'bukti' => $bukti,
);
$data_laporans = DataLaporan::where('id',$request->id[$item])->first();
$data_laporans->update($data);
}
}
It uploaded the file and redirect me back to the index page after updating successfully, but when I check the database, the controller updated all of the bukti column value to the same file name. If I tried to update file for two rows, all of the bukti column is just update the value to the last uploaded file name. What I want is there's just one data for jumlah, keterangan, and one bukti file in a row, so I can have different files for each row. I don't really know how to fix this because I'm still a beginner. Thank you for your help.

Try to add the double question (??) mark for check whether the index of array is available or not:
<?php
if($request->file('bukti') !=null) {
//upload file
$bukti=array();
if($files=$request->file('bukti')) {
foreach($files as $file) {
$name=$file->getClientOriginalName();
$path=$file->storeAs('blabla',$name);
$bukti[]=$name;
}
}
//Update multiple data
if(count($request->id) > 0) {
foreach($request->id as $item => $v) {
$data = array(
'id_laporan' => $laporan_indikators->id,
'id_pertanyaan' => $request->id_pertanyaan[$item],
'jumlah' => $request->jumlah[$item],
'keterangan' => $request->keterangan[$item],
'bukti' => $bukti[$item] ?? '',
);
$data_laporans = DataLaporan::where('id',$request->id[$item])->first();
$data_laporans->update($data);
}
}

After trying for some times, finally I found a solution. Actually, I just need to give a condition if a file is uploaded in certain row.
View:
<div class="custom-file" style="margin-left:10px; margin-right:10px;">
<input type="file" name="bukti_{{ $data->id }}" class="custom-file-input">
<label class="custom-file-label"></label>
</div>
Controller:
if (count($request->id) > 0) {
$bukti = '';
foreach ($request->id as $item => $v) {
$id_item = $request->id[$item];
$file = $request->file('bukti_'.$id_item);
$data = array(
'id_laporan' => $laporan_indikators->id,
'id_pertanyaan' => $request->id_pertanyaan[$item],
'jumlah' => $request->jumlah[$item],
'keterangan' => $request->keterangan[$item],
);
//If file is updated
if ($file) {
$name = $file->hashName();
$path = $file->storeAs('blabla', $name);
$bukti = $name;
$data['bukti'] = $name;
}
$data_laporans = DataLaporan::where('id', $request->id[$item])->first();
$data_laporans->update($data);
}
}

Related

Codeigniter multiple files uploads per field with input array key

I have here multiple fields with multiple files uploads with array key name. I need to uploads multiple files per field and pass the filenames in array with keys into the model.
I really need this, hoping for someone would help me.
Thanks.
Here's the example of my fields
//with 2 or more files
<input accept="application/pdf" name="vtc_file[0]" id="vtc_file0" type="file" multiple/>
//with 2 or more files
<input accept="application/pdf" name="vtc_file[1]" id="vtc_file1" type="file" multiple/>
<?php
$count = count($_FILES['vtc_file']['name']);
for ($i=0; $i < $count; $i++)
{
foreach ($_FILES['vtc_file'] as $key1 => $value1)
{
foreach ($value1 as $key2 => $value2)
{
$files[$key2][$key1] = $value2;
}
}
}
$_FILES = $files;
$document_config['upload_path'] = 'uploads/';
$document_config['allowed_types'] = 'pdf';
$this->load->library('upload', $document_config);
foreach ($_FILES as $fieldname => $fileObject)
{
if (!empty($fileObject['name']))
{
$this->upload->initialize($document_config);
if (!$this->upload->do_upload($fieldname))
{
$errors = $this->upload->display_errors();
}
else
{
$results = $this->models->save();
}
}
}
I've made a demo for you, I've explained the steps in the code itself. Hope it works for you.
View
<form action="<?Php echo base_url('home/myfunc'); ?>" method="POST" enctype="multipart/form-data">
<!-- You need to make an array of input fields(See {name}) -->
<input accept="application/pdf" name="vtc_file[0][]" id="vtc_file0" type="file" multiple/>
<!-- If the usdr uploads only one file it will be stored like vtc_file[0][1] = 'filename.pdf' and so on.-->
<br>
<input accept="application/pdf" name="vtc_file[1][]" id="vtc_file1" type="file" multiple/><br>
<button type="submit" value="submit">Submit</button>
</form>
Controller
function myfunc(){
// check if the $_FILES is not empty(your validation here) then perform these actions↓↓
foreach ($_FILES['vtc_file']['name'] as $key1 => $value1){ // We only need to loop through all the input fields(vtc_file)
foreach ($value1 as $key2 => $value2){ // loop through name of all the files uploaded
// Make a new dummy element(userfile) with the file(vtc_file') details in it
$_FILES['userfile']['name'] = $_FILES['vtc_file']['name'][$key1][$key2];
$_FILES['userfile']['type'] = $_FILES['vtc_file']['type'][$key1][$key2];
$_FILES['userfile']['tmp_name'] = $_FILES['vtc_file']['tmp_name'][$key1][$key2];
$_FILES['userfile']['error'] = $_FILES['vtc_file']['error'][$key1][$key2];
$_FILES['userfile']['size'] = $_FILES['vtc_file']['size'][$key1][$key2];
$config['upload_path'] = './uploads/aaa'; // path to the folder
$config['allowed_types'] = 'pdf';
$config['max_size'] = 1000;
// $config['max_width'] = 1024;
// $config['max_height'] = 768;
$this->load->library('upload', $config); // load the {upload} library
$this->upload->initialize($config); // initialize the library
if (!$this->upload->do_upload("userfile")){ // upload the file(current)
$data['errors'][$key1][$key2] = $this->upload->display_errors(); // if any error store them in {errors} variable with keys
}else{
$upload_data = $this->upload->data(); // get the uploaded file data
$data['filename'][$key1][$key2] = $upload_data['file_name']; // store the upload file name in {filename} variable with keys
}
}
}
//load model {event_model}
$this->load->model('event_model');
$success = $this->event_model->save_file($data['filename']); // call model function
// check if query successful
if($success){
// do something (Load a view)
// You can show uploaded files in your view with $data['filename'][$key1][$key2]
// And the errors of files that couldn't be uploaded with $data['errors'][$key1][$key2]
echo '<pre>'; print_r($data['errors']);
}else{
// do something else
}
}
Model
function save_file($data){
echo '<pre>'; print_r($data);
// Your insert query here.
return true;
}
Output:
Model: $filename array
Array
(
[0] => Array
(
[0] => Account_Software______Payment.pdf
[1] => Account_Software______RTGS_Report.pdf
)
[1] => Array
(
[0] => BUSINESS_PROFILE.pdf
)
)
Controller: $errors array
Array
(
[1] => Array
(
[1] =>
The filetype you are attempting to upload is not allowed.
)
)
Images:

tables export into to csv files

In laravel 5.5 project I want some my tables export into to csv files and I get this http://www.maatwebsite.nl/laravel-excel/docs/export#export library
with code like:
$path = $directoriesArray[count($directoriesArray)-1];
Excel::create($filename, function($excel) use($dataArray) {
$excel->sheet('file', function($sheet) use($dataArray) {
$sheet->fromArray( $dataArray );
});
})->store('csv', $path)->export('csv');
I upload 1 file ok, but as I need to upload several tables any
to the relative file I run the exporting function in a circle
and only the first file is uploaded. Is it restriction of the
browser(I tried in chromium, firefox) or if is the way to upload all files?
1) If there is a way just to write this csv file to disk without uploading?
2) If there is some way to make kind of buffering ( like ob_start ) of output data and write them to the files manually?
3) If there is some other tools that could do this?
Thanks!
The decision was :
})->store('csv', $path);
without
->export('csv')
:)
I did it like :
<?php
$tablesArray= [ // array of tables to export
'settings',
'groups',
'users',
];
$now_label= '_1';//strftime('%Y-%m-%d_%H_%M_%S');
$directories= [ public_path('uploads'), public_path('uploads/csv') , public_path('uploads/csv/loan_dump_'.$now_label) ];// to which directory export files
foreach( $tablesArray as $next_key=>$next_table_name ) { // all tables
$columnListing= \Schema::getColumnListing( $next_table_name ); // table columns
if ( $next_table_name == 'settings' ) {
$dataRows = Settings::all();
}
if ( $next_table_name == 'groups' ) {
$dataRows = Group::all();
}
if ( $next_table_name == 'users' ) {
$dataRows = User::all();
}
if ($dataRows == null) {
die("-1 XXZ INVALID DATA");
}
$dataRowsArray = $dataRows->toArray();
$writeRowsArray= [];
$row= 0;
foreach( $dataRowsArray as $next_key=>$nextDataRow ) {
if (!empty($nextDataRow['created_at'])) {
$nextDataRow['created_at'] = $this->dateTimeToDbFormat($nextDataRow['created_at']);
}
if (!empty($nextDataRow['updated_at'])) {
$nextDataRow['updated_at'] = $this->dateTimeToDbFormat($nextDataRow['updated_at']);
}
if ($row == 0) { // first row with keys/file headers
$writeRowsArray[] = $nextDataRow;
} else {
$writeRowsArray[] = array_values($nextDataRow);
}
$row++;
}
$this->writeArrayToCsvFile($writeRowsArray, $next_table_name, $directories);
} // foreach( $tablesArray as $next_key=>$next_table_name ) { // all tables
public function writeArrayToCsvFile(array $dataArray, string $filename, array $directoriesArray) : int
{
self::createDir( $directoriesArray );
$path = $directoriesArray[count($directoriesArray)-1];
Excel::create($filename, function($excel) use($dataArray) {
$excel->sheet('file', function($sheet) use($dataArray) {
$sheet->fromArray( $dataArray );
});
})->store('csv', $path)->export('csv');
return 1;
}
?>
Only 1st file in $tablesArray array is exported ok.
If to comment 1st table, then next table would uploaded ok and so on.
What is strange file is uploaded twice : in $directories path and in "Downloads" directory(I am under kubuntu).

I cant insert multiple photos together with multiple rows

I cant insert multiple photos together with multiple rows.
I have here input fields:
<input name="u_code[]" required="required" style="margin:0px; ">
<input name="u_name[]" required="required" style="margin:0px; ">
<input name="u_address[]" required="required" style="margin:0px; ">
<input name="photo[]" required="required" style="margin:0px; ">
and this is my controller:
function user_add()
{
if ($_POST)
{
$u_id =$this->input->post('u_id');
$u_code =$this->input->post('u_code');
$u_name =$this->input->post('u_name');
$u_address = $this->input->post('u_address');
$data = array();
for ($i = 0; $i < count($this->input->post('u_id')); $i++)
{
$data[$i] = array(
'u_id' => $u_id[$i],
'u_code' => $u_code[$i],
'u_name' => $u_name[$i],
'u_address' => $u_address[$i],
);
}
$insert = $this->user_model->user_add($data);
echo json_encode(array("status" => TRUE));
}
}
My problem is i dont know where what exact code should be add to upload photos in a multiple rows.
please check photo here:
screenshot of input field
Thanks advance for the help..
Try this,
function user_add()
{
if ($_POST) {
$u_id = $this->input->post('u_id');
$u_code = $this->input->post('u_code');
$u_name = $this->input->post('u_name');
$u_address = $this->input->post('u_address');
$data = array();
for ($i = 0; $i < count($this->input->post('u_id')); $i++) {
$data[$i] = array(
'u_id' => $u_id[$i],
'u_code' => $u_code[$i],
'u_name' => $u_name[$i],
'u_address' => $u_address[$i],
);
$insert = $this->user_model->user_add($data);
}
echo json_encode(array("status" => true));
}
}
Have a look at this code may this help in understanding you how to handle multiple images
$files = $_FILES;
$count = count($_FILES['uploadfile']['name']);
for($i=0; $i<$count; $i++)
{
$_FILES['uploadfile']['name']= $files['uploadfile']['name'][$i];
$_FILES['uploadfile']['type']= $files['uploadfile']['type'][$i];
$_FILES['uploadfile']['tmp_name']= $files['uploadfile']['tmp_name'][$i];
$_FILES['uploadfile']['error']= $files['uploadfile']['error'][$i];
$_FILES['uploadfile']['size']= $files['uploadfile']['size'][$i];
$this->upload->initialize($this->set_upload_options());//function defination below
$this->upload->do_upload('uploadfile');
$upload_data = $this->upload->data();
$name_array[] = $upload_data['file_name'];
$fileName = $upload_data['file_name'];
$images[] = $fileName;
}
$fileName = $images;
$_FILE it is an associative array of items uploaded to the current script via the POST method.for the further look this helper link
it's an automatic variable available within all scopes of the script
function set_upload_options()
{
// upload an image options
$config = array();
$config['upload_path'] = LARGEPATH; //give the path to upload the image in folder
$config['remove_spaces']=TRUE;
$config['encrypt_name'] = TRUE; // for encrypting the name
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '78000';
$config['overwrite'] = FALSE;
return $config;
}
Some basic tip:
An input element must have multiple=" multiple" or just multiple.Load upload Library.
$this->upload->do_upload() will upload the file selected in the given field name to the destination folder.
$this->upload->data() returns an array of data related to the uploaded file like the file name, path, size etc.

codeigniter insert many images name into database

I am build uploader images and store it into database, I already can upload many images to folder, but I can't insert all images name that uploaded, and I don't know how to insert into database, first I have put commend on my code below when error occur, second I don't know the query to put it in database if the image count is different e.g 1-10 images, last question, if I do query "SELECT id..." and I want to return it, is there method to return it into string or int? If I use row() it will return stdClass object. please help me,
below is my code:
controller :
$this->load->library("myupload", "form_validation");
$this->load->model("testModel");
$barangImage = array();
if($this->input->post("formSubmit")) {
$this->form_validation->set_rules("nama", "Nama", "required|trim");
if($this->form_validation->run()) {
$insertData = array(
"nama" => $this->input->post("nama")
);
if($id = $this->testModel->add($insertData)) {
//print_r($id);
if(isset($_FILES) && $image = $this->myupload->uploadFile($_FILES)) {
//$image here is already fill with all images name
if(isset($image["error"]) && $image["error"]) {
echo $image["error"];
}else {
foreach($image as $img) {
$barangImage = array(
"gambar" => $img,
"barangid" => $id
);
}
//but when i put into barangImage,
//it only stored last image name
print_r($barangImage);
//output `Array ( [gambar] => 2.JPG [barangid] => Array ( [id] => 52 ) )`
}
}
if($id = $this->testModel->add_images($barangImage)) {
echo "SUCCESS !!!";
}else {
echo "FAIL INSERT IMAGES!!!";
}
}else {
echo "FAIL INSERT DATA NAMA";
}
}else {
echo "FAIL VALIDASI RUN";
}
}
model :
public function add($newData){
$this->db->insert("cobabarang", $newData);
$nama = $newData["nama"];
$id = $this->db->query("SELECT id FROM cobabarang WHERE nama = \"$nama\"");
return $id->row_array();
}
public function add_images($newImage) {
//$this->db->insert("cobagambar", $newImage);
$id = $newImage["barangid"]["id"];
$gambar = $newImage["gambar"];
$this->db->query("INSERT INTO cobagambar(barangid, gambar1) VALUES($id, \"$gambar\")");
}
there is an error here:
foreach($image as $img)
{
$barangImage = array(
"gambar" => $img,
"barangid" => $id
);
}
change the $barangImage to $barangImage[]
when you put the images into database i suggest that using json_encode($barangImage), and then json_decode($images-json-string) when you going to use the images.
There is something wrong with your foreach loop
foreach($image as $img) {
$barangImage = array(
"gambar" => $img //might be img['img'] I guess $img is again an array...you hvae to check that
"barangid" => $id //might be $img['id']check on this too..will be $img['id'] I guess
);
}
My guess is that $img is again an array with some keys. You really need to check on that And you can directly call the insert function in that foreach loop itself like this,
foreach($image as $img) {
$barangImage = array(
"gambar1" => $img['img'], //I guess $img is again an array...you hvae to check that
"barangid" => $img['id'] //check on this too..will be $img['id'] I guess
);
$id = $this->testModel->add_images($barangImage));
}
NOTE: The keys in your array barangImage must be column name in the table. i.e
gambar1 and barangid will be your column names. so you can directly use codeIgniter's active records.
Just change your add_images function
public function add_images($newImage) {
$this->db->insert("cobagambar", $newImage);
}

Only the first IF statement out of 3 is executed within a PHP loop

The following code uploads multiple images no problem. However, I'm trying to get it to update a field in a table based on what iteration the loop is in. PROBLEM: The IF Statement seems to not work when looped. I.e. it only adds the first file_name to the database.
Anyone see what I'm doing wrong here? Much appreciated if so!!!
for ($i = 1; $i < 4; $i++)
{
/* Handle the file upload */
$upload = $this->upload->do_upload('image' . $i);
/* File failed to upload - continue */
if ($upload === FALSE)
continue;
/* Get the data about the file */
$data = $this->upload->data();
$uploadedFiles[$i] = $data;
if ($i == 1)
{
$filenames1 = array(
'product_image_front' => $data['file_name'],
);
$this->db->where('id', $this->db->insert_id());
$this->db->update('products', $filenames1);
}
if ($i == 2)
{
$filenames2 = array(
'product_image_back' => $data['file_name'],
);
$this->db->where('id', $this->db->insert_id());
$this->db->update('products', $filenames2);
}
if ($i == 3)
{
$filenames3 = array(
'product_image_back' => $data['file_name'],
);
$this->db->where('id', $this->db->insert_id());
$this->db->update('products', $filenames3);
}
}
insert_id - Get the ID generated in the last query.
Store it in a variable before the loop.

Categories