【问题标题】:How to get filename in MODEL to insert into database from the input form?如何在 MODEL 中获取文件名以从输入表单插入数据库?
【发布时间】:2012-05-30 19:40:08
【问题描述】:

我是使用 CodeIgniter 的新手。如果我的问题很愚蠢,我深表歉意。
我的模型功能如下:

function entry_insert($fname){
$this->load->database();
$maxFileID=mysql_fetch_array(mysql_query("SELECT max(convert(substring( FileID , 1,length(FileID)-4 ) ,unsigned integer)) as val FROM docrepository"));
 // Find the maximum value of FileID
  if ($maxFileID['val']==NULL)
  {
  $temp="1.PDF";
  }
  else
  {
  //$data["FileID"]=(string)(intval(substr($row[0],0,-4))+1).".PDF"; // If there is already file 
  $temp=(string) ($maxFileID['val']+1).".PDF";
  }
   $data=array(

    'FileID'        =>$temp,
   'FileName'       =>$fname,// Not sure whether it will work---I need your suggestion
    'title'         =>$this->input->post('title'),
    'author'        =>$this->input->post('author'),
    'description'   =>$this->input->post('description'),
    'companyName'   =>$this->input->post('companyName'),
    'aircraftModel' =>$this->input->post('aircraftModel'),
    'aircraftNumber'=>$this->input->post('aircraftNumber'),
    'documentType'  =>$this->input->post('documentType'),
    'documentNumber'=>$this->input->post('documentNumber'),
    'sectionNumber' =>$this->input->post('sectionNumber'),
    'dateCreated'   =>$this->input->post('dateCreated'),
    'keywords'      =>$this->input->post('keywords'),
    'notes'         =>$this->input->post('notes')

   );
   $this->db->insert('docrepository',$data);

  }

所有数据都来自视图中的输入表单
我的目标是将原始文件名存储为数据库并将服务器中的文件上传为特定格式(FileID)。 所以在这种情况下

  1. 我应该将我的 do_upload() 函数放在模型中还是控制器中?
  2. 如何在视图中获取用户上传的文件名?
  3. 以及我们如何将 FileID 作为新文件名发送,以便 do_upload 函数可以将文件上传到服务器?

我会很感激任何关于示例代码的想法。

【问题讨论】:

  • 函数总是在模型中。将文件名放入名为 filename 的隐藏输入中,然后在您的函数中:'FileName' => $this->input->post('filename'), 然后,在您的函数中调用 $this->do_upload 进行上传。您的视图应该有一个表单,该表单可以发布到您的控制器中调用entry_insert 的函数。我不知道你的意思是不是 entry_insertdo_upload 不同。

标签: php codeigniter


【解决方案1】:

我应该将我的 do_upload() 函数放在模型中还是控制器中?

模型是与数据库一起工作的,所以我在这个中使用控制器。

如何获取用户上传的文件名 查看?

嗯,$_FILES 是 PHP 原生的,可以在您的控制器中访问。您也可以查看Upload class。该类具有data() 函数,该函数返回有关文件的所有信息。

//sample result of $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
    [client_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"
)

以及我们如何将 FileID 作为新文件名发送,以便 do_upload 函数可以上传服务器中的文件吗?

在模型中创建一个返回新名称的函数,并在开始上传类之前进行设置,例如:

//function save in the controller
function save() {
  $fname = $_FILES["input_upload"]["name"];
  $name = $this->model->getNewFileName($fname);
  $config["file_name"] = $name; //this replaces the name of the file on upload
  $this->load->library('upload', $config);
  if( $this->upload->do_upload() ) {
    $this->model->save();
  }
}

【讨论】:

    【解决方案2】:

    您想在控制器中进行上传。您可以获取文件名作为 POST 变量并将其发送到模型。

    $this->load->model('model_name');
    $this->model_name->saveFile($_POST['fileName'];
    

    别忘了清理数据等等。希望这可以帮助。我不太明白你说的#3 是什么意思。

    【讨论】:

    • 可选(并且推荐)使用 CI 的输入助手访问 POST 数据:$this->input->post('fileName');
    【解决方案3】:

    在你看来:

    echo form_open('your_controller/controller_function');
    

    例子:

    echo form_open('userfiles/saveform');
    

    然后,在您的控制器中:

    public function saveform() 
    {
        if($this->userfiles_model->entry_insert())
        {
            $this->load->view('your_view', $array);
        }
    }
    

    而且,entry_insert 将是您模型中的一个函数,它可能会或可能不会调用您模型中的 do_upload

    编辑: 我的理解是控制器只是视图和模型之间的“流量控制器”。任何上传或数据库保存都应该进入模型。控制器使用:

    1. 从模型中检索信息并将该信息发送到视图
    2. 从视图中显示的表单向模型发送信息

    【讨论】:

      猜你喜欢
      • 2017-05-13
      • 2014-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-13
      • 2011-10-22
      • 1970-01-01
      相关资源
      最近更新 更多