php 使用codeigniter上传图片并在数据库中保存路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36659977/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
image upload and save path in database using codeigniter
提问by charan
i did not get image upload and save image
path in database
My table name is uploadimage
and controller name is upload.php
我没有image
在数据库中上传图片和保存路径 我的表名是uploadimage
,控制器名是upload.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Upload extends CI_Controller {
public function index()
{
$this->load->view('view');
}
public function do_upload()
{
$status="";
$msg="";
$filename='product_pic';
if(empty($_POST['title']))
{
$status="error";
$msg="plz enter title";
}
if($status!="error")
{
$config['upolad_path']='./file/';
$config['allowed_types']='gip|jpg|png';
$config['max_size']=1024*0;
$config['encrypt_name']=true;
$this->load->library('upload',$config);
if(!$this->upload->do_upload($filename))
{
$status='error';
$msg=$this->upload->display_errors('','');
}
else
{
$this->load->model('file_model');
$data=$this->upload->data();
$file_id=$this->file_model->insert_file($data['file_name'],$_POST['title']);
if($file_id)
{
redirect('Upload/view');
}
}
}
回答by elddenmedio
Hi you need to have this
嗨,你需要这个
view.php
查看.php
<?= form_open_multipart('upload/do_upload')?>
<?= form_upload('userfile')?>
<?= form_close()?>
upload.php
上传.php
function do_upload(){
$name = $_FILES["file"]["name"];
$ext = end((explode(".", $name))); # extra () to prevent notice
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 0;
$this->load->library('upload', $config);
if( ! $this->upload->do_upload()){
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
else{
$upload_data = $this->upload->data();
#you can choose from
/*
Array(
[file_name] => mypic.jpg
[file_type] => image/jpeg
[file_path] => /path/to/your/upload/
[full_path] => /path/to/your/upload/jpg.jpg
[raw_name] => mypic
[orig_name] => mypic.jpg
[client_name] => mypic.jpg
[file_ext] => .jpg
[file_size] => 22.2
[is_image] => 1
[image_width] => 800
[image_height] => 600
[image_type] => jpeg
[image_size_str] => width="800" height="200"
)
*/
$this->model->insert_data($upoad_data['file_name'], $upoad_data['full_path']);
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success', $data);
}
}
model.php
模型.php
function insert_data($name, $path_name){
$data = array(
'name' => $name,
'path' => $path_name
);
$this->db->insert('table', $data);
return $this->db->insert_id();
}
With that, you upload your file, then get the data about the upload and insert it in db
这样,您上传文件,然后获取有关上传的数据并将其插入数据库
回答by Gopal Gautam
I use the following snippet for uploading file with multiple file upload field in the form, saving uploaded file to custom directories and finally saving the file location path to the database
我使用以下代码段在表单中上传带有多个文件上传字段的文件,将上传的文件保存到自定义目录,最后将文件位置路径保存到数据库
$input1 = $this->input->post('input1'); //Input field value
$input2 = $this->input->post('input2'); //Input field value
$photo1 = ''; //File field value
$photo2 = ''; //File field value
$config['upload_path'] = './photo_gallery/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '';
$this->load->library('upload', $config);
foreach($_FILES as $key=>$file) {
//$key will be the field name
$out_dir = "./photo_gallery/$key";
if(!file_exists($out_dir)) {
mkdir($out_dir, '0777', true);
}
if ( ! $this->upload->do_upload($key))
{
$error = array('error' => $this->upload->display_errors());
}
else
{
$data = array('upload_data' => $this->upload->data());
$fileName = $data['upload_data']['file_name'];
$newName = sha1_file("./photo_gallery/$fileName") . "." . pathinfo("./photo_gallery/$fileName", PATHINFO_EXTENSION);
rename("./photo_gallery/$fileName", "$out_dir/$newName");
$$key = "$out_dir/$newName"; //This will assign values for $photo1 and $photo2 with new paths
}
}
$data_to_insert = array(
'input1' => $input1,
'input2' => $input2,
'photo1' => $photo1, //Updated with new file path
'photo2' => $photo2, //Updated with new file path
);
//pass variable $data_to_insert in model function that implements following
$success = $this->db->insert('my_table', $data_to_insert);
return $this->db->insert_id();