【问题标题】:Get Renamed File Names of JavaScript Forms in CodeIgniter在 CodeIgniter 中获取 JavaScript 表单的重命名文件名
【发布时间】:2010-12-03 22:41:21
【问题描述】:

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

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



    }    
}

【问题讨论】:

  • hmm,刚刚发现…… $files[0][“file_name”] $files[1][“file_name”] 给出了重命名的文件名。

标签: php database codeigniter upload filenames


【解决方案1】:

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

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

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

您可以以相同的方式获取有关该文件的所有其他数据。 CodeIgniter 手册中提到了$this-&gt;upload-&gt;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"
)

更多信息check out the manual

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-06
    • 1970-01-01
    • 2014-03-04
    • 1970-01-01
    • 2014-03-15
    • 2011-06-21
    • 2015-10-26
    相关资源
    最近更新 更多