这是使用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.' );
}
答案 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