0

我在尝试在我的应用程序中实现 Codeigniter 类时遇到问题。问题是当我提交表单时,执行的函数没有将文件保存到我指定的文件夹中,也没有抛出错误消息。我的代码如下:

看法:

<?php echo form_open_multipart('requests/submit_candidate'); ?>
<table>
        <tr>
        <td><label for="text">Upload CV</label></td>
        <td><input type="file" name="file"></textarea></td>
    </tr>
    <tr>
        <td><label for="text">Extra Information</label></td>
        <td><textarea name="extra_info"></textarea></td>
    </tr>       
</table>
<input type="submit" name="submit" value="Submit" />
</form>

控制器:

public function submit_candidate($slug)
    {
        $this->load->library('upload');

        $config =  array(
              'upload_path'     => dirname($_SERVER["SCRIPT_FILENAME"])."/uploads/",
              'upload_url'      => base_url()."uploads/",
              'allowed_types'   => "gif|jpg|png|jpeg|pdf|doc|xml",
              'overwrite'       => TRUE,
              'max_size'        => "1000KB",
              'max_height'      => "768",
              'max_width'       => "1024"  
            );

        if($this->upload->do_upload($config))
        {
            echo "file upload success";
        }
        else
        {
           echo "file upload failed";
        }
}

有人可以帮忙吗?

非常感谢,

SR

4

1 回答 1

0

如果它适合你,试试这个:

1) HTML

<td><input type="file" name="file"></td>

2)控制器

public function submit_candidate($slug)
{
    $this->load->library('upload');

    $config =  array(
          'upload_path'     => "./uploads/",
          //'upload_url'      => base_url()."uploads/",
          'allowed_types'   => "gif|jpg|png|jpeg|pdf|doc|xml",
          'overwrite'       => TRUE,
          'max_size'        => 1000,
          'max_height'      => 768,
          'max_width'       => 1024  
        );
    $this->upload->initialize( $config );
    if(!$this->upload->do_upload('file'))  // "file" is the name of the element default it's "userfile"
    {
        print_r( $this->upload->display_errors() );die; //display the error
    }
    else
    {
       $data    = $this->upload->data();
       print_r( $data );die;                            //file uploaded data
    }
}

1)在函数内部,您需要给出表单文件元素的名称(对您来说它的“文件”)
2)您还使用了一个无效的配置变量“upload_url”。
3)您需要初始化上传库的配置。
这里(文件上传文档)是您的参考。

于 2014-06-25T13:53:44.933 回答