Multi image uploader using CodeIgniter - image

I am a new user to using code igniter in my project, I am facing one problem while uploading multiple files but only function image uploading images and save into the database and function sliderimage is not uploading images or save into the database?
Controller
public function manage($id = FALSE) {
$data = array();
if ($id) {
$this->{$this->model}->{$this->_primary_key} = $id;
$data['item'] = $this->{$this->model}->get();
if (!$data['item'])
show_404();
} else {
$data['item'] = new Std();
}
$this->load->library("form_validation");
$this->form_validation->set_rules("image", 'Image', "trim|callback_image[$id]");
$this->form_validation->set_rules("sliderimage", 'sliderimage', "trim|callback_image[$id]");
if ($this->form_validation->run() == FALSE)
$this->load->view($this->module . '/manage', $data);
else {
$this->{$this->model}->save();
redirect('admin/' . $this->module);
}
}
public function image($var, $id) {
$config['upload_path'] = './cdn/sliders/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$this->load->library('upload', $config);
if ($this->upload->do_upload('image')) {
$data = $this->upload->data();
if ($data['file_name'])
$this->{$this->model}->image = base_url() . '/cdn/sliders/' . $data['file_name'];
}
return true;
}
public function sliderimage($var, $id) {
$config['upload_path'] = './cdn/sliders/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$this->load->library('upload', $config);
if ($this->upload->do_upload('sliderimage')) {
$data = $this->upload->data();
if ($data['file_name'])
$this->{$this->model}->sliderimage = base_url() . '/cdn/sliders/' . $data['file_name'];
}
return true;
}
Html Form
<form role="form" class="form-horizontal" role="form"
method="post" enctype="multipart/form-data">
<input class="form-control" type="file" name="image" >
<input class="form-control" type="file" name="sliderimage">
</form>

insted of calling both time image function call:
$this->form_validation->set_rules("sliderimage", 'sliderimage', "trim|callback_sliderimage[$id]");

Related

Laravel upload multiple files

I'm building a gallery with multiple files functionality.
So far I'm having two issues, but let's paste my code first.
Controller:
public function store(Request $request)
{
$gallery = new GalleryImage();
if($request->hasFile('image')) {
$images = $request->file('image');
foreach($images as $image) {
$path = $image->getClientOriginalName();
$name = time() . '-' . $path;
$gallery->image = $image->move(public_path().'/uploads/temp/', $name);
//$gallery->image = $request->file('image')->storeAs('public/gallery-images', $name);
}
}
$gallery->gallery_id = $request->gallery_id;
$gallery->save();
return back()->with('success_message','Images has been uploaded!');
}
View blade:
<form action="{{ route('gallery-images.store') }}" method="POST" enctype="multipart/form-data">
{{ csrf_field() }}
<div class="form-group">
<input type="hidden" name="gallery_id" value="{{ $gallery->id }}">
<input type="file" name="image[]" id="id_imageGallery" class="form-control" multiple="multiple" required>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Upload</button>
</div>
</form>
My first issue when I have my code like this and upload three files, it's successfully storing the files into /uploads/temp directory, but in my database I can see there's only one image uploaded, instead of three.
My second issue is when I change my code and use the commented part (because I want to store those three images into Storage):
$gallery->image = $request->file('image')->storeAs('public/gallery-images', $name);
I'm getting this error:
Call to a member function storeAs() on array
How can I upload those multiple images into the Storage folder and record them all into the database?
-- EDIT --
I've solved my first issue!
I've simply put the save method and other details inside the loop. I've should have thought about this before :)
Here's my updated code:
public function store(Request $request)
{
if($request->hasFile('image')) {
$images = $request->file('image');
foreach($images as $image) {
$gallery = new GalleryImage();
$path = $image->getClientOriginalName();
$name = time() . '-' . $path;
$gallery->image = $image->move(public_path().'/uploads/temp/', $name);
//$gallery->image = $request->file('image')->storeAs('public/gallery-images', $name);
$gallery->gallery_id = $request->gallery_id;
$gallery->save();
}
}
return back()->with('success_message','Images has been uploaded!');
}
Now only thing to do is how to store them into File Storage, instead of the public_path and avoid that error:
Call to a member function storeAs() on array
I've finally solved the issues and I'm posting it, so it may be useful for some.
Here's my code:
public function store(Request $request)
{
if($request->hasFile('image')) {
foreach($request->image as $image) {
$path = $image->getClientOriginalName();
$name = time() . '-' . $path;
$gallery = new GalleryImage();
$gallery->image = $image->storeAs('public/gallery-images', $name);
$gallery->gallery_id = $request->gallery_id;
$gallery->save();
}
}
return back()->with('success_message','The images have been uploaded!');
}
Try to implement using the following code, it will give uploaded files url in serialized format
public static function upload_file($file){
$file_name = time();
$file_name .= rand();
$file_name = sha1($file_name);
if ($file) {
$ext = $file->getClientOriginalExtension();
$file->move(public_path() . "/uploads", $file_name . "." . $ext);
$local_url = $file_name . "." . $ext;
$s3_url = url('/').'/uploads/'.$local_url;
return $s3_url;
}
return "";
}
public static function upload_multiple_files($files){
if(is_array($files)){
$return_array = array();
foreach ($files as $file){
if(!empty($file)){
$return_array[] = self::upload_file($file);
}else{
$return_array[] = '';
}
}
return serialize($return_array);
}else{
return NULL;
}
}
//I have updated and commented your code. Try it, It should work.
public function store(Request $request)
{
if($request->hasFile('image')) {
$images = $request->file('image');
foreach($images as $image) {
$gallery = new GalleryImage();
// laravel auto manages unique name and extension of file.
$gallery->image = $image->store('gallery-images', 'public');
$gallery->gallery_id = $request->gallery_id;
$gallery->save();
}
}
return back()->with('success_message','Images has been uploaded!');
}

I can not upload a file in codeigniter

I cannot upload a file in codeigniter. Code I'm using:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Main extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->library('session');
$this->load->library('upload');
$this->load->library('form_validation');
$this->load->helper(array('form', 'url'));
$this->load->model('User_m');
}
public function form()
{
if($_POST){
//print_r($_POST);exit;
if(!empty($_FILES['userfile']['name']))
{
$config['upload_path'] = "./uploads/";
$config['allowed_types'] = 'jpg|jpeg|png|gif';
$config['file_name'] = $_FILES['userfile']['name'];
//Load upload library and initialize configuration
//print_r($config);exit;
$this->load->library('upload',$config);
//$this->upload->initialize($config);
if($this->upload->do_upload('userfile')){
$uploadData = $this->upload->data();
$userfile = $uploadData['file_name'];
//echo $userfile;exit;
}
else
{
$userfile = '';
}
}
else
{
$userfile = '';
}
$data['name']=$this->input->post('name');
$data['email']=$this->input->post('email');
$data['phone']=$this->input->post('phone');
$data['userfile']=$userfile;
$this->User_m->form_insert($data);
}
$this->load->view('form');
}
}
This is my controller function code for processing multipart form data in codeigniter. But file is not uploading.
as you have given code i have modified code to trace it.Here below i have attached the view file as well as controller file :
1) Below is a view file code.In this i have submitted form by giving method name in action attribute.If you are submitting the form through ajax then you can pass the method name over there.
<div id="container">
<h1>Please Fill up the Registration Form.!</h1>
<div id="body">
<!-- <?php echo $error;?> -->
<form method="post" action="<?php echo site_url() . '/Main/form' ?>" enctype="multipart/form-data"> Full name:<br> <input type="text" name="name"><br> Email:<br> <input type="text" name="email"><br> Phone:<br> <input type="text" name="phone"><br><br> Image:<br> <input type="file" name="userfile" /> <br /><br /> <input type="submit"><br> </form>
</div>
</div>
2) Below is a controller code.
<?php
class Main extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url','html'));
}
public function index()
{
$this->load->view('form');
}
public function form()
{
if($_POST){
if(!empty($_FILES['userfile']['name']))
{
$config['upload_path'] = "./uploads/";
$config['allowed_types'] = 'jpg|jpeg|png|gif';
$config['file_name'] = $_FILES['userfile']['name'];
//Load upload library and initialize configuration
//print_r($config);exit;
$this->load->library('upload',$config);
//$this->upload->initialize($config);
if($this->upload->do_upload('userfile')){
$uploadData = $this->upload->data();
$userfile = $uploadData['file_name'];
//echo $userfile;exit;
}
else
{
$userfile = '';
}
}
else
{
$userfile = '';
}
$data['name']=$this->input->post('name');
$data['email']=$this->input->post('email');
$data['phone']=$this->input->post('phone');
$data['userfile']=$userfile;
// Below $data getting all the form data
print_r($data);
}
$this->load->view('form');
}
}
Hope this will help you!
You can try this
public function form()
{
if($_POST){
if(!empty($_FILES['userfile']['name']))
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'jpg|jpeg|png|gif';
$config['file_name'] = $_FILES['userfile']['name'];
$this->upload->initialize($config);
$this->load->library('upload',$config);
if($this->upload->do_upload('userfile')){
$userfile = $_FILES['userfile']['name'];
}
else
{
$userfile = '';
}
}
else
{
$userfile = '';
}
$data['name']=$this->input->post('name');
$data['email']=$this->input->post('email');
$data['phone']=$this->input->post('phone');
$data['userfile']=$userfile;
$this->User_m->form_insert($data);
}

Get file path of file uploaded using dropzone

How to get the file path of a file uploaded using dropzone in CodeIgniter?
My View page is
<form action="<?php echo site_url('Upload/imageUploadPost');?>" class="dropzone" id="my-awesome-dropzone" enctype="multipart/form-data" method="post">
<div class="fallback">
<input name="userfile" type="file"/>
</div>
</form>
My Controller is
public function imageUploadPost()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 1024;
$this->load->library('upload', $config);
$this->upload->do_upload('file');
//here I need to fetch the uploaded file details like name ,path
}
Hope this will help you :
Use $this->upload->data(); after upload successful to get the file details, Your method should be like this ::
public function imageUploadPost()
{
//print_r($_FILES);die;
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 1024;
$this->load->library('upload', $config);
if ($this->upload->do_upload('userfile'))
{
$data = array('upload_data' => $this->upload->data());
/* for full file path use $data['full_path'] and so like this */
$file_path = $data['full_path'];
echo $file_path ;
var_dump($data);
die();
}
else
{
$error = array('error' => $this->upload->display_errors());
var_dump($error);
die();
}
}
For more : https://www.codeigniter.com/user_guide/libraries/file_uploading.html

Codeigniter Modular HMVC extension - cant upload file

I have a problem with uploading files by form. Everytime I try, I get the error "File type not allowed". I am using HMVC modular extension and my controllers extends MX_Controller.
I already tried to $var_dump($_FILES) from my form-view and the result is fine, but when I try to use method do_upload() in controller it doesnt work.
Does anyone have any idea? Here is the controller:
Class Store_data extends MX_Controller
{
function __construct()
{
parent::__construct();
}
function upload_pic()
{
//$data['item_id'] = $item_id;
$template = "bootstrap_theme";
//$current_url = current_url();
//$data['form_location'] = $current_url;
$data['view_file'] = "upload_pic";
$this->load->module('template');
$this->template->$template($data);
}
function do_upload()
{
//var_dump($_FILES);
$config['upload_path'] = './images/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '10000';
$config['max_width'] = '4024';
$config['max_height'] = '4768';
$this->load->library('upload', $config);
$this->upload->overwrite = true;
$this->upload->initialize($config);
if ( ! $this->upload->do_upload())
{
$data['error'] = $this->upload->display_errors();
$template = "bootstrap_theme";
$data['view_file'] = "upload_pic";
$this->load->module('template');
$this->template->$template($data);
}
else
{
$data['upload_data'] = $this->upload->data();
$template = "bootstrap_theme";
$data['view_file'] = "upload_success";
$this->load->module('template');
$this->template->$template($data);
}
}
And here is the upload form view:
<?php echo $error;?>
<?php echo form_open_multipart('store_data/do_upload');?>
<input type="file" name="userfile" size="20" />
<br /><br />
<input type="submit" value="upload" />
</form>
I solved the problem.
I had to uncomment extension=php_fileinfo.dll in php.ini and comment ;extension=php_mime_magic.dll
Thanks anyway

CodeIgniter - Write file names to database from multiple images

I have an upload form with two file upload fields. I am trying to upload the files, and then write the file names to the same row in a database. I have everything working except the file names get written to their own row. I am guessing it's because I am making two separate calls to the model, but I am not sure how to go about doing it in one call since they each have their own "if" statement.
To make my question clear. How do I write the names of two uploaded files to the same row in the database?
Any ideas would be great. This is the code I am working with. Thanks.
View:
<?php echo form_open_multipart('upload_controller'); ?>
<p>
<input type="file" name="userfile">
</p>
<p>
<input type="file" name="userfile1">
</p>
<p><?php echo form_submit('submit', 'Upload them files!') ?></p>
<?php form_close() ?>
Controller:
if (isset($_POST['submit'])) {
$this->load->library('upload');
if (!empty($_FILES['userfile']['name'])) {
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->upload->initialize($config);
// Upload file 1
if ($this->upload->do_upload('userfile')) {
$img = $this->upload->data();
$file_name = $img['file_name'];
$this->load->model('upload_file', 'upload_model');
$this->upload_model->upload_file1($file_name);
}
}
if (!empty($_FILES['userfile1']['name'])) {
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->upload->initialize($config);
// Upload file 2
if ($this->upload->do_upload('userfile1')) {
$img = $this->upload->data();
$file_name = $img['file_name'];
$this->load->model('upload_file', 'upload_model');
$this->upload_model->upload_file2($file_name);
} else {
echo $this->upload->display_errors();
}
}
} else {
$this->load->view("upload_form");
}
Model:
public function upload_file1($file_name) {
$data = array('file1' => $file_name);
$this->db->insert('images_table', $data);
}
public function upload_file2($file_name) {
$data = array('file2' => $file_name);
$this->db->insert('images_table', $data);
}
Database row:
| id | file1 | file2 |
Try like this(if you have the number of files....in your case it is "2" )
$this->load->model('upload_file', 'upload_model');
for($i=1;$i<=$numimg;$i++) //in your case $numimg = 2;
{
if($this->upload->do_upload('File'.$i)) //File1,File2 are filenames
{
$img_data = $this->upload->data();
$images[$i] = $img_data['file_name'];
}
}
$data['Image'] = implode(',',$images);
$this->upload_model->upload_file($data);
at your model:
public function upload_file2($data)
{
$data = array('file_name' => $data['Image']);
$this->db->insert('images_table', $data);
}
That's it.If you want to retrive it from database use
$images = explode(',',$data->file_name);
it will give you an array of image names.

Resources