php - 使用 CodeIgniter 上传文件

标签 php mysql file codeigniter upload

我想使用 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);
        }
    }
}
?>

最佳答案

尝试一下。希望这对您有帮助------------

$file_name = "";

if ($_FILES['image']['error']!= 4) {

            $fileParts = pathinfo($_FILES['image']['name']);
            $file_name = time() . '.' . $fileParts['extension'];
            $config['upload_path'] = './uploads/';
            $config['allowed_types'] = 'gif|jpg|png|jpeg';

            $config['file_name'] = $file_name;
            $this->load->library('upload', $config);
            $this->upload->initialize($config);
            $this->upload->do_upload('image');
        }

关于php - 使用 CodeIgniter 上传文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29470850/

相关文章:

mysql - Laravel深度优先树遍历

c - 太多打开的文件c

c - ftell 返回 -1 并导致程序崩溃

php - 如何在 laravel mongodb 上使用 "not like"?

php - 如何为莫里斯制作数组中的响应数据

mysql - mysql的最佳查询

mysql加载数据本地infile语法问题与设置字段

php - 如何将输入时间的值插入到数据库表中?

php - 如何从 PHP 脚本转到新的 Html 页面?

mysql - 使用 Catalyst 使用 DBIC 将文件存储在数据库中