Codeigniter : You did not select a file to uplaod - php

following is the code that i am trying to upload file . and the error i get is
You did not select a file to upload.
Please help me with this how can i fix this
<?php echo form_open_multipart('garage/do_upload');?>
<input type="file" name="userfile" />
<input type="submit" value="upload" />
</form>
do_upload function
public function do_upload() {
$this->load->library('upload');
$files = $_FILES;
$cpt = count($_FILES['userfile']['name']);
for($i=0; $i<$cpt; $i++)
{
$_FILES['userfile']['name']= $files['userfile']['name'][$i];
$_FILES['userfile']['type']= $files['userfile']['type'][$i];
$_FILES['userfile']['tmp_name']= $files['userfile']['tmp_name'][$i];
$_FILES['userfile']['error']= $files['userfile']['error'][$i];
$_FILES['userfile']['size']= $files['userfile']['size'][$i];
$this->upload->initialize($this->set_upload_options());
if($this->upload->do_upload()){
echo 'Done';
die;
}
else {
print_r($this->upload->display_errors());
}
}
}
private function set_upload_options()
{
//upload an image options
$config = array();
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '10000000000000';
$config['overwrite'] = FALSE;
return $config;
}
i will use this code for multiple image uploads but curretly its not workign for single image , Kindly help

public function do_upload()
{
$error = NULL;
// check permission of upload_path
// I recommended that user fullpath
$this->load->library('upload', array(
'delete_origin' => TRUE,
'upload_path' => './uploads/',
'remove_spaces' => TRUE,
'file_ext_tolower' => TRUE,
'allowed_types' => 'jpg|png|jpeg',
'max_size' => '4096', // 1024 KB => 1 MB
'max_width' => '1920',
'max_height' => '1080',
));
if( ! $this->upload->do_upload('userfile') )
{
$error = $this->upload->display_errors();
}
else
{
$data = $this->upload->data();
}
return ( empty( $error ) ) ? $data : $error;
}

Related

Multiple image upload with CodeIgniter

I am new user to using code igniter in my project, I am facing one problem while uploading multiple files but the last one only insert to all image three images field.
my controller is:
function products()
{
date_default_timezone_set("Asia/Kolkata");
$config['upload_path'] = './resources/images/products/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 1000;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$this->load->library('upload', $config);
$this->upload->do_upload('userfile');
$data = array('prod_image' => $this->upload->data(),
'prod_image1' => $this->upload->data(),
'prod_image2' => $this->upload->data());
$product_image=$data['prod_image']['file_name'];
$product_image1=$data['prod_image1']['file_name'];
$product_image2=$data['prod_image2']['file_name'];
$data = array(
'name' => $this->input->post('pd_name'),
'prod_image' => $product_image,
'prod_image1' => $product_image1,
'prod_image2' => $product_image2,
'created_time' => date('Y-m-d H:i:s'));
// insert form data into database
$result_set= $this->tbl_products_model->insertUser($data);
}
my view part is:
<input class="form-control" name="pd_name"type="text"/>
<input type="file" class="file_upload2" name="userfile"/> //1
<input type="file" class="file_upload2" name="userfile"/> //2
<input type="file" class="file_upload2" name="userfile"/>//3
Please help how to insert 3 images.
my datad base like
===========================================
id|name|prod_image|prod_image1|prod_image2|
===========================================
1|ard| | | |
============================================
Html :
<input type="file" name="userfile[]" multiple="multiple">
PHP :
<?php
public function products()
{
$this->load->library('upload');
$dataInfo = array();
$files = $_FILES;
$cpt = count($_FILES['userfile']['name']);
for($i=0; $i<$cpt; $i++)
{
$_FILES['userfile']['name']= $files['userfile']['name'][$i];
$_FILES['userfile']['type']= $files['userfile']['type'][$i];
$_FILES['userfile']['tmp_name']= $files['userfile']['tmp_name'][$i];
$_FILES['userfile']['error']= $files['userfile']['error'][$i];
$_FILES['userfile']['size']= $files['userfile']['size'][$i];
$this->upload->initialize($this->set_upload_options());
$this->upload->do_upload('userfile');
$dataInfo[] = $this->upload->data();
}
$data = array(
'name' => $this->input->post('pd_name'),
'prod_image' => $dataInfo[0]['file_name'],
'prod_image1' => $dataInfo[1]['file_name'],
'prod_image2' => $dataInfo[2]['file_name'],
'created_time' => date('Y-m-d H:i:s')
);
$result_set = $this->tbl_products_model->insertUser($data);
}
private function set_upload_options()
{
//upload an image options
$config = array();
$config['upload_path'] = './resources/images/products/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '0';
$config['overwrite'] = FALSE;
return $config;
}
?>
Multiple files uploads unlimited files
Database table(profile_images) column names are image_name(255,varcher), added_datetime(current timestamp)
View
<?php echo validation_errors();?>
<?php echo form_open_multipart('pages/multiple_files');?>
<p><input type="file" multiple="multiple" name="image_name[]" class="form-control" /></p>
<input type="submit" class="btn btn-success btn-block"/>
</form>
Controller
public function multiple_files(){
$this->load->library('upload');
$image = array();
$ImageCount = count($_FILES['image_name']['name']);
for($i = 0; $i < $ImageCount; $i++){
$_FILES['file']['name'] = $_FILES['image_name']['name'][$i];
$_FILES['file']['type'] = $_FILES['image_name']['type'][$i];
$_FILES['file']['tmp_name'] = $_FILES['image_name']['tmp_name'][$i];
$_FILES['file']['error'] = $_FILES['image_name']['error'][$i];
$_FILES['file']['size'] = $_FILES['image_name']['size'][$i];
// File upload configuration
$uploadPath = './assets/images/profiles/';
$config['upload_path'] = $uploadPath;
$config['allowed_types'] = 'jpg|jpeg|png|gif';
// Load and initialize upload library
$this->load->library('upload', $config);
$this->upload->initialize($config);
// Upload file to server
if($this->upload->do_upload('file')){
// Uploaded file data
$imageData = $this->upload->data();
$uploadImgData[$i]['image_name'] = $imageData['file_name'];
}
}
if(!empty($uploadImgData)){
// Insert files data into the database
$this->pages_model->multiple_images($uploadImgData);
}
}
Model
public function multiple_images($image = array()){
return $this->db->insert_batch('profile_images',$image);
}
The problem is with the following line of code:
<input type="file" class="file_upload2" name="userfile"/> //1
<input type="file" class="file_upload2" name="userfile"/> //2
<input type="file" class="file_upload2" name="userfile"/>//3
These all three have same name.
To solve this there are two ways:
Give diff name to all 3 input type file
Make a single input type file with its multiple file selection true and its name must be an array like:
<input type="file" name="filefield[]" multiple="multiple">
Make the following changes and try again.
code of controller.
public function upload_multiple($field_name,$path){
$this->load->library('upload');
$files = $_FILES;
$cpt = count($_FILES[$field_name]['name']);//count for number of image files
$image_name =array();
for($i=0; $i<$cpt; $i++)
{
$_FILES[$field_name]['name']= $files[$field_name]['name'][$i];
$_FILES[$field_name]['type']= $files[$field_name]['type'][$i];
$_FILES[$field_name]['tmp_name'] = $files[$field_name]['tmp_name'][$i];
$_FILES[$field_name]['error']= $files[$field_name]['error'][$i];
$_FILES[$field_name]['size'] = $files[$field_name]['size'][$i];
$this->upload->initialize($this->set_upload_options($path));
//for initalizing configuration for each image
$this->upload->do_upload($field_name);
$data = array('upload_data' => $this->upload->data());
$image_name[]=$data['upload_data']['file_name'];
//store file name into array
}
return $image_name;//all images name which is uploaded
}
public function set_upload_options($path)
{
$config = array();
$config['upload_path'] = $path;
$config['allowed_types'] = '*';
$config['overwrite'] = FALSE;
return $config;
}
call function
$image_name=$this->upload_multiple('profile_image',$path=USER_OTHER);//we get all name of uploaded file in $image_name array.

how to upload path in codeigniter

I am new to CI. Currently I have the following:
$config['upload_path'] = './uploads/';
I just want to know how to update path in codeignitor. I have tried the below code. Is there something I'm doing wrong?
<?php
defined('BASEPATH')`enter code here` OR exit('No direct script access allowed');
class Upload extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
}
function index()
{
$this->load->view('upload_form', array('error' => ' ' ));
}
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';
$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 = array('upload_data' => $this->upload->data());
$this->load->view('upload_success', $data);
}
}
}
?>
From Your code Your file will be uploaded at uploads folder which is in your root directory. $config['upload_path'] = './uploads/'; this is the place where your uploaded files are stored.
You make an directory uploads there where an application folder is .
If you want your current code.
You'll need a destination folder for your uploaded images. Create a folder at the root of your CodeIgniter installation called uploads and set its file permissions to 777.
By default the upload routine expects the file to come from a form field called userfile.
from file uploading class
Use this library to upload... Its easy to use
http://demo.codesamplez.com/codeigniter/file-upload-demo
View
<form action="" method="POST" enctype="multipart/form-data" >
Select File To Upload:<br />
<input type="file" name="userfile" multiple="multiple" />
<input type="submit" name="submit" value="Upload" class="btn btn-success" />
</form>
{if isset($uploaded_file)}
{foreach from=$uploaded_file key=name item=value}
{$name} : {$value}
<br />
{/foreach}
{/if}
Controller
/**
* the demo for file upload tutorial on codesamplez.com
* #return view
*/
public function file_upload_demo()
{
try
{
if($this->input->post("submit")){
$this->load->library("app/uploader");
$this->uploader->do_upload();
}
return $this->view();
}
catch(Exception $err)
{
log_message("error",$err->getMessage());
return show_error($err->getMessage());
}
}
Component
/**
* Description of uploader
*
* #author Rana
*/
class Uploader {
var $config;
public function __construct() {
$this->ci =& get_instance();
$this->config = array(
'upload_path' => dirname($_SERVER["SCRIPT_FILENAME"])."/files/",
'upload_url' => base_url()."files/",
'allowed_types' => "gif|jpg|png|jpeg|pdf|doc|xml",
'overwrite' => TRUE,
'max_size' => "1000KB",
'max_height' => "768",
'max_width' => "1024"
);
}
public function do_upload(){
$this->remove_dir($this->config["upload_path"], false);
$this->ci->load->library('upload', $this->config);
if($this->ci->upload->do_upload())
{
$this->ci->data['status']->message = "File Uploaded Successfully";
$this->ci->data['status']->success = TRUE;
$this->ci->data["uploaded_file"] = $this->ci->upload->data();
}
else
{
$this->ci->data['status']->message = $this->ci->upload->display_errors();
$this->ci->data['status']->success = FALSE;
}
}
function remove_dir($dir, $DeleteMe) {
if(!$dh = #opendir($dir)) return;
while (false !== ($obj = readdir($dh))) {
if($obj=='.' || $obj=='..') continue;
if (!#unlink($dir.'/'.$obj)) $this->remove_dir($dir.'/'.$obj, true);
}
closedir($dh);
if ($DeleteMe){
#rmdir($dir);
}
}
}
$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('image name'))
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success', $data);
}

Multiple images resizing toupload folder in Codeiniter

I have this controller which inserts the images to upload folder without resizing them.
public Function Upload() {
$this->load->library('upload');
$config['upload_path'] = FCPATH . 'uploads/property-images';
$config['allowed_types'] = 'gif|jpg|png';
$config['file_name'] = 'property_image_1';
$config['max_size'] = '0';
$config['overwrite'] = FALSE;
for ($i = 0; $i < $number_of_files; $i++) {
$_FILES['uploadedimage']['name'] = $files['name'][$i];
$_FILES['uploadedimage']['type'] = $files['type'][$i];
$_FILES['uploadedimage']['tmp_name'] = $files['tmp_name'][$i];
$_FILES['uploadedimage']['error'] = $files['error'][$i];
$_FILES['uploadedimage']['size'] = $files['size'][$i];
$this->upload->initialize($config);
if ($this->upload->do_upload('uploadedimage', $i)) {
$data['uploadedimage'] = $this->upload->data();
$image_name[$i] = $data['uploadedimage']['file_name'];
//$this->_property_images_resize($data); // Private Function For Resize
$data['images'] = implode(',',$image_name);
$this->model_users->insert_property_details($data))
redirect('view');
} else {
$this->form_validation->set_message('fileupload_check', $this->upload->display_errors());
return FALSE;
}
}
}
I further needed to resize the images with the help of a private function in controller:
private function _property_images_resize($data) {
$this->load->library('image_lib');
$config = array(
'image_library' => 'gd2',
'source_image' => 'uploads/property-images/'.$data['uploadedimage']['file_name'],
'new_image' => 'uploads/profile/test1/',
'create_thumb' => TRUE,
'maintain_ratio' => TRUE,
'width' => 10,
'height' => 10
);
$this->image_lib->initialize($config);
if(!$this->image_lib->resize()) echo $this->image_lib->display_errors();
}
After adding this second function, it changes nothing. Images uploads the way they were before without resizing.
Looking for someone's help as can't figure it out.
For validation Callback
function if_image_was_selected(){
$this->form_validation->set_message('if_image_was_selected', 'Please select at least 1 image in jpg/jpeg/png/gif format.');
$number_of_files = count($_FILES['uploadedimages']['tmp_name']);
$files = $_FILES['uploadedimages'];
for($i=0;$i<$number_of_files;$i++) {
if($_FILES['uploadedimages']['error'][$i] != 0) {
return false;
}else{
return true;
}
}
}
Are you sure your not suppose to call:
$this->load->library('image_lib', $config);
instead of
$this->image_lib->initialize($config);
Does your webserver user have write permissions to the uploads/profile/test1/ directory?
Do you get any error message? Have you checked the PHP log or Apache log?
Try adding this code below, above your code to see the error echo'd out to the screen if you can.
error_reporting(E_ALL);
ini_set("display_errors", 1);

How to upload multiple files using codeigniter

I want to upload multiple files using codeigniter.
I have two types of images beforeimage and afterimage. After uploading each image I make entry in database.
I am creating thumbnail as well. To amke it ease I created a separate function, but its not working. this logic works for single image upload.
Html
controller
foreach($_FILES["beforepicture"]['name'] as $key=>$files){
//if(isset($_FILES["beforepicture"]['name']) && $_FILES["beforepicture"]['name'] != ''){
$imagestatus = $this->upload_images($files,'beforepicture');
if(isset($imagestatus['name'])){
$inputdata = array('image' => $imagestatus['name'],
'lead_id' =>$inserted_id,
'user_id'=>$this->session->userdata['userdata']['userid'],
'when'=>'before');
$this->common_model->save('lead_images',$inputdata);
//}
}
}
function upload_images($data = NULL,$inputname=NULL){
if($data){
$this->load->library('image_lib');
// echo '<pre>';
// print_r($_FILES);
$new_name = time().$data;
$config = array(
'upload_path' => getcwd()."/assets/themes/default/avatars/",
'allowed_types' => "gif|jpg|png|jpeg",
'overwrite' => TRUE,
'max_size' => "2048000", // Can be set to particular file size , here it is 2 MB(2048 Kb)
// 'max_height' => "768",
//'max_width' => "1024",
'file_name' => $new_name
);
$this->load->library('upload', $config);
$this->upload->initialize($config);
//$this->upload->initialize($config);
if($this->upload->do_upload($inputname))
{
$image_data = $this->upload->data();
//thumb1
$config = array(
'source_image' => $image_data['full_path'], //path to the uploaded image
'new_image' => getcwd()."/assets/themes/default/avatars/thumb", //path to
'maintain_ratio' => true,
'width' => 180,
'height' => 200
);
$this->image_lib->initialize($config);
$this->image_lib->resize();
$message = array('name' => $image_data['file_name']);
}else
{
//echo $this->upload->display_errors(); die;
$message = array('failed' => $this->upload->display_errors());
}
return $message;
}
}
I am trying but failed
Try this
<?php
class My_Controller extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->library('upload');
}
$files = $_FILES;
$cpt = count($_FILES['userfile']['name']);
for($i=0; $i<$cpt; $i++)
{
$_FILES['userfile']['name']= $files['userfile']['name'][$i];
$_FILES['userfile']['type']= $files['userfile']['type'][$i];
$_FILES['userfile']['tmp_name']= $files['userfile']['tmp_name'][$i];
$_FILES['userfile']['error']= $files['userfile']['error'][$i];
$_FILES['userfile']['size']= $files['userfile']['size'][$i];
$this->upload->initialize($this->set_upload_options());
$this->upload->do_upload();
$fileName = $_FILES['userfile']['name'];
$images[] = $fileName;
}
$fileName = implode(',',$images);
$this->my_model->upload_image($fileName);
}
private function set_upload_options()
{
// upload an image options
$config = array();
$config['upload_path'] = './upload/'; //give the path to upload the image in folder
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '0';
$config['overwrite'] = FALSE;
return $config;
}
}
For more how to upload multiple file in codeigniter try this tutorial:http://w3code.in/2015/09/upload-file-using-codeigniter/
Try this
$files = $_FILES;
$cpt = count($_FILES['beforepicture']['name']);
for($i=0; $i<$cpt; $i++){
$_FILES['beforepicture']['name']= $files['beforepicture']['name'][$i];
$_FILES['beforepicture']['type']= $files['beforepicture']['type'][$i];
$_FILES['beforepicture']['tmp_name']= $files['beforepicture']['tmp_name'][$i];
$_FILES['beforepicture']['error']= $files['beforepicture']['error'][$i];
$_FILES['beforepicture']['size']= $files['beforepicture']['size'][$i];
$imagestatus = $this->upload_images($_FILES["beforepicture"]['name'],'beforepicture');
if(isset($imagestatus['name'])){
$inputdata = array('image' => $imagestatus['name'],
'lead_id' =>$inserted_id,
'user_id'=>$this->session->userdata['userdata']['userid'],
'when'=>'before');
$this->common_model->save('lead_images',$inputdata);
}
}

Upload and Resize in CodeIgniter

I am uploading and re-sizing multiple images, but this code work good for 1 file if i upload 2 or more file it will re-size 1st pic as complete black and all other pics uploaded unchanged.
function do_upload()
{
$files = $_FILES;
$cpt = count($_FILES['userfile']['name']);
for($i=0; $i<$cpt; $i++)
{
$_FILES['userfile']['name']= $files['userfile']['name'][$i];
$_FILES['userfile']['type']= $files['userfile']['type'][$i];
$_FILES['userfile']['tmp_name']= $files['userfile']['tmp_name'][$i];
$_FILES['userfile']['error']= $files['userfile']['error'][$i];
$_FILES['userfile']['size']= $files['userfile']['size'][$i];
$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())
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
$path=$data['upload_data']['full_path'];
$q['name']=$data['upload_data']['file_name'];
$configi['image_library'] = 'gd2';
$configi['source_image'] = $path;
$configi['maintain_ratio'] = TRUE;
$configi['width'] = 75;
$configi['height'] = 50;
$this->load->library('image_lib', $configi);
$this->image_lib->resize();
$this -> load -> view('upload_success', $q);
}
}
}
where i am missing something, that re-size function is not working accurate.
Below this line
$path=$data['upload_data']['full_path'];
write
echo $path;
then check what is the output?
please try below
function do_upload(){
$this->load->library('upload');
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
foreach($_FILES['userfile'] as $key => $value)
{
if( ! empty($key['name']))
{
$this->upload->initialize($config);
if ( ! $this->upload->do_upload($key))
{
$error['error'] = $this->upload->display_errors();
$this->load->view('pages/uploadform', $error);
}
else
{
$data[$key] = array('upload_data' => $this->upload->data());
$this->load->view('pages/uploadsuccess', $data[$key]);
}
}
}
}
}

Categories