File wont upload in CodeIgniter - codeigniter

I want to upload files via bootstrap modal . The file doesn't get uploaded I've tried several other methods I just get the same result.I should be able to upload pdf and images
Following is my controller
public function add_file() {
$config['upload_path'] = '/emp/uploads';
$config['allowed_types'] = 'jpg|jpeg|png|pdf';
$config['max_size'] = 2048000;
$config['max_width'] = 1600;
$config['max_height'] = 1600;
$config['encrypt_name'] = TRUE;
$this->load->library('upload', $config);
if (!$this->upload->do_upload('file')) {
redirect('employees/display');
} else {
$data['emp_id'] = $this->input->post('emp');
$data ['type'] = $this->input->post('type');
$data ['file_name'] = $this->upload->data('file_name');
$this->employee_model->insert_f($data);
}
}
View:
<form action="<?php echo base_url('employees/add_file'); ?>" method="post" enctype="multipart/form-data">
<div class="modal fade" id="newmodal" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Upload</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div class="form-group">
<label class="col-sm-2 col-form-label">File Type</label>
<input type="text" name="type" class="form-control" >
</div>
<div class="form-group">
<label class="col-sm-2 col-form-label">File</label>
<input type="file" name="file" size="20" class="form-control" >
</div>
<input type="hidden" name="emp" class="form-control" value ="<?php echo$emp->emp_id ?>">
<input type="hidden" name="file_name" class="form-control" >
<div class="modal-footer">
<button type="button" class="btn btn-secondary btn-sm" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-success btn-sm" name="submit"> Save</button>
</div>
</div>
</div>
</div>
</div>
</form>
model
public function insert_f($data){
$this->db->insert('emp_doc',$data);
$id = $this->db->inser_id();
return $id;
}
please help me out where have gone wrong

You forgot to add one line pls add this lines add & then try.
$_FILES['upload_file'] = $_FILES['file'];
$config['file_name'] = $_FILES['upload_file']['name'];
if not then try from this link.
https://codeigniter.com/userguide3/libraries/file_uploading.html

You should change your path to the uploads folder, like so -
$config['upload_path'] = './uploads';
or you can use -
$config['upload_path'] = realpath('uploads');
Here is a similar question regarding your issue.
This error can also occur if you're autoloading the library, so initializing it might solve your issue(Reference).
$this->load->library('upload', $config);
$this->upload->initialize($config);
See if it helps you.

change your upload path a little bit like this.
$config['upload_path'] = './uploads/';
don't forget the last /. see if it helps

Related

Upload multiple image files using 2 input file in Laravel

I want to upload multiple images using 2 input file fields in Laravel and put that 2 files to DB with different attributes (imagepath1, imagepath2). If I try that code there both input & upload the same file like imagekitchen2 (there both change but imagepath1 become imagepath2 and imagepath2 still imagepath2).
Controller
public function store(Request $request)
{
$kitchens = new Kitchen();
$kitchens->title = $request->input('title-kitchen');
$kitchens->description = $request->input('description-kitchen');
if ($request->hasfile('imagekitchen1')) {
$file = $request->file('imagekitchen1');
$extension = $file->getClientOriginalExtension();
$filename = time().'.'.$extension;
$file->move('uploads/product/kitchen/', $filename);
$kitchens->imagepath1 = $filename;
} else {
$kitchens->imagepath1 = '';
}
$kitchens->save();
if ($request->hasfile('imagekitchen2')) {
$file = $request->file('imagekitchen2');
$extension = $file->getClientOriginalExtension();
$filename = time().'.'.$extension;
$file->move('uploads/product/kitchen/', $filename);
$kitchens->imagepath2 = $filename;
} else {
$kitchens->imagepath2 = '';
}
$kitchens->save();
}
View
<div class="card-body">
<div class="row">
<div class="col-md-6">
<form action="{{ route('addimagekitchen') }}" enctype="multipart/form-data" method="POST">
{{ csrf_field() }}
<div class="form-group">
<label>Title</label>
<label>
<input type="text" name="title-kitchen" class="form-control">
</label>
</div>
<div class="input-group">
<div class="custom-file">
<label for="image" style="display: block">Main image</label> <br/>
<input type="file" name="imagekitchen1" style="margin-left: 20px">
</div>
</div>
<div class="input-group">
<div class="custom-file">
<label for="image" style="display: block">Second image</label> <br/>
<input type="file" name="imagekitchen2" style="margin-left: 20px">
</div>
</div>
<div class="input-group">
<div class="custom-file">
<label for="image" style="display: block">Third image</label> <br/>
<input type="file" name="imagekitchen[]" style="margin-left: 20px">
</div>
</div>
<div class="form-group">
<label>Description</label>
<textarea class="form-control" name="description-kitchen" id="description-kitchen"
rows="3"></textarea>
</div>
<button type="submit" class="btn btn-success"> Insert</button>
Cancel
</form>
</div>
</div>
</div>
I'm not completely sure what you mean, but probably you should change:
$kitchens->save();
if($request->hasfile('imagekitchen2')){
into
$kitchens->save();
$kitchens = new Kitchen();
if($request->hasfile('imagekitchen2')){
this way, you will create 2 records, otherwise you used same object and updated it after creation. Depending on your needs you might also want to add:
$kitchens->title = $request->input('title-kitchen');
$kitchens->description = $request->input('description-kitchen');
before:
if($request->hasfile('imagekitchen2')){
in case you want to save same title and description for both records.
Of course I'm not sure if you want to create records if there are no files - at the moment both will be saved in case no file uploaded.

how to import excel file in mysql using laravel 5.7 with vue JS

i was creating vue file for inpute Excel or csv file laravel 5.7
i was using Maatwebsite/Laravel-Excel
dont know how to route there things it if my first time to make app like laravel with vue JS
Don't know what to doo
there is Vue file
<template>
<div class="container">
<div class="row mt-5">
<div class="col-md-12">
<div class="card">
<div class="card-header">
<h3 class="card-title">Students</h3>
<div class="card-tools">
<button class="btn btn-success" data-toggle="modal" data-target="#exampleModal">Add New <i class="fas fa-user-plus"></i></button>
<button class="btn btn-primary" data-toggle="modal" data-target="#importModel">Import <i class="fas fa-file-excel"></i></button>
<button class="btn btn-warning" data-toggle="modal" data-target="#export">Export <i class="fas fa-file-excel"></i></button>
</div>
</div>
</div>
<!-- /.card -->
</div>
</div><!-- /.row -->
<!-- Modal -->
<div class="modal fade" id="importModel" tabindex="-1" role="dialog" aria-labelledby="importModelLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="importModelLabel">Import Excel</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<form method="POST" enctype="multipart/form-data">
<div class="modal-body">
<div class="form-group">
<label>Import</label>
<input type="file" name="excel_file"
class="form-control" :class="{ 'is-invalid': form.errors.has('excel_file') }">
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save Student</button>
</div>
</form>
</div>
</div>
</div>
<!--Exit Modal-->
</div>
</template>
<script>
export default {
data(){
return{
form: new Form({
first_name:'',
last_name:'',
email:'',
password:'',
designation:''
})
}
},
mounted() {
console.log('Component mounted.')
}
}
</script>
set a post route to import data in web.php file
that will route the things as pr laravelExcel
Route::post('importExcel', 'API/StudentMasterController#import');
in a controller file
public function import()
{
Excel::import(new StudentImport, request()->file('excel_file'));
//return redirect('/')->with('success', 'All good!');
}
and in a APP/import/studentmaster.php file
public function model(array $row)
{
return new Student_master([
'EnrollmentNo' => $row[22],
]);
}
this is vue views and js my code :
<form method="POST" action="#" class="form-horizontal" #submit.prevent="createUpload" enctype="multipart/form-data">
<div class="box-body">
<div class="form-group row"><label for="name" class="col-sm-2 form-control-label">Parameter</label>
<div class="col-sm-10">
<input type="file" class="form-control" id="result_file" ref="myFiles" placeholder="Name Parameter" required="required" #change="previewFiles($event)">
<input type="hidden" name="result_file2" class="form-control" id="title" v-model="datafileupload.result_file3" placeholder="Name Parameter" required="required">
<input type="hidden" name="hasilcek" class="form-control" id="hasilcek" v-model="datafileupload.hasilcek3" placeholder="Name Parameter" required="required">
<input type="hidden" class="form-control" v-model="datapush.filename">
<input type="hidden" class="form-control" v-model="datapush.size">
</div><br><br><br>
<div v-if="this.hasilcekexcel > 0" class="alert alert-warning">File {{ namafile }} exists , do you want to replace it ?</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" #click="doSomethingOnHidden" >Close</button>
<input type="submit" class="btn btn-primary" id="submit_upload2" v-if="this.hasilcekexcel > 0" value="Replace">
<input type="submit" class="btn btn-primary" id="submit_upload" v-if="this.hasilcekexcel == 0" value="Upload">
</div>
</form>
js upload :
createUpload () {
var data = new FormData();
var file = this.$refs.myFiles.files[0];
var size = this.$refs.myFiles.files[0].size;
data.append('filenya', file);
data.append('namasifile', this.datafileupload.result_file3);
data.append('angkacek', this.datafileupload.hasilcek3);
axios.post('excelupload/upload', data)
.then(response => {
this.pesertas.push(response.data.data);
this.showModal =false;
});
},
controller :
$excelupload = new Exceluploads;
$hasilcek = $request->angkacek;
$requestfile = $request->namasifile;
if ($request->filenya != '') {
$input = Input::all();
$size = $request->file('filenya')->getClientSize();
$file = array_get($input,'result_file');
$userID = Auth::user()->id;
$uploaded_by = Auth::user()->name;
$destinationPath = 'excel';
// GET THE FILE EXTENSION
$extension = 'xls';
$fileName = $requestfile.'.'.$extension;
if ($hasilcek > 0) {
File::delete($destinationPath.'/'.$fileName);
$upload_success = $request->filenya->move($destinationPath, $fileName);
return response()->json(['data' => Exceluploads::find($excelupload->id)]);
}else if ($hasilcek == 0) {
$upload_success = $request->filenya->move($destinationPath, $fileName);
$excelupload->filename = $fileName;
$excelupload->directory = $destinationPath.'/'.$fileName;
$excelupload->size = $size;
//1 = not custome
$excelupload->type = 1;
$excelupload->uploaded_by = $uploaded_by;
$excelupload->save();
return response()->json(['data' => Exceluploads::find($excelupload->id)]);
}
}

Laravel - data stored in a variable but becomes null in database

I have a problem with my function that saves data into database,
Basically user puts name into a form and when I do console.log I do get that value, I then call my function in controller and everything looks sucessfully - no errors but when I check in mysql name = NULL, why?
My form:
<div class="modal fade" id="myModalHorizontal" tabindex="-1" role="dialog"
aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<!-- Modal Header -->
<div class="modal-header">
<button type="button" class="close"
data-dismiss="modal">
<span aria-hidden="true">×</span>
<span class="sr-only">Close</span>
</button>
<h4 class="modal-title" id="myModalLabel">
Website Name
</h4>
</div>
<div class="modal-body">
<form action="{{ action('BuilderController#postDB') }}"
class="form-horizontal" role="form" method = "POST">
<div class="form-group">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="hidden" id="code" name="newCode" value="">
<label class="col-sm-2 control-label"
for="website_name">Name</label>
<div class="col-sm-10">
<input type="text" class="form-control"
id="website_name" placeholder="Website Name"/>
</div>
</div>
</div>
<!-- Modal Footer -->
<div class="modal-footer">
<button type="button" class="form-control margin btn btn-danger"
data-dismiss="modal">
Close
</button>
<button onClick=" updateDatabase(this);" type="submit" class="form-control margin btn btn-success" id="getRequest changes">
Save Website
</button>
</div>
</div>
</div></form>
</div>
JS:
var web_name;
function updateDatabase()
{
code2 = document.getElementById("content-link2").innerHTML;
var newCode = document.getElementById('code').value = code2;
web_name = ($('#website_name').val());
console.log(web_name);
console.log(newCode);
Controller:
public function postDB(Request $request) {
$newName = $request->input('web_name');
$newLat = $request->input('newCode');
$websites = new Website();
$websites->name = $newName;
$websites->html = $newLat;
$websites->save();
// Now we go to our other function
return $this->website($newName);
}
public function website($newName)
{
// Return our "website" object
$html = Website::where('name', $newName)->first();
// Pass the contents of the "html" property to the view
return view('layouts/website', [
'html' => $html->html
]);
}
You need to add name attribute to input element:
<input name="website_name" ...

Can't upload images in Codeigniter

I have a problem with upload images in Codeigniter, when I add a new image an error show ( You did not select a file to upload.) I dont know if miss something in my code
this my function controller:
public function upload_img(){
$this->load->model('esthetique_model');
$config['upload_path'] = realpath(APPPATH.'../upload');
$config['allowed_types'] = 'jpg|jpeg|png|gif';
$config['max_size'] = '204800';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload()){
$error = array('error' => $this->upload->display_errors());
foreach ($error as $item => $value){
echo'<ol class="alert alert-danger"><li>'.$value.'</ol></li>';
}
exit;
}else{
$upload_data = $this->upload->data();
$path = $upload_data['file_name'];
$this->esthetique_model->photo_insert_mdl($path);
echo'<h4 style="color:green">Image uploaded Succesfully</h4>';
}
}
my view :
<?php echo form_open('esthetique/upload_img'); ?>
<div class="modal-body">
<!-- hidden input montinned with class sr-only -->
<div class="form-group"><label class="sr-only" =""></label>
<input type="text" name="idsc" class="sr-only" id="idsc" ></div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label for="userfile">Choisissez l'image</label>
<input type="file" name="userfile" id="userfile">
</div>
</div>
<div class="form-group">
<img id="loader" src="<?php echo base_url() ?>asset/images/486.GIF" style="height: 30px;">
</div>
<div class="form-group">
<img id="preview" src="#" style="height: 80px;border: 1px solid #DDC; " />
</div>
</div>
<div class="form-group ">
<div class="input-group">
<div class="input-group-addon">
<i class="fa fa-calendar">
</i>
</div>
<input class="form-control" id="date" name="date" placeholder="Date" type="text"/>
</div>
</div>
<div class="form-group">
<textarea class="form-control" row="3" id="note" name="note" placeholder="Note"></textarea>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Fermer</button>
<button type="submit" class="btn btn-primary">Enregistrer</button>
</div>
<?php echo form_close(); ?>
</div>
</div>
</div>
thanks for helping
Try the following (specifying the file input name in your controller):
...
if ( ! $this->upload->do_upload('userfile')) {
...
More information that may be useful: http://www.codeigniter.com/user_guide/libraries/file_uploading.html
I have changed in view
<?php echo form_open('esthetique/upload_img'); ?>
by
<?php echo form_open_multipart('esthetique/upload_img'); ?>
its work now

Cannot retrieve posted data from vue.js ajax call

I'm giving a try to Vue.js, It's much simpler than Angular and then I stuck on this problem. I'm using CodeIgniter for the backend process. Basically I just want to send data using ajax call via Vue.js then I want to retrieve the data in mmy controller. But I can't retrive the data using $this->input->post().
Here is my view. I'm using form on a modal.
<div class="modal fade modal-primary" id="modalObat" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title">Tambah Obat</h4>
</div>
<form #submit.prevent="onSubmit" class="form-horizontal">
<div class="modal-body">
<div class="form-group">
<label for="nama-obat" class="col-sm-2 control-label">Nama Obat</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="namaObat" placeholder="nama obat" v-model="newObat.nama">
</div>
</div>
<div class="form-group">
<label for="harga" class="col-sm-2 control-label">Harga</label>
<div class="col-sm-10">
<input type="number" class="form-control" id="harga" placeholder="harga" v-model="newObat.harga">
</div>
</div>
<div class="form-group">
<label for="Stok" class="col-sm-2 control-label">Stok</label>
<div class="col-sm-10">
<input type="number" class="form-control" id="stok" placeholder="jumlah" v-model="newObat.stok">
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-outline pull-left" data-dismiss="modal">Close</button>
<button type="submit" id="simpen" class="btn btn-outline">Save Changes</button>
</div>
</form>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
This is how I send the data on vue
Then here is my controller
public function tambahObat()
{
$data = array(
'nama' => $this->input->post('nama'),
'harga' => $this->input->post('harga'),
'stok' => $this->input->post('stok')
);
$data1 = $this->input->post('newObat');
$query = $this->obat_m->save($data, null);
if( $query == true )
{
$message = $this->session->set_flashdata('message', 'berhasil menyimpan');
}
else
{
$message = $this->session->set_flashdata('message', 'berhasil mengupdate');
}
redirect('obat');
}
I succeed to send the data as I check on the console
I think there is a problem with the controller but I dont know why. Could you help me, please??
You are trying to access this from inside the post callback, where this refers to something other than your vue instance. Try this format:
this.$http.post('url/to/post',{},function(){
//because of "bind", 'this' is vue
}.bind(this));

Resources