上传图片codeigniter
Upload image codeigniter
我尝试使用 CodeIgniter 上传图片。必须将图像发送到文件夹并将路径发送到数据库。当我提交时,它什么也没做。 ($story_img)的var_dump显示:
string(8) "/images/"
这是我的代码:
if ($this->input->server('REQUEST_METHOD') == 'POST') {
$config['upload_path'] = '/images/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1000000';
$config['overwrite'] = TRUE;
$config['remove_spaces'] = TRUE;
$config['encrypt_name'] = FALSE;
$this->load->library('upload', $config);
$this->upload->do_upload('story_img');
$image_path = $this->upload->data();
$story_img = $image_path['full_path'];
echo var_dump($story_img);
$story_text = $this->input->post("story_text");
$users_id = $this->input->post('users_id');
$this->form_validation->set_rules('story_text', 'Story text', 'required');
$this->form_validation->set_rules('story_img', 'Story image ', 'callback__image_upload');
if ($this->form_validation->run() != FALSE) {
$new_story = new Story_model();
$new_story->Story_text = $story_text;
$new_story->Users_id = $users_id;
$new_story->Story_date = date('Y-m-d H:i:s');
$new_story->Story_img = $story_img;
$this->Story_model->addStory($new_story);
}
}
var_dump of $image_path
array(14) { ["file_name"]=> string(0) "" ["file_type"]=> string(0) "" ["file_path"]=> string(8) "/images/" ["full_path"]=> string(8) "/images/" ["raw_name"]=> string(0) "" ["orig_name"]=> string(0) "" ["client_name"]=> string(0) "" ["file_ext"]=> string(0) "" ["file_size"]=> NULL ["is_image"]=> bool(false) ["image_width"]=> NULL ["image_height"]=> NULL ["image_type"]=> string(0) "" ["image_size_str"]=> string(0) "" }
$image_path = $this->upload->data();
foreach ($image_path as $info) {
$path=$info['full_path'];
}
echo $path;
您确定我们的表单字段名称是 story_img 吗?
尝试添加条件:
if($this->upload->do_upload('story_img'))
{
//your code...
}
并尝试有条件地显示smth,看你是否通过。
您是否在表单标签
中提到了enctype="multipart/form-data"
我尝试使用 CodeIgniter 上传图片。必须将图像发送到文件夹并将路径发送到数据库。当我提交时,它什么也没做。 ($story_img)的var_dump显示:
string(8) "/images/"
这是我的代码:
if ($this->input->server('REQUEST_METHOD') == 'POST') {
$config['upload_path'] = '/images/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1000000';
$config['overwrite'] = TRUE;
$config['remove_spaces'] = TRUE;
$config['encrypt_name'] = FALSE;
$this->load->library('upload', $config);
$this->upload->do_upload('story_img');
$image_path = $this->upload->data();
$story_img = $image_path['full_path'];
echo var_dump($story_img);
$story_text = $this->input->post("story_text");
$users_id = $this->input->post('users_id');
$this->form_validation->set_rules('story_text', 'Story text', 'required');
$this->form_validation->set_rules('story_img', 'Story image ', 'callback__image_upload');
if ($this->form_validation->run() != FALSE) {
$new_story = new Story_model();
$new_story->Story_text = $story_text;
$new_story->Users_id = $users_id;
$new_story->Story_date = date('Y-m-d H:i:s');
$new_story->Story_img = $story_img;
$this->Story_model->addStory($new_story);
}
}
var_dump of $image_path
array(14) { ["file_name"]=> string(0) "" ["file_type"]=> string(0) "" ["file_path"]=> string(8) "/images/" ["full_path"]=> string(8) "/images/" ["raw_name"]=> string(0) "" ["orig_name"]=> string(0) "" ["client_name"]=> string(0) "" ["file_ext"]=> string(0) "" ["file_size"]=> NULL ["is_image"]=> bool(false) ["image_width"]=> NULL ["image_height"]=> NULL ["image_type"]=> string(0) "" ["image_size_str"]=> string(0) "" }
$image_path = $this->upload->data();
foreach ($image_path as $info) {
$path=$info['full_path'];
}
echo $path;
您确定我们的表单字段名称是 story_img 吗? 尝试添加条件:
if($this->upload->do_upload('story_img'))
{
//your code...
}
并尝试有条件地显示smth,看你是否通过。
您是否在表单标签
中提到了enctype="multipart/form-data"