【问题标题】:error in $this->upload->do_upload(),$this->upload->do_upload() 中的错误,
【发布时间】:2013-09-13 21:27:35
【问题描述】:

我想通过视图->控制器->模型传递文本输入和 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();           
    }       
}

【问题讨论】:

  • 我遇到另一个错误,“上传路径似乎无效。”

标签: php codeigniter file-upload ftp


【解决方案1】:

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

【讨论】:

  • 我可以问另一个问题,当我使用 $this->upload->do_upload('myDoc') 时出现“上传路径似乎无效。”错误。在我的“server_path”中,我使用了“username:password@hostname:port/public_html/img/blog”,表明我的服务器上有这条路径
  • 第一次看到有能力通过你的方法'username:password@host:port'来验证与服务器的连接,除非你想通过 ftp 上传你可以使用 ftp class "@987654322 @" 可以直接使用目录路径,无需用户名、密码和主机,可写目录使用绝对路径或相对路径。
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:
    <input type="file" name = "myDoc" id = "myDoc" accept=".doc,.docx,.pdf,.txt,.xls,.xlsx,.ppt,.pptx">
    

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

    $this-&gt;upload-&gt;do_upload("myDoc")

    【讨论】:

      【解决方案4】:
      echo $this->upload->display_errors(); 
      

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

      【讨论】:

        猜你喜欢
        • 2014-12-22
        • 2016-05-15
        • 1970-01-01
        • 1970-01-01
        • 2016-09-03
        • 2019-03-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多