1

我想通过视图->控制器->模型传递文本输入和2个文件,当我运行此代码时,总是出现“您没有选择要上传的文件”的错误。

查看- 在此视图中,我输入文本和两个名为“myFile”和“myDoc”的加载文件输入

              <form action="<?=site_url('blog_maintain/save')?>" method = "post" enctype="multipart/form-data"><li class="field" style="display:inline"><p><i class = "icon-docs"></i>&nbsp;หัวข้อ : 
              <input class="text input" type="text" placeholder="Topic Input" name = "topic" id ="topic" required="required">
              </p></li>
              <li class="field" style="display:inline"><p><i class = "icon-menu"></i>&nbsp;คำอธิบาย : 
              <input class="text input" type="text" placeholder="Short Description" name = "desc" id = "desc" required="required">
              </p></li>
              <li class="field" style="display:inline"><p><i class = "icon-picture"></i>&nbsp;รูปภาพ :
              ******<input type="file" name = "myFile" id = "myFile" required="required" accept="image/*">******
              </p></li>
              <li class="field" style="display:inline"><p><i class = "icon-attach"></i>&nbsp;ไฟล์แนบ : 
              ***<input type="file" name = "myDoc" id = "myDoc" accept=".doc,.docx,.pdf,.txt,.xls,.xlsx,.ppt,.pptx">***
              </p></li>               
              <li class="field" style="display:inline"><p><i class = "icon-doc-text"></i>บทความ : 
              <textarea class="input textarea" placeholder="Description" rows="5" name = "text" id = "text" required="required"></textarea>
              </p></li>
              <input class="medium btn pill-left default push_seven two columns" type="submit" value = "upload" onclick = "return check();">
              <input class="medium btn pill-right default two columns" type="Reset" value = "Reset">

模型- 在这个模型中,我将 $_POST 传递给控制器​​ save_blog()

public function save(){
    $this->load->model('blog_maintain_helper');
    $data['result'] = $this->blog_maintain_helper->save_blog();
    $this->loadView('blogMaintainView',$data);
}

控制器

function save_blog(){           
    $config =  array(
        'upload_path'     => 'server_path',
        'allowed_types'   => "gif|jpg|png|jpeg|pdf|doc|xml",
        'overwrite'       => TRUE,          
    );
    get_instance()->load->library('upload', $this->config);
    if($this->upload->do_upload())          
    {
        echo "file upload success";             
    }           
    else            
    {
        echo $this->upload->display_errors();           
    }       
}
4

4 回答 4

3

在 codeigniter 中,将上传的默认文件名是userfile,如果您要使用其他名称上传文件,则需要将其传递给 do_upload 方法

function save_blog(){ 
    $config = array( 'upload_path' => 'server_path',
                     'allowed_types' => "gif|jpg|png|jpeg|pdf|doc|xml",
                     'overwrite' => TRUE, );

    get_instance()->load->library('upload', $this->config); 
    if($this->upload->do_upload('myDoc')) { 
        echo "file upload success"; 
    } else { 
        echo $this->upload->display_errors(); 
    } 
}
于 2013-09-13T21:40:37.883 回答
0

do_upload 需要一个名为“userfile”的表单字段,这不是您的情况。要设置另一个字段名称,您必须将其作为参数传递:

$field_name = "some_field_name";
$this->upload->do_upload($field_name)
于 2013-09-13T21:33:50.063 回答
0
<input type="file" name = "myDoc" id = "myDoc" accept=".doc,.docx,.pdf,.txt,.xls,.xlsx,.ppt,.pptx">

使用您的输入 type='file' name (myDoc) as

$this->upload->do_upload("myDoc")

于 2013-09-13T23:20:21.920 回答
0
echo $this->upload->display_errors(); 

你在 MVC 技术中有错误你不能在控制器中“回显”任何东西

于 2014-01-22T19:18:35.800 回答