How to store the file in mysql database using codeigniter? - php

I have created a simple contact form.I want to store the file in database which is uploaded by any user.I am able to store the file in my folder but i have to store the file in database to identify the admin which file is upload.
Controller
public function index()
{
$this->load->view('demo', array('error' => ' ' ));
}
public function do_upload()
{
$id = $this->session->userdata('id');
$this->load->model('Model_doc');
$config['upload_path'] = './upload/';
$config['allowed_types'] = 'docx';
$config['max_size'] = 1000;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$this->load->library('upload', $config);
$this->upload->do_upload();
$this->Model_doc->index($id, $this->upload->data());
}
Model
public function index($id,$imgdata)
{
$imgdata = file_get_contents($imgdata['../upload/']);
$data = array(
'path'=>$imgdata,
);
$this->db->set('curentDate', 'NOW()', FALSE);
$this->db->insert('test',$data);
}
View
<?php echo form_open_multipart('welcome/do_upload');?>
<input type="file" name="userfile" size="20" />
<br /><br />
<input type="submit" value="upload" />
</form>

Change code in the Controller File as Folow:
public function do_upload()
{
$id = $this->session->userdata('id');
$this->load->model('Model_doc');
$config['upload_path'] = './upload/';
$config['allowed_types'] = 'docx';
$config['max_size'] = 1000;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$this->load->library('upload', $config);
$this->upload->do_upload();
$full_file_path = base_url()."/upload/".$_FILES['userfile']['name'];
//This Is Full Path of the file stored to your folder,Change it if required.
$this->Model_doc->index($id,$full_file_path);
}
Changes in the Model File:
public function index($id,$full_file_path)
{
$data = array(
'path'=>$full_file_path,
);
$this->db->set('curentDate', 'NOW()', FALSE);
$this->db->insert('test',$data);
}

Related

insert multiple images in database using codeigniter and filepond

Following is my controller In this I am using two if statements one for multiple images and another is for the featured image.. my images are uploaded in a folder very well but multiple names are not inserted in the database...Only one file name is inserted in the database...
public function uploadApi()
{
if (isset($_FILES['userfile'])) {
$config['upload_path'] = 'uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 200000;
$config['max_width'] = 2024;
$config['max_height'] = 1768;
$this->upload->initialize($config);
$this->load->library('upload', $config);
$this->upload->do_upload('userfile');
$data = array( $this->upload->data());
$this->m->update_post($data[0]['file_name']);
}
if(isset($_FILES['userfile1'])) {
$config['upload_path'] = 'uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 200000;
$config['max_width'] = 2024;
$config['max_height'] = 1768;
$this->upload->initialize($config);
$this->load->library('upload', $config);
$this->upload->do_upload('userfile1');
$data = array( $this->upload->data());
$this->m->update_feature($data[0]['file_name']);
}
}
This is a model ..
#-Update images Post-#
public function update_post($picture) {
$post = array(
'post_images'=>$picture,
);
$this->db
->where('post_status','draft')
->update('post',$post);
return true;
}
public function update_feature($picture) {
$post = array(
'post_featured_image'=>$picture,
);
$this->db
// ->set('post_created', 'NOW()', FALSE)
->where('post_status','draft')
->update('post',$post);
return true;
}
filepond plugin script
FilePond.registerPlugin(
FilePondPluginFileValidateSize,
FilePondPluginImageExifOrientation,
FilePondPluginImageCrop,
FilePondPluginImageResize,
FilePondPluginImagePreview,
FilePondPluginImageTransform
);
// Set default FilePond options
FilePond.setOptions({
// maximum allowed file size
maxFileSize: '50MB',
imagePreviewHeight: 100,
imagePreviewWidth: 200,
instantUpload: true,
// crop the image to a 1:1 ratio
imageCropAspectRatio: '1:1',
// upload to this server end point
server: {
url: '<?php echo base_url() ?>Admin/uploadApi',
}
});
var pond = FilePond.create(document.querySelector('input[name="userfile"]'));
var pond = FilePond.create(document.querySelector('input[name="userfile1"]'));
**This is a view ..**
<form method="post" enctype="multipart/form-data" class="toggle-disabled" action="<?php echo base_url() ?>Admin/update_post1" id='ritesh'>
<div class="col-md-6">
<div class="form-group">
<label>Upload images</label>
<input type="file"
class="filepond"
name="userfile"
multiple
data-max-file-size="5MB"
data-max-files="50" data-validation="required extension" />
</div>
<div class="form-group">
<label>Feature image</label>
<input type="file"
class="filepond"
name="userfile1"
data-max-file-size="5MB"
data-validation="required extension"
/>
</div>
</form>
For multiple image upload you should post images array like; imagename[]. Your current approach is not good.
You must try already posted answers:
Multiple image upload with CodeIgniter
Multiple image upload with Codeigniter saving only one file path to MySQL Database
https://www.codexworld.com/codeigniter-upload-multiple-files-images/
Please try to this in controller
function uploadApi() {
$image = $_FILES;
foreach ($image as $key => $img) {
if (!is_dir('./Uploads/')) {
mkdir('./Uploads/', 0777, TRUE);
}
if (!empty($img['name'])) {
$config['upload_path'] = './Uploads/Products/';
$config['allowed_types'] = '*';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$config['overwrite'] = TRUE;
$config['file_name'] = date('U') . '_' . $img['name'];
$this->load->library('upload', $config);
$this->upload->initialize($config);
if (!$this->upload->do_upload($key)) {
$error = array('error' => $this->upload->display_errors());
print_r($error);
die;
} else {
if ($this->upload->do_upload($key)) {
$image_data = $this->upload->data();
$update["userfile"] = $config['file_name'];
$res = $this->m->update_post($update);
}
}
}
}
$this->load->view('imgtest');
}

Codeigniter - Need help uploading picture to both folder and database

I need help uploading the picture to the folder and database. It seems I can only send it to the database, but not upload it to the folder. Is there anything wrong with the code?
admin.php (controller)
public function input_siswa(){
$config['upload_path'] = './gambarfolder/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload');
$this->upload->initialize($config);
$location=base_url().'/gambarfolder/';
$pict=$location.$data_imge;
$nama_siswa = $this->input->post('nama_siswa');
$nis = $this->input->post('nis');
$id_jurusan = $this->input->post('id_jurusan');
$data = array(
'nama_siswa'=>$nama_siswa,
'nis'=>$nis,
'id_jurusan'=>$id_jurusan,
'gambar'=>$pict
);
$this->m_model->create('siswa',$data);
redirect(base_url('index.php/admin/tampil_siswa'));
}
Upload image in codeigniter using upload library.
public function input_siswa(){
$config['upload_path'] = './gambarfolder/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$location=base_url().'/gambarfolder/';
$pict=$location.$data_imge;
$this->load->library('upload', $config);
if ($this->upload->do_upload('userfile'))
{
$nama_siswa = $this->input->post('nama_siswa');
$nis = $this->input->post('nis');
$id_jurusan = $this->input->post('id_jurusan');
$data = array(
'nama_siswa'=>$nama_siswa,
'nis'=>$nis,
'id_jurusan'=>$id_jurusan,
'gambar'=>$pict
);
$this->m_model->create('siswa',$data);
redirect(base_url('index.php/admin/tampil_siswa'));
// redirect succsess page when image upload succsessfully.
}else{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload', $error);
// return image upload error.
}
}
first check your folder gambarfolder is in the root of your codigniter application
second check in your form tag you add enctype="multipart/form-data"
and add that lines for upload image to gambarfolder folder
$this->load->library('upload', $config);
if($this->upload->do_upload('userfile'))
{
}
where userfile is the name of file tag like
<input type="file" name="userfile" size="20" />
and $config is your config array
and also check https://www.codeigniter.com/userguide3/libraries/file_uploading.html
You need to add below line after initialise config
$this->upload->do_upload('userfile')

my code does not work as i am new to codeigneter so having trouble with this

views/image.php
<form action="upload1" method="post" enctype="multipart/form-data">
<h3>Image Upload Form</h3>
<input type="file" name="pic" tabindex="2" required>
<input type="text" name="alt" placeholder="Image Alt Text" tabindex="1"
required>
<button type="submit" id="img-submit" data-submit="Sending">Submit</button>
</form>
controller/upload.php
function upload1()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 100;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$data = array(
'image_url' => $this->input->$_FILES['pic']['name'],
'alt' => $this->input->post('alt')
);
$result = $this->upload_m->upload1($data);
}
model/upload_m.php
<?php
class upload_m extends CI_Model {
function __construct()
{
parent::__construct();
}
function upload1($data)
{
$data ['image_url']= $this->upload1($data);
$data ['alt']= $this->input->post('alt');
$this->db->insert('image', $data);
redirect(base_url() . 'index.php/upload/', 'refresh');
}
}
?>
when i click on the submit button upload1 function call and it gives error array to string conversion
and in model
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 262144 bytes) in C:\xampp\htdocs\CodeIgniter\application\models\upload_m.php on line 12
Can anyone detect and tell me what mistake I am doing?
You should change your controller as below:
function upload1() {
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 100;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$data = array(
'image_url' => $_FILES['pic']['name'],
'alt' => $this->input->post('alt')
);
if ( ! $this->upload->do_upload('pic')) {
//$error = array('error' => $this->upload->display_errors());
// use $error by passing to a view. Please see the documentation.
} else {
$result = $this->upload_m->upload1($data);
redirect(base_url() . 'index.php/upload/', 'refresh');
}
}
and the model:
class upload_m extends CI_Model
{
function __construct()
{
parent::__construct();
}
function upload1($data)
{
$image_data = $this->upload->data();
$data['image_url'] = $image_data['file_name'];
$data['alt'] = $this->input->post('alt');
return $this->db->insert('image', $data);
}
}
Note: You should not redirect inside the model.
'image_url' => $this->input->$_FILES['pic']['name'],
Above line is syntactically wrong. And below line in model make a recursive call
$data ['image_url']= $this->upload1($data);
Try this
In Controller
function upload1()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 100;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('pic'))
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('ViewName', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
$alt = $this->input->post('alt');
$this->upload_m->upload1($data, $alt);
$this->load->view('successView', $data);
}
}
In Model
function upload1($data, $alt)
{
$data['upload_path']= $this->upload1($data);
$data['alt']= $alt;
$this->db->insert('image', $data);
}
Links
CodeIgniter: Allowed memory exhausted
File Uploading Class

Uploading multiple images with codeigniter to a database

My functions upload only one image at a time, when the form is submitted. I can not upload multiple images at once. This is a huge problem because I am building a car-sales website, and people need to upload multiple car images.
My upload.php controller:
<?php
class Upload extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
$this->load->model('upload_model');
}
function index()
{
$this->load->view('common/header');
$this->load->view('nav/top_nav');
$this->load->view('upload_form', array('error' => ' ' ));
}
function do_upload()
{
if($this->input->post('upload'))
{
$config['upload_path'] = './images/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1024';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
else
{
$data=$this->upload->data();
$this->thumb($data);
$file=array(
'img_name'=>$data['raw_name'],
'thumb_name'=>$data['raw_name'].'_thumb',
'ext'=>$data['file_ext'],
'upload_date'=>time()
);
$this->upload_model->add_image($file);
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success', $data);
}
}
else
{
redirect(site_url('upload'));
}
}
function thumb($data)
{
$config['image_library'] = 'gd2';
$config['source_image'] =$data['full_path'];
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 160;
$config['height'] = 110;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
}
}
My upload_form.php view:
<?php $attributes = array('name' => 'myform');
echo form_open_multipart('/upload/do_upload',$attributes);?>
<input type="file" name="userfile" size="20" />
<input type="submit" value="upload" name="upload" />
<?php echo form_close(); ?>
My upload_model.php Model:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Upload_model extends CI_Model {
public function __construct()
{
parent::__construct();
}
function add_image($data)
{
$this->db->insert('jobs',$data);
}
}
I can't modify the function in a way that allows multiple image upload. I would highly appreciate any kind of guidance or help. Thank you in advance!
#####################
# Uploading 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;
what's happening in code??
well $_FILE---->it is an associative array of items uploaded to the current script via the POST method.for further look this LINK
it's an automatic variable avaliable within all scopes of 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;
}
and in your html markup don't don't forget:
Input name must be be defined as an array i.e. name="file[]"
Input element must have multiple="multiple" or just multiple
3.$this->load->library('upload'); //to load library
4.The callback, $this->upload->do_upload() will upload the file selected in the given field name to the destination folder.
5.And the callback $this->upload->data() returns an array of data related to the uploaded file like the file name, path, size etc.

trying to upload two images file_path in one column in codeigniter

I am trying to upload two images file_path in one column. In my model i am getting first image path successfully from $filepath but $a is not working i am also confused that $a is getting my second image path or or not
Kindly help me
Thanks in advance.
My Controller
<?php
class upload extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
}
function index()
{
$this->load->view("main_temp/header.php");
$this->load->view('post_ad_views', array('error' => ' ' ));
$this->load->view("main_temp/footer.php");
}
// controller
function do_upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$config['file_name'] = $new_file_name;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$this->load->view("main_temp/header.php");
$this->load->view('post_ad_views', $error);
$this->load->view("main_temp/footer.php");
}
else
{
// success
$data = array('upload_data' => $this->upload->data());
// a model that deals with your image data (you have to create this)
$this->load->model('submit_ad_model');
$this->submit_ad_model->ad_post();
$this->load->view('upload_success', $data);
}
}
}
?>
MY Model
<?php
class submit_ad_model extends CI_Model
{
function ad_post()
{
$filepath = $this->upload->data()['file_name'];
$a = $this->upload->data()['file_name'];
$this->db->query("insert into ads (ad_pic) values ('$filepath,$a')");
?>
My Views
<input type="file" name="userfile" class="upload image" id="userfile" />
<input type="file" name="userfile2" class="upload image" id="userfile" />
You arent looking to get the second uploaded item, you need to use a loop:
$upload_data = array();
foreach($_FILES as $key => $value){
$this->upload->do_upload($key);
$upload_data[$key] = $this->upload->data($key);
}
$this->submit_ad_model->ad_post($upload_data);

Categories