CodeIgniter Image Upload - can't get error message to show - php

This is my upload model
function upload_avatar()
{
$id = $this->tank_auth->get_user_id();
//config upload parameters and upload image
$config = array(
'allowed_types' => 'jpeg|jpg|png',
'upload_path' => $this->upload_path,
'max_size' => 2048,
'encrypt_name' => TRUE,
'overwrite' => FALSE,
);
$this->load->library('upload', $config);
$this->upload->do_upload();
//get upload data, config, resize uploaded image, save in avatars subfolder
$image_data = $this->upload->data();
if ($image_data['file_size'] < 2048) {
$config = array(
'source_image' => $image_data['full_path'],
'new_image' => $this->upload_path . '/avatars',
'maintain_ratio' => TRUE,
'width' => 125,
'height' => 125
);
$this->load->library('image_lib', $config);
$this->image_lib->resize();
//only burn avatar path to user_profiles table if no upload errors
if (!$this->upload->display_errors()) {
$data = array('avatar' => base_url() . 'images/avatars/' . $image_data['file_name']);
$this->db->where('id', $id);
$this->db->update('user_profiles', $data);
}
//delete the original file from server
$this->load->helper('file');
unlink($image_data['full_path']);
} else {
echo $this->upload->display_errors();
}
}
I can't get the error message to echo straight to the browser when I try uploading a file > 2MB.
To be fair, CI ignores this large file, and uploads correctly when a file is < 2MB.
The only thing is that I can't get the error message to show on the front-end to give the suer some feedback.
Any ideas what's wrong here?

$config['upload_path'] = 'uploads/category/'.$id.'/';
//echo $file_name;die;
//echo $config['upload_path'];
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = '2048';
$config['max_width'] = '1920';
$config['max_height'] = '1280';
$this->load->library('upload');
foreach ($_FILES as $key => $value) {
//print_r($key);
if (!empty($key['name'])) {
$this->upload->initialize($config);
if (!$this->upload->do_upload($key)) {
// echo 'test';die;
// rmdir('uploads/category/'.$id);
$errors = $this->upload->display_errors();
flashMsg($errors);
}
}
}
try this!!

Is your post_max_size limit less than 2MB? (http://ca3.php.net/manual/en/ini.core.php#ini.post-max-size) If so the file may have been discarded before your code is invoked.
Update:
If you take out your function call in the else block, and just drop in an exit('too big'); are you able to see errors then? If so there may be an issue with how you're pasing the call off.

Related

resizing image in codeigniter php not working correctly

I am trying to resize multiple images using codeigniter, my file upload code in controller looks like below:
$this->load->library('upload');
$image = array();
$ImageCount = count($_FILES['pimage']['name']);
for($i = 0; $i < $ImageCount; $i++){
$_FILES['file']['name'] = $_FILES['pimage']['name'][$i];
$_FILES['file']['type'] = $_FILES['pimage']['type'][$i];
$_FILES['file']['tmp_name'] = $_FILES['pimage']['tmp_name'][$i];
$_FILES['file']['error'] = $_FILES['pimage']['error'][$i];
$_FILES['file']['size'] = $_FILES['pimage']['size'][$i];
// File upload configuration
$uploadPath = './uploads/products/';
$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[] = $imageData['file_name'];
$this->resizeImage($imageData['file_name']);
}
}
public function resizeImage($filename)
{
$source_path = './uploads/products/' . $filename;
$target_path = './uploads/products/';
$config_manip = array(
'image_library' => 'gd2',
'source_image' => $source_path,
'new_image' => $target_path,
'maintain_ratio' => TRUE,
'width' => 500,
);
$this->load->library('image_lib', $config_manip);
if (!$this->image_lib->resize()) {
echo $this->image_lib->display_errors();
}
$this->image_lib->clear();
}
Here I have two issues, first one is only the first image is getting resized, multiple images are not. Second thing is: if portrait image is uploaded its turned into landscape after conversion. Can anyone please tell me what is wrong in here, thanks in advance

when is using uploader system in codeigniter the system say Call to a member function site_url() on array

This is a very simple file uploading with CI but the system says functions site_url() or base_url() can't be used in original file Helper Codeigniter
$this->config = array('upload_path' => dirname($_SERVER["SCRIPT_FILENAME"])."/images/",
'upload_url' => base_url()."images/",
'allowed_types' => "png|jpg",
'overwrite' => TRUE,
'max_size' => "10000KB",
'max_height' => "7680",
'max_width' => "10240",
'file_name' => "about"
);
$this->load->library('upload', $this->config);
if($this->upload->do_upload('logo'))
{
$this->session->set_flashdata('success_upload', 'You logo has been uploaded' );
}
else
{
$this->session->set_flashdata('error_upload', 'Can\'t Upload this file, Please try agine.' );
}
Use this code it will helps you.
$obj = &get_instance();
$obj->load->library('upload');
$file_name = "about." . pathinfo($_FILES['logo']['name'], PATHINFO_EXTENSION); //userrandom file name instead of about
$full_path = FCPATH . 'images/';
$config['upload_path'] = $full_path;
$config['allowed_types'] = 'gif|jpg|png|jpeg|GIF|JPG|PNG|JPEG';
$config['max_size'] = 2048;
$config['max_width'] = 2048;
$config['max_height'] = 2048;
$config['file_name'] = $file_name;
$obj->upload->initialize($config);
if (!$obj->upload->do_upload('logo')) {
$this->session->set_flashdata('error_upload', $obj->upload->display_errors());
return $obj->upload->display_errors();
} else {
$this->session->set_flashdata('success_upload', 'You logo has been uploaded');
return $obj->upload->data();
}
don't use dirname() for file path .
use FCPATH it will gile you absolute path from root like "/var/www/html/project_name"
upload_url is not a key in codeiginter upload library
use random file name because if you upload another image it will override or create file new file like file_name(1).extension
As per your this comment I think you have to load the URL helper first.
$this->load->helper('url');
Or you can load it in autoload.php
$autoload['helper'] = array('url');
To Upload the image try this code:
$config['upload_path'] = './images/';
$config['allowed_types'] = 'png|jpg';
$config['max_size'] = 10000;
$config['max_width'] = 10240;
$config['max_height'] = 7680;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('userfile'))
{
$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);
}
}

codeigniter file is not uploading on remote server

this is a code for swf fiel uploading but the problem is it works fine on localhost but not in the remote server.here is my controller
function do_upload()
{
$pid=$this->input->post('Page_id');
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'swf';
$config['max_size'] = '1048';
$config['file_name'] =$pid;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
}
else{
$data=array(
'id'=>$pid,
'link'=>base_url()."uploads/",
'file_name'=>$config['file_name']
);
$this->file_upload_model->upload_data($data);
$this->upload_success();
}
}
i have not done anything except just uploading the path and file name to the database in model. see the demo here
Try this
$pid=$this->input->post('Page_id');
$config['file_name'] = $pid;
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'swf';
$config['max_size'] = '1048';
$config['remove_spaces'] = TRUE;
$this->load->library('upload', $config);
if ($this->upload->do_upload())
{
//upload successful
//do something
}
else
{
$this->session->set_flashdata('error',$this->upload->display_errors());
redirect('controller/method');
/*
In the view file you can echo the error like this
echo $this->session->flashdata('error');
*/
}
You have pass the the field name in the do_upload call like this:
$config = array(
'allowed_types' => 'jpg|jpeg|swf |png', // pipe seperated file types
'upload_path' => './uploads/', // should be the root path
'max_size' => '1048',
'file_name' => $pid
);
$this->load->library('upload', $config);
if (!$this->upload->do_upload('your_field_name')) {
// like $this->upload->do_upload($pid)
$error = array('error' => $this->upload->display_errors());
} else {
$swf_data = $this->upload->data();
}
Hope it makes sense
it may be due to PHP server version and it's option.
1).go to cpanel account
2).click "select php version", in the software section.
3).tap the "fileinfo" chexbox in the php option and save.
now you can upload file perfectly.

Categories