how to upload different files (images & documents) in codeigniter - codeigniter

hi i'm new to codeigniter, can you please help me in insert multiple files(documents and images) in code igniter. here's my sample code
view:
<label>Picture</label>
<input type="file" name="userfile" size="100" />
<label>Document</label>
<input type="file" name="documentfile" size="10" />
controller:
$m = $_FILES['userfile']['name'];
$n = $_FILES['documentfile']['name'];
if ($m !== "")
{
$config['upload_path'] = './upload_images/';
$config['allowed_types'] = 'jpg|png|jpeg|gif';
$config['max_size'] = '0'; // 0 = no file size limit
$config['max_width'] = '0';
$config['max_height'] = '0';
$config['overwrite'] = TRUE;
$this->load->library('upload', $config);
$this->upload->do_upload();
$upload_result = $this->upload->data();
}
elseif ($n !== "")
{
$config_document['upload_path'] = './upload_documents/';
$config_document['allowed_types'] = 'pdf';
$config_document['max_size'] = '0';
$config_document['overwrite'] = TRUE;
$this->load->library('upload', $config_document);
$this->upload->do_upload();
$upload_result2 = $this->upload->data();
}
$image_filename = $upload_result['file_name'];
$docu_filename = $upload_result2['file_name '];
$this->MODEL->add_asset($image_filename, $docu_filename);
i tried to echo both file names and it works but my $docu_filename generates NULL value;
please help. thank you

hi its simple just check the file extension of uploaded file and make settings according to that. one more thing you have to set your html form enctype also. check the following example
form View
<form method="post" action="controller" enctype="multipart/form-data">
<input type="file" name="test">
<input type="submit" value="submit" />
</form>
in controller
$path = $_FILES['test']['name'];
$ext = pathinfo($path, PATHINFO_EXTENSION);
$img_ext_chk = array('jpg','png','gif','jpeg');
$doc_ext_chk = array('pdf','doc');
if (in_array($ext,$img_ext_chk))
{
$config['upload_path'] = './upload_images/';
$config['allowed_types'] = 'jpg|png|jpeg|gif';
$config['max_size'] = '0'; // 0 = no file size limit
$config['max_width'] = '0';
$config['max_height'] = '0';
$config['overwrite'] = TRUE;
$this->load->library('upload', $config);
$this->upload->do_upload();
$upload_result = $this->upload->data();
}
elseif (in_array($ext,$doc_ext_chk))
{
$config_document['upload_path'] = './upload_documents/';
$config_document['allowed_types'] = 'pdf';
$config_document['max_size'] = '0';
$config_document['overwrite'] = TRUE;
$this->load->library('upload', $config_document);
$this->upload->do_upload();
$upload_result2 = $this->upload->data();
}

Related

codeigniter upload images from different inputs

my code is working fine in the single upload, but I want to make a loop for my code that I won't repeatedly create a function over and over because of using two different inputs.
public function uploadImage_1()
{
$config['upload_path'] = './uploads';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = '2608';
$config['max_width'] = '2608';
$config['max_height'] = '2608';
$this->load->library('upload', $config);
if ( !$this->upload->do_upload('userfile1')){
$error = array('error' => $this->upload->display_errors());
}else{
$fileName = $this->upload->data();
$post_image = $fileName['file_name'];
return $post_image;
}
}
public function uploadImage_2()
{}
` View
<input type="file" name="userfile1" size="20" required/>
<input type="file" name="userfile2" size="20" required/>`
You can pass the input field name as parameter into your function.
like this:
public function uploadImage($input_field_name)
{
$config['upload_path'] = './uploads';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = '2608';
$config['max_width'] = '2608';
$config['max_height'] = '2608';
$this->load->library('upload', $config);
if ( !$this->upload->do_upload($input_field_name)){
$error = array('error' => $this->upload->display_errors());
}else{
$fileName = $this->upload->data();
$post_image = $fileName['file_name'];
return $post_image;
}
}
Now use the above function like this:
// These variables stores the name of your files
$fileName_1 = $this->uploadImage('userfile1');
$fileName_2 = $this->uploadImage('userfile2');

codeigniter upload files from multiple input

i want to upload files with multiple input. I have tried the code below, but the files not uploaded, and i cant get the upload directory.
HTML
<input type="file" name="userfile[]" size="20" />
Controller
public function detailTugasInput($idKelas,$id){
$this->load->model('tugass');
$records = $_FILES['userfile'];
//var_dump($records); die();
$config['upload_path'] = './file/';
$config['allowed_types'] = 'pdf|doc|txt|jpg|png';
$config['max_size'] = 0;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$this->load->library('upload', $config);
foreach ($_FILES as $key => $value) {
$upload_data = $this->upload->data();
$file_ary = array(
'nama_kembali' => $value['name'],
'file_kembali' => $value['tmp_name']
);
}
}
And i also tried to get the uploaded data with this code below, but why always appear error?
$checked_arr = $_POST['userfile'];
Try Below Codes
HTML :
<input type="file" class="form-control" name="userfile[]" multiple />
Controller :
public function detailTugasInput($idKelas, $id){
$this->load->model('tugass');
if(!empty($_FILES['userfile']['name'])) {
$filesCount = count($_FILES['userfile']['name']);
for($i = 0; $i < $filesCount; $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'] = './file/';
$config['allowed_types'] = 'pdf|doc|txt|jpg|png';
$this->load->library('upload', $config);
$this->upload->initialize($config);
$this->upload->do_upload('userFile');
if($this->upload->do_upload('userFile')){
$fileData = $this->upload->data();
$uploadData[$i]['file_name'] = $fileData['file_name'];
$uploadData[$i]['created'] = date("Y-m-d H:i:s");
$uploadData[$i]['modified'] = date("Y-m-d H:i:s");
}
}
}
}
For more Support, Check This

CI multiple file upload - cannot upload more than 2 files

This function cannot upload more than 2 images. If tried produces error,
Message : undefined index:userfile
View
<input name="userfile[]" id="userfile" type="file" multiple="" />
Controller
function do_upload() {
$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';
$config['max_size'] = 0;
$this->load->library('upload', $config);
$this->upload->do_upload();
$data = $this->upload->data();
}
}
}
You should definitely look into your php.ini max post/upload size. Look for values like:
; Maximum allowed size for uploaded files.
upload_max_filesize = ##M
; Must be greater than or equal to upload_max_filesize
post_max_size = ##M
and change the # to values that will suit you

codeigniter image resize not working

can't understand why this image resizing not working pls help
//updating article
function updateArticle(){
$data = array(
'a_title' =>$_POST['a_title'],
'a_description' =>$_POST['a_description'],
'a_flash_news' => $_POST['a_flash_news'],
'a_content' =>$_POST['a_content'],
//'a_views' => $_POST['a_views'],
'a_image_caption' =>$_POST['a_image_caption'],
'a_audio_caption' =>$_POST['a_audio_caption'],
'a_video' =>$_POST['a_video'],
'a_video_caption' =>$_POST['a_video_caption'],
'a_channel' =>$_POST['a_channel'],
'a_grouping' =>$_POST['a_grouping'],
'a_status' =>$_POST['a_status'],
'a_breaking' =>$_POST['a_breaking'],
'a_hot' =>$_POST['a_hot'],
'a_category_id' =>$_POST['a_category_id'],
'a_featured' =>$_POST['a_featured'],
'a_tags' =>$_POST['a_tags'],
'a_author' =>$_POST['a_author'],
'a_date' =>$_POST['a_date']
);
//UPLOAD IMAGE
//some $config vars for image
$config['upload_path'] = './images/articles';
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = '0';
$config['remove_spaces'] = true;
$config['overwrite'] = false;
$config['max_width'] = '0';
$config['max_height'] = '0';
//for image resize
$config['image_library'] = 'gd2';
$config['maintain_ratio'] = TRUE;
$config['width'] = 320;
$config['height'] = 320;
$this->load->library('upload', $config);
$this->load->library('image_lib', $config);
$this->image_lib->resize();
//upload main image
if(!$this->upload->do_upload('a_image')){
//$e = $this->upload->display_errors();
//print_r($e);
}
$image = $this->upload->data();
if($image['file_name']){
$data['a_image'] = "images/articles/". $image['file_name'];
}
//UPLOAD THUMBNAIL
unset($config);
//now upload thumb
//some $config vars for thumb
$config['upload_path'] = './images/articles/thumb';
$config['allowed_types'] = 'gif|jpg|jpeg|png|wav';
$config['max_size'] = '0';
$config['remove_spaces'] = true;
$config['overwrite'] = false;
$config['max_width'] = '0';
$config['max_height'] = '0';
$this->upload->initialize($config);
if(!$this->upload->do_upload('a_thumbnail')){
//$e = $this->upload->dispaly_errors();
//print_r($e);exit();
}
$thumb = $this->upload->data();
if($thumb['file_name']){
$data['a_thumbnail'] = "images/articles/thumb/". $thumb['file_name'];
}
//UPLOAD AUDIO
unset($config);
//now upload thumb
//some $config vars for thumb
$config['upload_path'] = './audio';
$config['allowed_types'] = 'mp3|gif|jpg|jpeg|png|wav';
$config['max_size'] = '0';
$config['remove_spaces'] = true;
$config['overwrite'] = false;
$config['max_width'] = '0';
$config['max_height'] = '0';
$this->upload->initialize($config);
if(!$this->upload->do_upload('a_audio')){
//$e = $this->upload->dispaly_errors();
//print_r($e);exit();
}
$thumb = $this->upload->data();
if($thumb['file_name']){
$data['a_audio'] = "audio/". $thumb['file_name'];
}
//goes at last
$this->db->where('id',$_POST['id']);
$this->db->update('articles', $data);
}
what error are you getting?
have the upload paths 777 permissions?
I think if you need to rezise the images you need to set 777 permissions on the upload path.

Uploading an image in codeigniter

I am trying to upload an image,create thumbnail but i get an error.
Here is my controller.
<?php
class Upload extends Controller {
function Upload()
{
parent::Controller();
$this->load->helper(array('form','url','file'));
}
function index()
{
$this->load->view('upload_form'); //Upload Form
}
function picupload()
{
//Load Model
$this->load->model('Process_image');
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '2048'; //2 meg
$this->load->library('upload');
foreach($_FILES as $key => $value)
{
if( ! empty($key['name']))
{
$this->upload->initialize($config);
if ( ! $this->upload->do_upload($key))
{
$errors[] = $this->upload->display_errors();
}
else
{
$this->Process_image->process_pic();
}
}
}
$data['success'] = 'Thank You, Files Upladed!';
$this->load->view('upload_success', $data); //Picture Upload View
}
}
?>
My model:
<?php
class Process_image extends Model {
function Process_image()
{
parent::Model();
$this->load->library('image_lib');
//Generate random Activation code
function generate_code($length = 10){
if ($length <= 0)
{
return false;
}
$code = "";
$chars = "abcdefghijklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ123456789";
srand((double)microtime() * 1000000);
for ($i = 0; $i < $length; $i++)
{
$code = $code . substr($chars, rand() % strlen($chars), 1);
}
return $code;
}
}
function process_pic()
{
//Connect to database
$this->load->database();
//Get File Data Info
$uploads = array($this->upload->data());
$this->load->library('image_lib');
//Move Files To User Folder
foreach($uploads as $key[] => $value)
{
//Gen Random code for new file name
$randomcode = generate_code(12);
$newimagename = $randomcode.$value['file_ext'];
//Creat Thumbnail
$config['image_library'] = 'GD2';
$config['source_image'] = $value['full_path'];
$config['create_thumb'] = TRUE;
$config['thumb_marker'] = '_tn';
$config['master_dim'] = 'width';
$config['quality'] = 75;
$config['maintain_ratio'] = TRUE;
$config['width'] = 175;
$config['height'] = 175;
$config['new_image'] = '/pictures/'.$newimagename;
//$this->image_lib->clear();
$this->image_lib->initialize($config);
//$this->load->library('image_lib', $config);
$this->image_lib->resize();
//Move Uploaded Files with NEW Random name
rename($value['full_path'],'/pictures/'.$newimagename);
//Make Some Variables for Database
$imagename = $newimagename;
$thumbnail = $randomcode.'_tn'.$value['file_ext'];
$filesize = $value['file_size'];
$width = $value['image_width'];
$height = $value['image_height'];
$timestamp = time();
//Add Pic Info To Database
$this->db->set('imagename', $imagename);
$this->db->set('thumbnail', $thumbnail);
$this->db->set('filesize', $filesize);
$this->db->set('width', $width);
$this->db->set('height', $height);
$this->db->set('timestamp', $timestamp);
//Insert Info Into Database
$this->db->insert('pictures');
}
}
}
?>
The error:
A PHP Error was encountered
Severity: Warning
Message: rename(C:/wamp/www/uploads/Heaven_Clouds.jpg,/pictures/kFttl7lpE7Rk.jpg) [function.rename]: No such file or directory
Filename: models/Process_image.php
Line Number: 68
This is line 68:
rename($value['full_path'],'/pictures/'.$newimagename);
Remove the "/" before "picutres" in
rename($value['full_path'],'/pictures/'.$newimagename);
It wanna say that you want put your renamed file in a directory named "pictures" placed at the root of an Unix file system, then you're obviously in a Windows system and you do not seem to have a "pictures" directory at the root of your disk.
result :
rename($value['full_path'],'pictures/'.$newimagename);
This is a very simple script taken partly from the CI Docs:
$config['upload_path'] = 'uploads/cgm/';
$config['allowed_types'] = 'gif|jpg|png|bmp|jpeg';
$config['max_size'] = '0';
$config['max_width'] = '0';
$config['max_height'] = '0';
$this->load->library('upload', $config);
$configThumb = array();
$configThumb['image_library'] = 'gd2';
$configThumb['source_image'] = '';
$configThumb['create_thumb'] = TRUE;
$configThumb['maintain_ratio'] = TRUE;
$configThumb['width'] = 100;
$configThumb['height'] = 120;
$this->load->library('image_lib');
for($i = 1; $i < 6; $i++) {
$upload = $this->upload->do_upload('file'.$i);
if($upload === FALSE) continue;
$data = $this->upload->data();
$uploadedFiles[$i] = $data;
$imgName = $this->pictures_m->addPicture(array('listing_id' => $listing_id, 'ext' => $data['file_ext'], 'picture_name' => $this->input->post('file'.$i.'name')));
if($data['is_image'] == 1) {
$configThumb['source_image'] = $data['full_path'];
$configThumb['new_image'] = $data['file_path'].$imgName.$data['file_ext'];
$this->image_lib->initialize($configThumb);
$this->image_lib->resize();
}
rename($data['full_path'], $data['file_path'].$imgName.$data['file_ext']);
}
this will take 5 images, but if you only have one, you can just change the for loop.
i had the same problem but, in my scenario i've to only upload the array of files so i did some small changes to the library 'Upload.php'
view
<form encrypt="multipart/form-data" ...>
<input type="file" name="your_name[]" />
<input type="file" name="your_name[]" />
<input type="submit" />
</form>
controller
for($i = 0; $i < count($_FILES['your_name']['name']); $i++) {
$config['upload_path'] = 'your upload path';
$this->upload->initialize($config);
$this->upload->do_upload('listing_images', $i);
endfor;
if you have single file to upload then duplicate the function in system/libraries/Upload.php
function do_upload($field) to function do_upload_array($field, $i)
put [$i] index on lines 160, 162, 196, 197
and
function _file_mime_type($_FILES[$field]) to function _file_mime_type_array($_FILES[$field], $i)
and put [$i] index on lines 1026, 1043, 1057 and 1065
thats it...
your file array will upload easily now....

Resources