views:

1031

answers:

1

Hi friends,

I use codeigniter. I have a multiple image upload form. The code below is working well for uploading, but I also need to save file names to database. How can I get the names in here?

I spent hours & hours :/ but could not sort it :/

Appreciate helps!!!


uploadform.php

 echo form_open_multipart('gallery/upload');
  <input type="file" name="photo" size="50" />
  <input type="file" name="thumb" size="50" />  
  <input type="submit" value="Upload" />
 </form>


I have a controller between form view and model load model (of course : )) but didnt post here because of no need.


gallery_model.php

 function multiple_upload($upload_dir = 'uploads/', $config = array())
 {

  /* Upload */  

     $CI =& get_instance();
     $files = array();

     if(empty($config))
     {
         $config['upload_path']   = realpath($upload_dir);
         $config['allowed_types'] = 'gif|jpg|jpeg|jpe|png';
         $config['max_size']      = '2048';
     }

        $CI->load->library('upload', $config);

        $errors = FALSE;

        foreach($_FILES as $key => $value)
        {            
            if( ! empty($value['name']))
            {
                if( ! $CI->upload->do_upload($key))
                {                                           
                    $data['upload_message'] = $CI->upload->display_errors(ERR_OPEN, ERR_CLOSE); // ERR_OPEN and ERR_CLOSE are error delimiters defined in a config file
                    $CI->load->vars($data);

                    $errors = TRUE;
                }
                else
                {
                    // Build a file array from all uploaded files
                    $files[] = $CI->upload->data();
                }
            }
        }


        // There was errors, we have to delete the uploaded files
        if($errors)
        {                    
            foreach($files as $key => $file)
            {
                @unlink($file['full_path']);    
            }                    
        }
        elseif(empty($files) AND empty($data['upload_message']))
        {
            $CI->lang->load('upload');
            $data['upload_message'] = ERR_OPEN.$CI->lang->line('upload_no_file_selected').ERR_CLOSE;
            $CI->load->vars($data);
        }
        else
        {
            return $files;


        }



        /* -------------------------------
        Insert to database */

        // problem is here, i need file names to add db. 
        // if there is already same names file at the folder, it rename file itself. so in such case, I need renamed file name :/



    }    
}
A: 

You should keep your model for database operations only. All the upload processing and file moving has to be done in the controller. The model has to insert the record about the photo in the database and that's about it.

And as a response to your question do a print_r($files) and see what it contains. It should have the original filenames. It'll probably be something like artmania said above: $files[0]['file_name']. You should be able to loop through your $files array with a foreach construct like this:

foreach($files as $file) {
  $file_name = $file['file_name'];
}

You can get all the other data about the file in the same way. The CodeIgniter manual says about $this->upload->data():

This is a helper function that returns an array containing all of the data related to the file you uploaded. Here is the array prototype:

Array
(
    [file_name]    => mypic.jpg
    [file_type]    => image/jpeg
    [file_path]    => /path/to/your/upload/
    [full_path]    => /path/to/your/upload/jpg.jpg
    [raw_name]     => mypic
    [orig_name]    => mypic.jpg
    [file_ext]     => .jpg
    [file_size]    => 22.2
    [is_image]     => 1
    [image_width]  => 800
    [image_height] => 600
    [image_type]   => jpeg
    [image_size_str] => width="800" height="200"
)

Fore more info check out the manual.

kitsched