我尝试通过php codeigniter上传图像,但显示“上传路径似乎无效。”
此操作在xampp服务器和窗口10中执行
public function do_upload($username) {
$config = array(
'upload_path' => "./assets/images/",
'allowed_types' => "gif|jpg|png|jpeg",
'overwrite' => TRUE,
'max_size' => "2048000",
'max_height' => "768",
'max_width' => "1024"
);
$this->load->library('upload', $config);
if ($this->upload->do_upload()) {
$data = array('upload_data' => $this->upload->data());
print_r($data);
} else {
$error = array('error' => $this->upload->display_errors());
print_r($error);
}
}
我多次使用此代码,但未发现错误 代码有什么错误, 如果我在其他项目中使用相同的代码,则可以使用,但在主项目中则无法使用,请帮助,谢谢
答案 0 :(得分:0)
在CodeIgniter中,您应始终尝试使用常量APPPATH
查找应用程序文件夹。
APPPATH
是最好的,因为无论您使用的是哪种操作系统,它都会动态地检索适当的文件系统目录结构。这样可以确保代码中的所有文件路径都正确。
但是,如果您不尝试进入Application
文件夹。您可以使用FCPATH
常量,该常量动态指向应用程序的前端控制器。
您收到此错误,因为/ assets / images文件夹的路径无效。您需要确保路径正确。
答案 1 :(得分:0)
首先,您应该检查是否已在根目录中创建了文件夹asset / images文件夹,因为在配置中已定义了该路径
$config = [
'upload_path' => "./assets/images/",
'allowed_types' => "gif|jpg|png|jpeg",
'overwrite' => TRUE,
'max_size' => "2048000",
'max_height' => "768",
'max_width' => "1024"
];
$this->load->library('upload',$config);
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<div style="color:red;" class="error">', '</div>');
if$this->upload->do_upload()){
$img = $this->upload->data();
$img_name = base_url('uploads/'.$img['file_name']);
$data = $this->input->post();
$data['image_path'] = $img_name;
unset($data['submit']);
return $this->flash_message(
$this->articlemodel->add_article($data),
'Article added sucessfully',
'Article added to failed'
);
}
else
{
$upload_error = $this->upload->display_errors;
}
答案 2 :(得分:0)
解决起来很简单
如果目录位于应用程序文件夹中
application -> assets -> images
'upload_path' => APPPATH. 'assets/images/';
如果目录在应用程序文件夹之外
root folder -> assets -> images
'upload_path' => FCPATH. 'assets/images/';