【发布时间】:2015-06-10 19:49:03
【问题描述】:
我想用 CodeIgniter 上传一张图片。我按照文档中的教程进行操作,但它不起作用。有谁知道解决方案?
如何将图像发送到数据库?
在 Views/mod/mod.php 中查看
<form action="" method="post">
<?php if(isset($error)): ?>
<?php echo $error ?>
<?php endif; ?>
<?php echo form_open_multipart('upload/do_upload');?>
<label for="image">Afbeelding</label>
<input type="file" id="image" name="image" />
<input type="submit" value="upload" />
</form>
在 Views/mod/upload_succes.php 中上传成功
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Upload succes</title>
</head>
<body>
<h3>Your file was successfully uploaded!</h3>
<ul>
<?php foreach ($upload_data as $item => $value):?>
<li><?php echo $item;?>: <?php echo $value;?></li>
<?php endforeach; ?>
</ul>
<p><?php echo anchor('upload', 'Upload Another File!'); ?></p>
</body>
</html>
Controllers/upload.php 中的Upload.php
<?php
class Upload extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
}
public function index()
{
$this->load->view('mod/mod', array('error' => ' ' ));
}
public function do_upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'jpg|png';
$config['max_size'] = 100;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('mod/mod', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
$this->load->view('mod/upload_success', $data);
}
}
}
?>
【问题讨论】:
-
你有什么问题,你卡在哪里了?
-
文件没有上传到文件夹
-
给出路径为 $config['upload_path'] = 'uploads/';
-
$config['upload_path'] = '/var/www/html/images/ // 这里给出写入路径
-
您打开表单两次,您也应该检查一下。
标签: php mysql file codeigniter upload