Image file uploading issue in codeigniter

codeigniter

#1

Hii developers, I am facing issue in codeigniter when I uploading a image file in my database, and then, I check in database there in image column show 0. I also visit on stack-overflow and do research about this but i didn’t get any solution.

What is i want?
I don’t want make my file upload text-box as mandatory but I want if text-box is empty then there will show as default image.

so that, I want instead 0 which save in database there save some my custom text to I can make custom path of image to make it as default.

site-point community is my last hopes please any one here who the know the solution’s please let me give the answer.

my full code is below:

 /*
    * This function is invoked from another function to upload the image into the assets folder
    * and returns the image path
    */
	public function upload_image()
    {
    	// assets/images/product_image
        $config['upload_path'] = 'assets/images/product_image';
        $config['file_name'] =  uniqid();
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '1000';

        // $config['max_width']  = '1024';s
        // $config['max_height']  = '768';

        $this->load->library('upload', $config);
        if ( ! $this->upload->do_upload('product_image'))
        
        {
            $error = $this->upload->do_upload('product_image');
            return $error;
        }
        else
        {
            $data = array('upload_data' => $this->upload->data());
            $type = explode('.', $_FILES['product_image']['name']);
            $type = $type[count($type) - 1];
            
            $path = $config['upload_path'].'/'.$config['file_name'].'.'.$type;
            return ($data == true) ? $path : false;            
        }
    }

I thinks the issue might solve from below:

 $this->load->library('upload', $config);
        if ( ! $this->upload->do_upload('product_image'))
        
        {
            $error = $this->upload->do_upload('product_image');
            return $error;
        }

#3

#4

#5

I have had problems with Codeigniter when the supplied examples do not do exactly what I want them to do so…

Check the differences in the function/method return values between uploading a successful image and another with an image with problems or without an image.

The script used to check the return result differences:


$tmp = $this->upload->do_upload('product_image');
echo '<pre>'; // adds linefeeds for easier reading
  print_r( $tmp );
echo '</pre>';
die; // halts program execution
// copy and paste good and bad results to a text file to compare results

Beware: Please note the above script was tapped on my tablet and not tested.


#6

First, I think it would be nice to know what kind of columns your database has currently and if you are actually inserting the data into your database. If you haven’t then there’s actually a much simpler way if you aren’t too picky about it being strictly CodeIgniter or not.


#7

Thank you sir, thank you for your help but I just jumb PHP and Codeigniter that is why I not understand what and where , how i edit you code in my file. Please could you paste my code with exact correct method.?


#8

Try this and copy and paste the results then compare a successful upload with an upload that has problems.

public function upload_image()
{
// assets/images/product_image
    $config['upload_path'] = 'assets/images/product_image';
    $config['file_name'] =  uniqid();
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size'] = '1000';

// $config['max_width']  = '1024';s
// $config['max_height']  = '768';

// FIRST DEBUG STARTS HERE
if(FALSE): // CHANGE T0 TRUE FOR ORIGINAL SCRIPT 
    // ORIGINAL SCRIPT
    $this->load->library('upload', $config);
else:
    // DEBUG SCRIPT
    $tmp = $this->upload->do_upload('product_image');
    echo '<pre>'; // adds linefeeds for easier reading
      print_r( $tmp );
    echo '</pre>';
    die; // halts program execution
endif;    
// DEBUG ENDS HERE


    if ( ! $this->upload->do_upload('product_image'))
    {
        $error = $this->upload->do_upload('product_image');
        return $error;
    }
    else
    {
// SECOND DEBUG STARTS HERE
        if(FALSE): // CHANGE T0 TRUE FOR ORIGINAL SCRIPT 
            $data = array('upload_data' => $this->upload->data());
            $type = explode('.', $_FILES['product_image']['name']);
            $type = $type[count($type) - 1];
        
            $path = $config['upload_path'].'/'.$config['file_name'].'.'.$type;
            return ($data == true) ? $path : false;            
         else:
           // DEBUG SCRIPT STARTS HERE
            $tmp = array('upload_data' => $this->upload->data());
            echo '<pre>'; // adds linefeeds for easier reading
              print_r( $tmp );
            echo '</pre>';
            die; // halts program execution
         endif;   
// SECOND DEBUG ENDS HERE
    }
}


#9

#10

its not works and show error 500


#11

@gettingStuck

its not works and show error 500

Try remming the script I supplied and see if you can pinpoint the error.

I think a http 500 error is usually additional script that fails when Php tries to render the web page. I usually rem large chunks of a script to pinpoint the problem.

It is late here and I will try to replicate your problem tomorrow.


#12

I downloaded and installed CodeIgniter CI_VERSION 3.1.9 and followed the following Tutorial:

https://www.codeigniter.com/user_guide/libraries/file_uploading.html?highlight=file%20upload

As mentioned the following two views are required:

  1. ./views/upload_form.php
  2. ./views/upload_success.php

Also the upload image directory was created assets/uploads/.

Please note the image directory must have the writable file permissions set to 0777 otherwise the images will not save.

Here is the Controller with DEBUG modifications. I advise getting the Upload Tutorial working first because it is easier to make single changes to suit your requirements.

Keep making changes until the application stops working.

Try removing the // and activate the fred(…) function to see changes.

Source Code:

<?php
// file: ./controllers/Upload.php - CASE-SENSITIVE

// REMOVE FOLLOWING THREE LINES AFTER DEBUGGING
   declare(strict_types=1);
   error_reporting(-1);
   ini_set('display_errors', 'true');

defined('BASEPATH') OR exit('No direct script access allowed');

//=========================================================
//=========================================================
class Upload extends CI_Controller
{


//=========================================================
public function __construct()
{
	parent::__construct();
	$this->load->helper(array('form', 'url'));
}

//=========================================================
public function index()
{
	$this->load->view('upload', array('error' => ' ' ));
}

//=========================================================
public function do_upload()
{
// assets/images/product_image
// $config['upload_path'] 		= 'assets/images/product_image';
   $config['upload_path']    = './assets/uploads/';
   $config['file_name'] 			=  uniqid();
   $config['allowed_types'] 	= 'gif|jpg|png';
   $config['max_size'] 			= '1000';

// $config['max_width']  = '1024';
// $config['max_height']  = '768';
	// fred($config, __line__);
	// fred($config, __line__);
  $tmp = $this->load->library('upload', $config);
	// fred($tmp, __line__);
	if ( ! $tmp = $this->upload->do_upload('userfile'))
  {
	// fred($tmp, __line__);
  	$error = $this->upload->do_upload('product_image');
		// var_dump( $error );  	
		// fred( $error, __line__);;	
    
    return $error;

  }else{
		// fred(__line__, __line__);;	
    $data = array('upload_data' => $this->upload->data());
    // fred($data, '$data');
    # $type = explode('.', $_FILES['product_image']['name']);
		# $type = explode('.', "$_FILES['name']" );
    # $type = $type[count($type) - 1];

    $type = $_FILES['userfile']['type'];    
    $path = $config['upload_path']
    			.	'/'
    			.	$config['file_name']
    			.	'.'
    			.	$type
    			;

 		$this->load->view('upload_success', $data);

    return ($data == true) ? $path : false;            
  }
}//

}///

//=========================================================
function fred($val='no $val', $title='no $title???')
{
	echo '<div>';
		echo '<pre style="width:88%; margin:1em auto;background-color:#ffc;padding:1em;">';
			echo '<hr>file ==>  ' .__file__ .'<br><br>';
			echo '<b>function fred(...) </b><hr>';
			echo '$title ==> ' .$title .'<br>';
			print_r($val);
		echo '</pre>';
	echo '</div>';
}//