$ this-> upload-> do_upload()中的错误,

时间:2013-09-13 21:27:35

标签: php codeigniter file-upload ftp

我想传递文本输入和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 个答案:

答案 0 :(得分:2)

在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(); 
    } 
}

答案 1 :(得分:0)

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

$field_name = "some_field_name";
$this->upload->do_upload($field_name)

答案 2 :(得分:0)

<input type="file" name = "myDoc" id = "myDoc" accept=".doc,.docx,.pdf,.txt,.xls,.xlsx,.ppt,.pptx">

将您的输入类型='文件'名称(myDoc)用作

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

答案 3 :(得分:0)

echo $this->upload->display_errors(); 

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