Have the Form field like this
<form>
Image 1 <input type='file' name="userfile">
Image 2 <input type='file' name="userfile1"/>
</form>
controller function for upload image 1
public function profile_imageupload()
{
$random_no = rand();
$filename = date('YmdHis')."_".$random_no;
$config = array(
'upload_path' =>'./uploads/original/',
'allowed_types' => 'gif|jpg|png',
'max_size' =>'10000',
'max_width' =>'2000',
'max_height' =>'2000',
'file_name' =>$filename
);
$this->load->library('upload', $config);
$this->upload->initialize($config);
$this->load->library('image_lib');
if ( ! $this->upload->do_upload()){
$error = array('error' => $this->upload->display_errors());
}else{
$data = array('upload_data' => $this->upload->data());
$path_info = pathinfo($data['upload_data']['full_path']);
$fileExtension = $path_info['extension'];
return $config['file_name'].".".$fileExtension;
}
}
In this only one image upload (first image) second image not upload in folder.
How to insert second in another location
Try this
<form>
Image 1 <input type='file' name="userfile[]">
Image 2 <input type='file' name="userfile[]"/>
</form>
public function profile_imageupload()
{
$config['upload_path'] = 'upload/';
$path=$config['upload_path'];
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = '1024';
$config['max_width'] = '1920';
$config['max_height'] = '1280';
$this->load->library('upload');
foreach ($_FILES as $fieldname => $fileObject) //fieldname is the form field name
{
if (!empty($fileObject['name']))
{
$this->upload->initialize($config);
if (!$this->upload->do_upload($fieldname))
{
$errors = $this->upload->display_errors();
flashMsg($errors);
}
else
{
// Code After Files Upload Success GOES HERE
}
}
}
}
Related
I am new to CodeIgniter. This is what I want to do:
Resize the product image into 350*250, 50*50 and origional image while uploading the image in add product from admin.
All these three images in the three different folders.
Here is my code-
public function products()
{
$config=array();
$config['upload_path']="./uploads/";
$config['allowed_types']="jpg|jpeg|gif|png";
$this->load->library('upload',$config);
$this->upload->initialize($config);
if ( ! $this->upload->do_upload('userfile'))
{
$error = array('error' => $this->upload->display_errors());
}
else
{
$file_data = $this->upload->data();
$data1['img']=base_url().'images/'.$file_data['file_name'];
$data_image = array(
'agenda_id' => $this->input->post('agenda_id'),
'file' => $file_data['file_name']
);
$config['image_library']='gd2';
$config['source_image']='./uploads/'.$file_data["file_name"];
$config['create_thumb']=FALSE;
$config['maintain_ratio']=FALSE;
$config['quality']='60%';
$config['width']=350;
$config['height']=250;
$config['new_image']='./uploads350/'.$file_data["file_name"];
$this->upload->initialize($config);
$this->load->library('image_lib',$config);
$this->image_lib->resize();
}
$this->load->model("Admin_model");
$data= array(
"Type" =>$this->input->post("Type"),
"Brand" =>$this->input->post("Brand"),
"Product_Name" =>$this->input->post("Product_Name"),
"Price" =>$this->input->post("Price"),
"Image"=>$data_image['file']
);
$this->Admin_model->admin($data);
$this->session->set_flashdata('message', 'Product added successfully');
redirect('Admin/product_list');
}
This should work fine (haven't tested). Just make sure the folders exist. Also, if I were you I'd probably keep everything in the same folder and just prepend the size afterwords like some_image_50.jpg:
public function products() {
$config = array();
$config['upload_path'] = "./uploads/";
$config['allowed_types'] = "jpg|jpeg|gif|png";
$this->load->library('upload', $config);
$this->upload->initialize($config);
if (!$this->upload->do_upload('userfile')) {
$error = array('error' => $this->upload->display_errors()); // do something with this...
// e.g. show_error($this->upload->display_errors());
} else {
$file_data = $this->upload->data();
$data1['img'] = base_url() . 'images/' . $file_data['file_name'];
$data_image = array(
'agenda_id' => $this->input->post('agenda_id'),
'file' => $file_data['file_name']
);
//$config['image_library'] = 'gd2'; (default; not needed)
$this->load->library('image_lib');
$cfg['source_image'] = './uploads/' . $file_data["file_name"];
$cfg['create_thumb'] = FALSE;
$cfg['maintain_ratio'] = FALSE;
$cfg['quality'] = '60%';
$cfg['width'] = 350;
$cfg['height'] = 250;
$cfg['new_image'] = './uploads350/' . $file_data["file_name"];
//$this->upload->initialize($config); why??
$this->image_lib->initialize($cfg);
$this->image_lib->resize();
// the following vars will override certain vars above and keep the ones we haven't overridden
$cfg['width'] = 50;
$cfg['height'] = 50;
$cfg['new_image'] = './uploads50/' . $file_data["file_name"]; // make sure folder exists
$this->image_lib->initialize($cfg);
$this->image_lib->resize();
}
$this->load->model("Admin_model");
$data = array(
"Type" => $this->input->post("Type"),
"Brand" => $this->input->post("Brand"),
"Product_Name" => $this->input->post("Product_Name"),
"Price" => $this->input->post("Price"),
"Image" => $data_image['file']
);
$this->Admin_model->admin($data);
$this->session->set_flashdata('message', 'Product added successfully');
redirect('Admin/product_list');
}
I had somehow managed to upload and resize multiple images, everything was fine.
Then i wanted to show errors if no image was selected to upload.
I put a if statement to load the error but that's causing a havoc.
Each time i select images and upload, the last one gets duplicated, without resizing.
Again, if i remove the if else, it's perfectly fine.
Thankyou for any help and your time.
function do_upload(){
$path = array();
$count = count($_FILES['userfile']['size']);
foreach($_FILES as $key=>$value){
for($n=0; $n<=$count-1; $n++) {
$_FILES['userfile']['name']=$value['name'][$n];
$_FILES['userfile']['type'] = $value['type'][$n];
$_FILES['userfile']['tmp_name'] = $value['tmp_name'][$n];
$_FILES['userfile']['error'] = $value['error'][$n];
$_FILES['userfile']['size'] = $value['size'][$n];
$config['upload_path'] = './images';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$this->load->library('upload', $config);
$this->upload->do_upload();
$data = $this->upload->data();
$path[] = $data['full_path'];
}
if(!$this->upload->do_upload()){
$error = array('error' => $this->upload->display_errors());
$this->load->view('view_dashboard_error_car', $error);
}
else{
$this->load->library('image_lib');
foreach($path as $p=>$ath){
$config1 = array(
'source_image' => $ath,
'new_image' => './images',
'maintain_ration' => true,
'overwrite' => true,
'width' => 600,
'height' => 400
);
$this->image_lib->initialize($config1);
$this->image_lib->resize();
$this->image_lib->clear();
}
$this->load->view('view_dashboard_success_car');
}
}
}
You're running $this->upload->do_upload() twice, because your foreach() runs and then your if/else statement runs.
You need to put the if/else into your foreach loop. You should take care not to just copy/paste it in, but make sure you're only running $this->upload->do_upload() once per image.
I'm trying to set a site up so I can upload images using a web form in Code Igniter(CI). I'm not getting any errors but the file is not being saved either. I wanted to know if those that were successful in uploading images could help explain what might be the issue?
View:
<?php
echo form_open_multipart('admin/galleryUpload') . "\n";
echo "<div class='span-8'><span class='text'>Image:</span>" . form_upload('uploadImg') . "</div>";
foreach ($gallery as $picture)
{
$order[] = $picture->order;
}
$order[] = count($order) + 1;
echo "<div class='span-6 last'><span>Image Order #:</span>" . form_dropdown('order', $order) . "</div><div class='span-14'> </div>";
$conf = array('name' => 'alt_text', 'size' => '75');
echo "<div class='span-14 last'><span>Discription:</span>" . form_input($conf) . "<br /></div>";
echo form_hidden('propertyID', "$propertyID");
echo form_submit('upload', 'Upload');
echo form_close();
?>
Controller:
class Admin extends Controller
{
function galleryUpload()
{
if (! $this->session->userdata('is_admin'))
{
redirect('admin/index');
}
else
{
$this->load->model('admin_model');
$this->admin_model->imgUpload();
}
}
}
Model:
class Admin_model extends Model
{
function imgUpload()
{
$id = $this->input->post('propertyID');
$order = $this->input->post('order');
$alt_text = $this->input->post('alt_text');
$config = array(
'allowed_types' => 'jpg|jpeg|gif|png',
//'upload_path' => '../' . $this->imgPath($id),
'upload_path' => '../img/galleries/temp/',
'max_size' => '5000', // 5MB files max
);
$this->load->library('upload', $config);
$this->upload->do_upload();
$image_data = $this->upload->data();
$config = array(
'source_image' => $image_data['full_path'],
'new_image' => $this->imgPath($id) . '/thumbs',
'maintain_ratio' => TRUE,
'width' => '60'
);
$this->load->library('image_lib', $config);
$this->image_lib->resize();
}
}
From the user guide:
By default the upload routine expects the file to come from a form
field called userfile, and the form must be a multipart type
So you either have to change the name of the form field to userfile:
form_upload('userfile')
or pass the name of your form field to do_upload:
$this->upload->do_upload('uploadImg');
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 11 years ago.
I have been stuck at this point for a long time and even after reading a number of posts, I haven't been able to find a solution.
I am making an interface for a quiz, and in the admin interface, I need to upload images to a folder and upload the image name to the database. I can handle to other things but the image upload is bugging me alot for a long time.
Please take a look at my code below.
The Form
<?php echo form_open_multipart('admin/update_question'); ?>
<?php echo form_hidden('questionid', $question->level); ?>
<?php echo form_label('Comment', 'comment'); ?>
<div class="input">
<?php
$arr_comment = array(
'name' => 'comment',
'id' => 'comment',
'value' => $question->comment
);
echo form_textarea($arr_comment);
?>
</div>
<br/>
<?php echo form_label('Answer', 'answer'); ?>
<div class="input">
<?php
$arr_answer = array(
'name' => 'answer',
'id' => 'answer',
'size' => '10000',
'value' => $question->answer
);
echo form_input($arr_answer);
?>
</div>
<br/>
<?php echo form_label('Image', 'userfile'); ?>
<div class="input">
<?php
$arr_image = array(
'name' => 'userfile',
'id' => 'userfile',
'value' => ''
);
echo form_upload($arr_image);
?>
</div>
<br/>
<?php
$arr_button = array(
'name' => 'submit',
'value' => 'Update Question',
'class' => 'btn primary large'
);
?>
<div class="input">
<?php echo form_submit($arr_button); ?>
</div>
<br/>
<?php
echo form_close();
if ($error != '')
echo '<div class="alert-message error">'. $error.' </div>';
echo validation_errors();
?>
I have tried running a js which returns the filename in the upload box, and it does return it.
The Controller
public function update_question() {
$comment = $this->input->post('comment');
$answer = $this->input->post('answer');
echo var_dump(is_dir(base_url().'uploads/'));
/*
* Uploading image
*/
$config['upload_path'] = base_url().'/uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_width'] = 0;
$config['max_height'] = 0;
$config['max_size'] = 0;
$config['encrypt_name'] = TRUE;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
print_r($error);
}
else
{
$arr_image = array('upload_data' => $this->upload->data());
print_r($arr_image);
}
$questionid = $this->input->post('questionid');
$question_data = array(
'comment' => $comment,
'answer' => $answer
);
$this->load->model('adminmodel', 'admin');
$question_data = $this->admin->update_question_data($questionid, $question_data);
//redirect('admin/questions', 'location');
}
For my upload path, I have tried a number or combinations and the only one which returned a TRUE value was var_dump(is_dir('/wamp/www/quark_edorado/uploads')); but this also returned the same error.
I do not know where I am going wrong.
Update
My directory structure is
/application/
/public/
css/
fonts/
images/
js/
/system/
/uploads/
I am operating a Windows machine and using WAMP. Does that make a difference?
I was autoloading the library and somehow when I was trying to initialize the configuration by $this->load->library('upload', $config); it wouldn't do so.
Instead I put my config files in config/upload.php
The other method to do so would have been $this->upload->initialize($config);
Ok put your upload folder in on the original place as you show in your question and try this (give the relative path to your base_url)
$config['upload_path'] = 'uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_width'] = 0;
$config['max_height'] = 0;
$config['max_size'] = 0;
$config['encrypt_name'] = TRUE;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload()) {
$error = array('error' => $this->upload->display_errors());
print_r($error);
} else {
$arr_image = array('upload_data' => $this->upload->data());
print_r($arr_image);
}
if ( ! $this->upload->do_upload())
try adding the file field name
if ( ! $this->upload->do_upload('userfile'))
i have a problem while uploading and resizing images in a loop.
can anyone please provide me the working sample of code of codeigniter for uploading and resizing at the same time in a loop.
I want to upload and resize images uploaded from the form. There will be more than 1 images so i have to upload them in loop.
My code first uploads the image then resizes it. 1ts images is uploaded and resized correctly but during 2nd loop the image is uploaded but not resized. It throws this error:
Your server does not support the GD
function required to process this type
of image.
I have tried the clear function too
$this->image_lib->clear();
can anyone please help
Dont load image_lib multiple times. Add image_lib in autoload libs and change
$this->load->library('image_lib', $config);
to
$this->image_lib->initialize($config);
I had the same problem but this seemed to work:
// Do upload
if (! $this->upload->do_upload($image_name)) {
// return errors
return array('errors' => $this->upload->display_errors());
}
$data = $this->upload->data();
$config_manip = array(
'image_library' => 'gd2',
'source_image' => "./assets/uploads/{$data['file_name']}",
'new_image' => "./assets/uploads/thumbs/{$data['file_name']}",
'create_thumb' => true,
'thumb_marker' => '',
'maintain_ratio' => true,
'width' => 140,
'height' => 140
);
// Create thumbnail
$this->load->library('image_lib');
$this->image_lib->resize();
$this->image_lib->clear();
$this->image_lib->initialize($config_manip);
if ( ! $this->image_lib->resize()){
return array('errors' => $this->image_lib->display_errors());
}
Notice the Create Thumbnail goes:
Load Library,
Resize Image,
CLEAR,
Initialize Config
I was putting the clear after the initialize which was causing the same error you're getting.
Here is a working code from my image gallery controller. This function uploads a batch of images, resizes them and saves them to database.
public function create_photo_batch() {
$this->load->library('image_lib');
$this->load->library('upload');
// Get albums list for dropdown
$this->data['albums'] = $this->gallery_m->get_albums_list();
if(isset($_FILES['images']) && $_FILES['images']['size'] > 0) {
$album_id = $this->input->post('albumid');
$images = array();
$ret = array();
// Upload
$files = $_FILES['images'];
foreach ($files['name'] as $key => $image) {
$_FILES['images[]']['name']= $files['name'][$key];
$_FILES['images[]']['type']= $files['type'][$key];
$_FILES['images[]']['tmp_name']= $files['tmp_name'][$key];
$_FILES['images[]']['error']= $files['error'][$key];
$_FILES['images[]']['size']= $files['size'][$key];
$upload_config = array(
'allowed_types' => 'jpg|jpeg|gif|png',
'upload_path' => realpath(APPPATH . "../uploads/gallery"),
'max_size' => 5000,
'remove_spaces' => TRUE,
'file_name' => md5(time())
);
$this->upload->initialize($upload_config);
if ($this->upload->do_upload('images[]')) {
$image_data = $this->upload->data();
$images[] = $image_data['file_name'];
} else {
$this->session->set_flashdata('error', $this->upload->display_errors() );
redirect('admin/gallery/create_photo_batch');
}
}
// Resize
foreach ($images as $image) {
$resize_config = array(
'source_image' => realpath(APPPATH . '../uploads/gallery') .'/'. $image,
'new_image' => realpath(APPPATH . '../uploads/gallery/thumbs'),
'maintain_ratio' => TRUE,
'width' => 500,
'height' => 500
);
$this->image_lib->initialize($resize_config);
if ( ! $this->image_lib->resize() ) {
echo $this->image_lib->display_errors();
die;
}
$this->image_lib->clear();
}
// Save to db
foreach ($images as $image) {
$ret[] = array(
'AlbumID' => (int) $album_id,
'Url' => $image,
'IsActive' => 1
);
}
$this->gallery_m->save_images($ret);
redirect('admin/gallery/album/'.$album_id);
}
//Load view
$this->data['subview'] = 'admin/gallery/photo_upload_batch_view';
$this->load->view('admin/_layout_main', $this->data);
}
Your error message suggest that it's not the loop that is the problem, but rather that the 2nd file is of a different filetype than the 1st. And that the underlying server don't have the needed libraries (http://www.libgd.org/Main_Page) installed to handle that file type.
$config['image_library'] = 'gd2';
$config['source_image'] = './assets/upload_images/A.jpg';
$config['create_thumb'] = FALSE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 1600;
$config['height'] = 900;
$config['new_image'] = './assets/upload_images/'.$randy;
$this->load->library('image_lib', $config);
$this->image_lib->initialize($config);
$this->image_lib->resize();
$this->image_lib->clear();
I had same problem but solved these by this code.