当在codeigniter中使用uploader系统时,系统会调用数组上的成员函数site_url()

时间:2017-09-06 21:04:20

标签: php codeigniter

这是使用CI上传的非常简单的文件,但系统说功能site_url()base_url()无法在原始文件Helper Codeigniter中使用

$this->config = array('upload_path' => dirname($_SERVER["SCRIPT_FILENAME"])."/images/",
      'upload_url'      => base_url()."images/",
      'allowed_types'   => "png|jpg",
      'overwrite'       => TRUE,
      'max_size'        => "10000KB",
      'max_height'      => "7680",
      'max_width'       => "10240",
      'file_name'       => "about"
            );

$this->load->library('upload', $this->config);
    if($this->upload->do_upload('logo'))
    {
        $this->session->set_flashdata('success_upload', 'You logo has been uploaded' );
    }
    else
    {
        $this->session->set_flashdata('error_upload', 'Can\'t Upload this file, Please try agine.' );
    }

2 个答案:

答案 0 :(得分:0)

根据您的this comment我认为您必须先加载网址助手。

$this->load->helper('url');

或者您可以在autoload.php

中加载它
$autoload['helper'] = array('url');

要上传图片,请尝试以下代码:

$config['upload_path']          = './images/';
$config['allowed_types']        = 'png|jpg';
$config['max_size']             = 10000;
$config['max_width']            = 10240;
$config['max_height']           = 7680;

$this->load->library('upload', $config);

if ( ! $this->upload->do_upload('userfile'))
{
        $error = array('error' => $this->upload->display_errors());

        $this->load->view('upload_form', $error);
}
else
{
        $data = array('upload_data' => $this->upload->data());

        $this->load->view('upload_success', $data);
}

答案 1 :(得分:0)

使用此代码可以帮助您。

$obj = &get_instance();
  $obj->load->library('upload');
  $file_name = "about." . pathinfo($_FILES['logo']['name'], PATHINFO_EXTENSION); //userrandom file name instead of about
  $full_path = FCPATH . 'images/';

  $config['upload_path'] = $full_path;
  $config['allowed_types'] = 'gif|jpg|png|jpeg|GIF|JPG|PNG|JPEG';
  $config['max_size'] = 2048;
  $config['max_width'] = 2048;
  $config['max_height'] = 2048;
  $config['file_name'] = $file_name;

  $obj->upload->initialize($config);

  if (!$obj->upload->do_upload('logo')) {
    $this->session->set_flashdata('error_upload', $obj->upload->display_errors());
    return $obj->upload->display_errors();
  } else {
    $this->session->set_flashdata('success_upload', 'You logo has been uploaded');
    return $obj->upload->data();
  }
  • 请勿使用dirname()作为文件路径。 使用FCPATH它会让你从根目录中找到绝对的路径,例如" /var/www/html/project_name"
  • upload_url不是codeiginter上传库中的密钥
  • 使用随机文件名,因为如果您上传其他图片,它将覆盖或创建文件新文件,如file_name(1).extension