CodeIgniter Forums

Full Version: Need help with image upload file name.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]nemo1112[/eluser]
Hi coder
I have try to upload 4 image file at the same time.
But I have problem with file name when upload finish.
I have try many time and check in upload folder there have 4 image i upload
but file name change like this.file name change to first image name i upload.
Please help. Thank you.

Output like this
File Name: FatboySlim_-_Why_try_harder4.jpg
File Name: FatboySlim_-_Why_try_harder4.jpg.jpg
File Name: FatboySlim_-_Why_try_harder4.jpg.jpg.jpg
File Name: FatboySlim_-_Why_try_harder4.jpg.jpg.jpg.jpg

Controllers
Code:
<?php

class Upload extends Controller {
    
    function Upload() {
        parent::Controller();
        // $this->load->helper('form');
    }
    
    function index() {
        $this->load->view('upload_form');
    }
    
    function doUpload() {
        $config['upload_path'] = 'uploads/';
        $config['allowed_types'] = 'gif|jpg|jpeg|png';
        $config['max_size'] = '1000';
        $config['max_width'] = '1920';
        $config['max_height'] = '1280';                        
        
        $this->load->library('upload', $config);
        
        foreach($_FILES as $key => $value)
        {
            if(!$this->upload->do_upload($key)) echo $this->upload->display_errors();
            else {
                $fInfo = $this->upload->data();
                $this->_createThumbnail($fInfo['file_name']);
            
                $data['uploadInfo'] = $fInfo;
                $this->load->view('upload_success', $data);    
            }
        }
    }
    
}
View upload_form.php
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

&lt;html&gt;
  &lt;head&gt;
    &lt;title&gt;&lt;/title>
    &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
    <div id="container">
        <h2>Upload an Image </h2>

        &lt;?php echo form_open_multipart('upload/doUpload'); ?&gt;
        &lt;?php
                if(isset($upload_message))
                {
                    echo $upload_message;
                }
            ?&gt;
            &lt;?php $i = 4; for ($j = 1; $j <= $i; $j++): ?&gt;
            <div class="form_element">
                <label for="photo&lt;?=$j?&gt;">Photo N°&lt;?=$j?&gt;</label>
                &lt;?=form_upload(array('name'     => 'photo'.$j,
                                    'id'        => 'photo'.$j,
                                    'size'      => '36'))?&gt;
            </div>
            &lt;?php endfor ?&gt;
        <p>&lt;input type="submit" value="Submit" name="submit" /&gt;&lt;/p>
        &lt;?php echo form_close(); ?&gt;
    </div>

  &lt;/body&gt;
&lt;/html&gt;
View upload_success.php
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

&lt;html&gt;
  &lt;head&gt;
    &lt;title&gt;&lt;/title>
    &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;
    &lt;style type="text/css"&gt;
    dt {
        font-weight: bold;
        float: left;
        padding-right: 1em;
    }
    &lt;/style&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
    <div id="container">
        <p>Congratulations. Your photo has been uploaded successfully. </p>

        <dl>
            <dt>File Name: </dt>
            <dd> &lt;?= $uploadInfo['file_name']; ?&gt; </dd>
            
            <dt>File Size: </dt>
            <dd> &lt;?= $uploadInfo['file_size']; ?&gt; </dd>
            
            <dt> File Extension: </dt>
            <dd> &lt;?= $uploadInfo['file_ext']; ?&gt; </dd>
        </dl>
        <img alt="Your Image" src="&lt;?= base_url() . 'uploads/' . $uploadInfo['file_name'];?&gt;" />
        <img alt="Your Thumbnail" src="&lt;?= base_url() . 'uploads/thumb/' . $thumbnail_name;?&gt;" />
    </div>  
  &lt;/body&gt;
&lt;/html&gt;

El Forum

[eluser]nemo1112[/eluser]
bump

El Forum

[eluser]naren_nag[/eluser]
Could you post the code for your $this->_createThumbnail method.

El Forum

[eluser]nemo1112[/eluser]
ok this is the code. ;-)
sorry if my english bad.
Code:
function _createThumbnail($fileName) {
        $config['image_library'] = 'gd2';
        $config['source_image'] = 'uploads/' . $fileName;
        $config['new_image'] = 'uploads/thumb/';
        $config['create_thumb'] = TRUE;
        $config['maintain_ratio'] = TRUE;
        $config['width'] = 75;
        $config['height'] = 75;
        
        $this->load->library('image_lib', $config);
        if(!$this->image_lib->resize()) echo $this->image_lib->display_errors();
    }

El Forum

[eluser]nemo1112[/eluser]
I can fix my problem now. Thank you.

I just put
"$this->upload->initialize($config);"

before doUpload every time I upload next image.don't know why i have to do this.