我在codeigniter中有以下php:
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '4000';
$config['max_width'] = '3000';
$config['max_height'] = '3000';
$this->load->library('upload', $config);
$image_data = $this->upload->do_upload();
但是,当文件上传时, $ image_data ['file_path'] 将返回/ home / public_html / sitename / uploads。我需要它去www.domain.com/uploads。对于为什么会发生这种情况的任何想法?
我宁愿不必在那里硬编码str替换。希望这不是重复。
答案 0 :(得分:2)
上传功能返回的数据是根据应用程序的DOCUMENT ROOT。您可以获取file_name,并相应地使用base_url()来完成此操作
实施例
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '4000';
$config['max_width'] = '3000';
$config['max_height'] = '3000';
$this->load->library('upload', $config);
$image_data = $this->upload->do_upload();
$image_data_file = $image_data["file_name"];
$image_file_url = base_url()."/uploads/".$image_data_file;
希望这有帮助。
答案 1 :(得分:0)
在codeigniter文档中指定$ image ['file_path']返回文件的绝对服务器路径。
或者你可以做
$upload_path = 'uploads/';
$config['upload_path'] = './'. $upload_path;
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '4000';
$config['max_width'] = '3000';
$config['max_height'] = '3000';
$this->load->library('upload', $config);
$image_data = $this->upload->do_upload();
$dir_url = base_url($upload_path);
请记住,您必须在codeigniter中包含 url helper 才能使用base_url函数