上传图片时,上传路径在 codeigniter 中似乎无效
The upload path does not appear to be valid in codeigniter when upload image
我正在尝试用上传的图片制作垃圾。当我尝试上传时,它一直返回失败。我把 var_dump 当我把代码显示错误时它显示错误 returns:
"The upload path does not appear to be valid"
我一直在寻找解决方案,但一无所获。谁能帮我找出错误:
这是我的控制器:
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'));
}
}
}
}
}
change this line
'upload_path' => is_dir('assets/reimburse/') . 'assets/reimburse/',
into this
'upload_path' => './assets/reimburse/',
check the output of this
var_dump(is_dir(./assets/reimburse));
如果为假,则文件夹的物理路径一定有问题
您必须使用 base_path 作为 codeigniter 中的物理路径。
$base_path = $this->config->item('base_path');
然后使用 $base_path 作为项目文件夹的根目录,您可以访问项目文件夹中的任何文件夹来上传图像。
喜欢。 $base_path.'/'.'upload/';
我正在尝试用上传的图片制作垃圾。当我尝试上传时,它一直返回失败。我把 var_dump 当我把代码显示错误时它显示错误 returns:
"The upload path does not appear to be valid"
我一直在寻找解决方案,但一无所获。谁能帮我找出错误:
这是我的控制器:
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'));
}
}
}
}
}
change this line
'upload_path' => is_dir('assets/reimburse/') . 'assets/reimburse/',
into this
'upload_path' => './assets/reimburse/',
check the output of this
var_dump(is_dir(./assets/reimburse));
如果为假,则文件夹的物理路径一定有问题
您必须使用 base_path 作为 codeigniter 中的物理路径。
$base_path = $this->config->item('base_path');
然后使用 $base_path 作为项目文件夹的根目录,您可以访问项目文件夹中的任何文件夹来上传图像。
喜欢。 $base_path.'/'.'upload/';