Welcome Guest, Not a member yet? Register   Sign In
multifile upload problem with file name
#1

[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;
#2

[eluser]Horvath Gabor[/eluser]
i suppose the UPLOAD class has some problems in it
the simplest way is to reset the file_name var:
Code:
foreach($_FILES as $key => $value)
        {
        $this->upload->file_name = '';//insert this line
        if(!$this->upload->do_upload($key)) echo $this->upload->display_errors();

or

Have a look at this topic for a solution: File extension problem with upload




Theme © iAndrew 2016 - Forum software by © MyBB