php - CodeIgniter, database with image name
问题描述
I have a form that works fine, but the images are always registered with "1" in the database. actually the image database "image_name.jpg" or "image_name.png" should be saved with.
Where did I go wrong? This İs My Controller;
public function send_Basvur_message(){
$config['upload_path'] = './panel/uploads/basvur_v/';
$config['allowed_types'] = 'jpg|png';
$config['max_size'] = 4096;
$config['overwrite'] = false;
$config['encrypt_name'] = false;
$config['remove_space'] = true;
$this->load->library('upload', $config);
$this->upload->initialize($config);
$this->load->library("form_validation");
$this->load->model("Basvur_model");
$this->form_validation->set_rules("name", "Ad", "trim|required");
$this->form_validation->set_rules("email", "E-posta", "trim|required|valid_email");
$this->form_validation->set_rules("subject", "Konu", "trim|required");
$this->form_validation->set_rules("message", "Mesaj", "trim|required");
$this->form_validation->set_rules("img_url", "img_url");
$this->form_validation->set_rules("captcha", "Doğrulama Kodu", "trim|required");
if($this->form_validation->run() === FALSE) {
// TODO Alert...
$this->session->set_flashdata('info','Action Completed');
redirect(base_url("basvuru"));
} else {
$insert = $this->Basvur_model->add(
array(
"name" => $this->input->post("name"),
"email" => $this->input->post("email"),
"message" => $this->input->post("message"),
"subject" => $this->input->post("subject"),
"img_url" => $this->upload->do_upload("img_url"),
"created_at" => date("Y-m-d H:i:s")
)
);
if($this->session->userdata("captcha") == $this->input->post("captcha")){
$name = $this->input->post("name");
$email = $this->input->post("email");
$subject = $this->input->post("subject");
$message = $this->input->post("message");
$img_url = $this->input->post("img_url");
$email_message = "{$name} isimli ziyaretçi. Başvuru Yaptı <br><b>Mesaj : </b> {$message} <br> <b>E-posta : </b> {$email}";
if(send_email("", "Yeni Aday başvurusu | $subject", $email_message)){
$this->session->set_flashdata('success','Action Completed');
redirect(base_url("basvuru"));
// TODO Alert..
} else {
$this->session->set_flashdata('success','Action Completed');
redirect(base_url("basvuru"));
// TODO Alert..
}
} else {
//başarısıs
$this->session->set_flashdata('error','Action Not Completed');
redirect(base_url("basvuru"));
}
}
}
解决方案
You are saving the do_upload()
response instead of the file name.
what you can do is like following
$image_url = null;
if($this->upload->do_upload("img_url")){
$image_url = $_FILES["fileToUpload"]["name"];
}
$insert = $this->Basvur_model->add(
array(
"name" => $this->input->post("name"),
"email" => $this->input->post("email"),
"message" => $this->input->post("message"),
"subject" => $this->input->post("subject"),
"img_url" => $image_url,
"created_at" => date("Y-m-d H:i:s")
)
);
save the image name if the do_upload()
was successful unless it will save null
推荐阅读
- gherkin - 在 Gherkin 的行尾添加空格
- r - R-Lookup 匹配另一个数据框中的值,然后通过创建新变量将它们合并为原始值
- ssh - 如何在 openstack 实例关闭时发送电子邮件警报?
- r - 根据字符串拆分数据框
- java - Spring 测试 - 从测试类创建第一个 bean 并生成真实代码
- python - 如何使用 tf.py_function 将张量转换为映射函数内的 numpy 数组
- flutter - 是否有任何 Flutter 软件包可以启用阻止和解除阻止对所有设备应用程序的 Internet 访问的功能?
- pandas - 使用 groupby 的 Pandas 数据帧计数值高于阈值 - 代码优化
- javascript - 如何从我们从 axios 获得的 HTML String 数据中获取 ID 的 innerText?
- angular - 拖放多个选择项