0

我有一个多张图片上传表单,下面的代码可以很好地上传。我需要将文件名保存到数据库中,但我无法弄清楚如何正确地做到这一点。

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>

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 :/



    }    
}
4

1 回答 1

1

您应该仅将模型保留用于数据库操作。所有的上传处理和文件移动都必须在控制器中完成。模型必须在数据库中插入关于照片的记录,仅此而已。

作为对您问题的回答,请执行 aprint_r($files)并查看其中包含的内容。它应该具有原始文件名。它可能会像上面的artmania所说的那样:$files[0]['file_name']。您应该能够使用这样的 foreach 构造遍历 $files 数组:

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

您可以以相同的方式获取有关该文件的所有其他数据。CodeIgniter 手册说$this->upload->data()

这是一个帮助函数,它返回一个数组,其中包含与您上传的文件相关的所有数据。这是数组原型:

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"
)

有关更多信息,请查看手册

于 2009-10-07T09:10:22.983 回答