我想做一个codeigniter控制器,它可以检索"POST“base64图像,解码它,然后将它保存到本地文件夹中,然后将该路径保存到我的MySQL中。
我被困在这一点上了。
你能给我一些这个案例的推荐人吗?
发布于 2017-01-11 15:57:31
下面的代码片段是来自CodeIgniter的官方用户文档的copy=paste。
<?php
class Upload extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
}
public function index()
{
$this->load->view('upload_form', array('error' => ' ' ));
}
public function do_upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 100;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('userfile'))
{
$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);
}
}
}
?>CodeIgniter确实有built-in file upload class,里面有非常好的文档,我真的建议你快速浏览一下。
你必须仔细检查的一个部分或修改如下:
$config['file_name'] = 'a_'.md5(microtime()).'.jpg';它告诉codeigniter在你想要的任何本地路径中将文件保存在哪里,然后你可以像这样简单地将它保存到你的数据库中:
$result = $this->db->query('INSERT INTO AVATARS VALUES (NULL, 'user1', '" . $config['file_name'] . "') ');请注意,这只是伪代码,没有在任何生产环境中测试过;然而,应该可以完美地工作。
祝好运
发布于 2018-04-05 20:41:24
$base应该有一个base64编码的图像字符串:
$base = $_POST["profile_image"];
$filename = $_POST["file_name"];
$filename = $_POST["user_id"];
$binary = base64_decode($base);
header('Content-Type: bitmap; charset=utf-8');
$file = fopen('./Images/'.$filename, 'wb');
fwrite($file, $binary);
fclose($file);https://stackoverflow.com/questions/41585263
复制相似问题