0

I've been trying to upload images to specific folder my scenario is I've upload controller in which I've do_upload function

   public function do_upload($field_name) {
        $field_name = 'image_title';
        $page_id = $this->input->post('page_id');
        $config = array(
            'allowed_types' => '*',
            'max_size' => '1024',
            'max_width' => '1024',
            'max_height' => '768',
            'upload_path' => './uploads/'. $page_id
        );
        $this->load->library('upload');
        $this->upload->initialize($config);
if (!is_dir('uploads'))
    {
        mkdir('./uploads/', 0777, true);
    }
$dir_exist = true; // flag for checking the directory exist or not
if (!is_dir('uploads/' . $page_id))
    {
        mkdir('./uploads/' . $page_id, 0777, true);
        $dir_exist = false; // dir not exist
    }
    else{
    }
        if (!$this->upload->do_upload($field_name)) {
            if(!$dir_exist)
          rmdir('./uploads/' . $page_id);
            $this->data['error'] = array('error' => $this->upload->display_errors());
        } else {

            $fInfo = $this->upload->data($field_name);
            $this->_createThumbnail($fInfo['file_name']);
            return $fInfo;
        }
 }
 /**********************************************************************************************/
    function _createThumbnail($filename)

    {

        $config['image_library']    = "gd2";      
        $config['source_image']     = "uploads/" .$filename;      
        $config['create_thumb']     = TRUE;      
        $config['maintain_ratio']   = TRUE;      
        $config['width'] = "80";      
        $config['height'] = "80";

        $this->load->library('image_lib',$config);

        if(!$this->image_lib->resize())

        {

            echo $this->image_lib->display_errors();

        } 

I've index view file given as

<section>
<?php echo validation_errors(); ?>
<?php //echo form_open_multipart('admin/upload/index/' . ((isset($page->id)) ? $page->id : '' ));   ?> 
<?php echo form_open_multipart('admin/upload/index/'); ?> 
<tr>
    <td><h3>Upload Images</h3></td>
</tr>
<table class="table table-striped">    
    <tr>
    <thead>
        <tr>
            <th>Image Name</th>
            <th>View</th>
            <th>Delete</th>
        </tr>
    </thead>
    <tbody>
        <?php if (count($images)): foreach ($images as $image): ?>
                <tr>
                    <td><?php echo anchor('admin/upload/index/' . $image->id, $image->image_title); ?></td>
                    <td><?php echo btn_edit('admin/upload/index/' . $image->id); ?></td>
                    <td><?php echo btn_delete('admin/upload/index/' . $image->id); ?></td>
                </tr>
            <?php endforeach; ?>
        <?php else: ?>
            <tr>
                <td colspan="3">There is no Image to display</td>
            </tr>
        <?php endif; ?>
        </tbody>
</table>
</section>
<section>
<table class="table table-striped">
    <tbody>
        <tr>
            <td><h5>Select Page</h5></td>
            <td><?php echo form_dropdown('page_id', $get_with_images, $this->input->post('page_id')); ?></td>

        </tr>
        <tr>
            <td><h5>Select Image</h5></td>
            <td>
                <?php echo form_upload('image_title', set_value('image_title', $image->image_title)); ?>
            </td> 
        </tr>
        <tr>
            <td><?php echo form_submit('submit', 'Upload', 'class="btn btn-success"'); ?></td>

        </tr>

</tbody>
</table>

<?php echo form_close(); ?>
</section>

dropdown is populating from page controller what actually I needed is I want to upload image to the page selected from dropdown for example if "Contact" page is selected from dropdown I want to upload my image to "Contact" page and on the backend I want to create directory with the same name as selected in dropdown in my case I want "uploads/home/abc.jpg" and same for the other pages please advice the basic idea I will modify it with my own

Hassan
  • 303
  • 2
  • 8
  • 25

1 Answers1

1

You're already sending your pageid value as post. Use it at uploading config.

...
$config = array(
   ...
   'upload_path' => './upload/' . $this->input->post('page_id')
)

You need to check if directory exists first, use isdir() PHP function, and if false, mkdir(), also PHP, with folder name and permission.

I'm not familiar anymore with CodeIgniter to tell you if there is some way to get written value of a <option> tag, but I would suggest to create a array with page ids and their respective name and look for the array's index by post value:

$page_name = [
   0 => 'contact',
   1 => 'home',
   2 => 'user',
   ...
]

...
$config = array(
   ...
   'upload_path' => './upload/' . $page_name[$this->input->post('page_id')]
)

EDIT: User had problems with thumb creation

With your update, you forgot to update also path of the thumbnail. So you must send the folder and filename:

function _createThumbnail($folder, $filename)

{

    $config['image_library']    = "gd2";      
    $config['source_image']     = "./uploads/" $folder . "/" .$filename; 
    $config['create_thumb']     = TRUE;      
    $config['maintain_ratio']   = TRUE;      
    $config['width'] = "80";      
    $config['height'] = "80";

    $this->load->library('image_lib',$config);

    if(!$this->image_lib->resize())

    {

        echo $this->image_lib->display_errors();

    }

Then you should call it as you were, replacing the parameters correctly:

$this->_createThumbnail($page_id, $fInfo['file_name']);
Fabiano Araujo
  • 876
  • 6
  • 19
  • as suggested by you I should use $page_name array for page_id and Page_Name but my pages are dynamically generating via page controller I mean I can add new pages so they will have new id but this page_name array is static choice how should I accomodate the scenario – Hassan May 19 '14 at 20:50
  • But if pages are dynamically generated, you would have to edit this array every time a new page is created. Instead to refer by page name, I suggest you to use a prefix like: page-$id. Following the first example, would be something: `'upload_path' => './upload/page-' . $this->input->post('page_id')`. You would have folders `page-1`, `page-2`, `page-45`, ... – Fabiano Araujo May 19 '14 at 20:56
  • Now I've edited my do_upload function you can check the new version ,previously after successful upload I use to have two files under uploads folder 1 abc.jpg 2: abc_thumb.jpg but now I am successful in creating folder with ID as its name i.e uploads/123/abc.jpg but its not creating any abc_thumb.jpg anymore also I am getting the following error The path to the image is not correct. Your server does not support the GD function required to process this type of image. don't know what to do and last thing I am not doing any kind of resizing of images – Hassan May 19 '14 at 22:00
  • Thumb creation is actually a resize of image. If removing `$this->_creatThumbnail($fInfo['file_name'])` fix it, then you may don't have GD2 lib, which may also point some error on your PHP, since GD2 is a bundled lib of PHP: http://www.php.net/manual/en/image.installation.php. Also, check this question: http://stackoverflow.com/questions/9100507/image-cropping-with-codeigniter-no-gd-library-gd2-is-intalled – Fabiano Araujo May 19 '14 at 22:10
  • great it worked without errors, but at the same time thumbnail creation is gone what should be done for thumb creation – Hassan May 19 '14 at 22:15