我正在尝试用上传的图像制作crud。当我尝试上传时,它不断返回失败。我放var_dump,当我放代码显示错误时,它显示false,它返回:
"上传路径似乎无效">
我一直在寻找解决方案,但一无所获。有人能帮我找到错误吗:
这是我的控制器:
public function saveReimburse()
{
validate_submitted_data(array(
'nama' => 'required',
'category_reimburse_id' => 'required',
'amount' => 'required|numeric',
'date_reimburse' => 'required',
// 'photo' => 'required'
));
// data
$data = [
'nama' => $this->input->post('nama'),
'category_reimburse_id' => $this->input->post('category_reimburse_id'),
'amount' => $this->input->post('amount'),
'date_reimburse' => $this->input->post('date_reimburse'),
'photo' => $_FILES['photo'],
// 'status'=> $this->input->post("PENDING"),
// 'nama' => $this->input->post('nama'),
];
// condition
$date = date('Y-m-d');
$date = strtotime($date);
$date = strtotime('-7 day', $date);
if ($data['date_reimburse'] < date('Y-m-d', $date)) {
echo json_encode(array('succes' => FALSE, 'message' => 'Max Reimburse was 1 week ago'));
} else {
// var_dump($data);
// exit;
if ($data['photo'] = "") {
} else {
$config = [
'upload_path' => is_dir('assets/reimburse/') . 'assets/reimburse/',
'allowed_types' => 'jpg|png|gif',
'overwrite' => TRUE
];
$this->load->library('upload', $config);
$upload = $this->upload->do_upload('photo');
var_dump($config);
print_r($this->upload->display_errors());exit;
var_dump($upload);exit;
if (!$upload) {
json_encode(array('success' => FALSE, 'message' => 'Failed Upload'));
redirect('Reimburse/index', 'refresh');
} else {
$this->upload->data('file_name');
$save = $this->reimburseModel->saveReimburse('reimburse', $data);
var_dump($data);exit;
if (!$save) {
echo json_encode(array('success' => FALSE, 'message' => 'Failed to reccord'));
} else {
redirect('Reimburse/index', 'refresh');
echo json_encode(array('success' => TRUE, 'message' => 'Reimburse Success'));
}
}
}
}
}
您必须在codeigniter中使用base_path作为物理路径。
$base_path=$this->config->item("base_path"(;
然后使用$base_path作为项目文件夹的根目录,您可以访问项目文件夹中的任何文件夹来上传图像。
喜欢$base_path。'/'。'上传/';
更改此行
'upload_path' => is_dir('assets/reimburse/') . 'assets/reimburse/',
进入此
'upload_path' => './assets/reimburse/',
检查此的输出
var_dump(is_dir(./assets/reimburse));
如果它是错误的,那么文件夹的物理路径一定有问题