如何将图像上传到特定文件夹并使用 codeigniter 重命名
how to upload image into specific folder and rename it using codeigniter
所以我的控制器中有这个功能,但我不知道如何在我的视图中实现它,我设法将图像上传到数据库中,但我上传的图像没有显示在任何文件夹中在数据库中,我还想将图像重命名为 date:month:year(时间戳),我错过了什么?
控制器
public function do_upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '500';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
$this->upload->initialize($config);
if ( ! $this->upload->do_upload('foto'))
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success', $data);
$file = $data['upload_data']['full_path'];
}
}
查看
<div class="form-group">
<label>Foto</label>
<input type="file" action="<?php echo site_url('admin/barang/do_upload');?>" name="foto" id="foto" class="form-control">
</div>
<button type="submit" class="btn btn-primary" style="width:100%;">Tambah</button>
编辑
所以我一直渴望做这件事,做了一些研究并阅读了很多文章,但 none 似乎可行,据我所知,我的观点是 form action="admin/barang/insert"
,并且这使得我的第二个表单被忽略,无论如何我们如何调用相同表单中的 2 个操作?
试试这个。
您的看法:
<?php echo form_open_multipart('admin/barang/do_upload');?>
<div class="form-group">
<label>Foto</label>
<?php echo form_upload('foto'); ?>
</div>
<button type="submit" class="btn btn-primary" style="width:100%;">Tambah</button>
<?php echo form_close(); ?>
在您的控制器上:
public function do_upload()
{
$config['upload_path'] = './uploads/';//this is the folder where the image is uploaded
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '500';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$config['file_name'] = 'enter new file name here';//rename file here
$this->load->library('upload', $config);
$this->upload->initialize($config);
if ( ! $this->upload->do_upload('foto'))
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success', $data);
$file = $data['upload_data']['full_path'];
}
}
希望对您有所帮助。
问题已解决,而不是在 Controller 中创建一个新函数 do_upload()
,我将 do_upload()
函数中的内容放入我的 insert()
函数,所以我的 insert()
函数有这样的东西
//photo
$photoName = gmdate("d-m-y-H-i-s", time()+3600*7).".jpg";
$config['upload_path'] = './assets/img/barang';
$config['allowed_types'] = 'gif||jpg||png';
$config['max_size'] = '2048000';
$config['file_name'] = $photoName;
$this->load->library('upload',$config);
if($this->upload->do_upload('userfile')){
$upload = 1;
}
else{
$upload = 2;
}
然后放一些if else语句,如果上传成功,则更新数据,否则不更新
所以我的控制器中有这个功能,但我不知道如何在我的视图中实现它,我设法将图像上传到数据库中,但我上传的图像没有显示在任何文件夹中在数据库中,我还想将图像重命名为 date:month:year(时间戳),我错过了什么?
控制器
public function do_upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '500';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
$this->upload->initialize($config);
if ( ! $this->upload->do_upload('foto'))
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success', $data);
$file = $data['upload_data']['full_path'];
}
}
查看
<div class="form-group">
<label>Foto</label>
<input type="file" action="<?php echo site_url('admin/barang/do_upload');?>" name="foto" id="foto" class="form-control">
</div>
<button type="submit" class="btn btn-primary" style="width:100%;">Tambah</button>
编辑
所以我一直渴望做这件事,做了一些研究并阅读了很多文章,但 none 似乎可行,据我所知,我的观点是 form action="admin/barang/insert"
,并且这使得我的第二个表单被忽略,无论如何我们如何调用相同表单中的 2 个操作?
试试这个。
您的看法:
<?php echo form_open_multipart('admin/barang/do_upload');?>
<div class="form-group">
<label>Foto</label>
<?php echo form_upload('foto'); ?>
</div>
<button type="submit" class="btn btn-primary" style="width:100%;">Tambah</button>
<?php echo form_close(); ?>
在您的控制器上:
public function do_upload()
{
$config['upload_path'] = './uploads/';//this is the folder where the image is uploaded
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '500';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$config['file_name'] = 'enter new file name here';//rename file here
$this->load->library('upload', $config);
$this->upload->initialize($config);
if ( ! $this->upload->do_upload('foto'))
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success', $data);
$file = $data['upload_data']['full_path'];
}
}
希望对您有所帮助。
问题已解决,而不是在 Controller 中创建一个新函数 do_upload()
,我将 do_upload()
函数中的内容放入我的 insert()
函数,所以我的 insert()
函数有这样的东西
//photo
$photoName = gmdate("d-m-y-H-i-s", time()+3600*7).".jpg";
$config['upload_path'] = './assets/img/barang';
$config['allowed_types'] = 'gif||jpg||png';
$config['max_size'] = '2048000';
$config['file_name'] = $photoName;
$this->load->library('upload',$config);
if($this->upload->do_upload('userfile')){
$upload = 1;
}
else{
$upload = 2;
}
然后放一些if else语句,如果上传成功,则更新数据,否则不更新