php - 如何将图像上传到codeigniter中的文件夹?

标签 php mysql

我试图同时将图像上传到文件夹和 MySQL 数据库中。但是当我提交将其插入数据库但未上传到文件夹时,会发生什么情况。重要的是,我希望将其上传到文件夹名称已上传,请查看。

Controller :

Class AddPost extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();

        $this->load->database();
        $this->load->helper('form');
        $this->load->library('upload');
        $this->load->model('add_post_model');
    }

    public function index()
    {
        $this->load->library('form_validation');

        $this->form_validation->set_error_delimiters('<div class="error">', '</div>');

        $this->form_validation->set_rules('url', 'url', 'required');
        /*$this->form_validation->set_rules('file', 'image', 'required');*/
        $this->form_validation->set_rules('date', 'date', 'required');
        $this->form_validation->set_rules('active', 'active', 'required');
        $this->form_validation->set_rules('img_id', 'img_id', 'required');


        $configUpload['upload_path']    = './uploads/';                 #the folder placed in the root of project
        $configUpload['allowed_types']  = 'gif|jpg|png|bmp|jpeg';       #allowed types description
        $configUpload['max_size']       = '0';                          #max size
        $configUpload['max_width']      = '0';                          #max width
        $configUpload['max_height']     = '0';                          #max height
        $configUpload['encrypt_name']   = true;                         #encrypt name of the uploaded file
        $this->load->library('upload', $configUpload);
        #init the upload class
        if(!$this->upload->do_upload('uploadimage')){
            $uploadedDetails    = $this->upload->display_errors();
        }else{
            $uploadedDetails    = $this->upload->data();
        }
        print_r($uploadedDetails);

        $prefix = "http://";

        if ($this->form_validation->run() == FALSE) {

            $this->load->view('AddPostView');
        }
        else
        {
            $example = $this->add_post_model->GetImageId();

                foreach ($example as $ex) {
                    echo $result = ($ex->ImgId + 1);
                }

                $data = array(
                    'ImgId' => $result,
                    'ImgPath' => $this->input->post('fileToUpload'),
                    'Url' => $prefix . $this->input->post('url'),
                    'Date' => $this->input->post('date'),
                    'Active' => $this->input->post('active')
                );

                $this->load->model('add_post_model');
                $this->add_post_model->InsertAdd($data);
                $data['message'] = 'Add Posted Successfully';
                $this->load->view('AddPostView', $data);
            }
        }

}


?>

型号:

   <?php

    class  add_post_model extends CI_Model
    {

        function __construct()
        {
            parent::__construct();
        }

        function InsertAdd($data)
        {

            $this->db->insert('Advertisements', $data);
        }

        function GetImageId()
        {
            /*$result = "SELECT img_id FROM advertisementnew ORDER BY img_id DESC LIMIT 1";*/
            $this->db->select('*');
            $this->db->select("ImgId");
            $this->db->from('Advertisements');
            $this->db->order_by("ImgId","desc");
            $this->db->limit(1);
            $query = $this->db->get();

            return $query->result();
        }
    }

    ?>

查看:

<html>
<body>
<head></head>
<?php echo form_open('AddPost'); ?>

<h1>Post Advertisements </h1><hr/>

<?php if (isset($message)) { ?>

    <h3 style="color:green;">Add Posted successfully</h3><br>
<?php }
$date = date('Y-m-d H:i:s');
$status = '1';


?>

<form method="POST" enctype="multipart/form-data">
    <label>URL:</label><input type="text" id="url" name="url" placeholder="Please Enter URL" value=""><br><br>
    <label>File: </label><input type="file" name="fileToUpload" id="fileToUpload" value=""/><br><br>
    <input type="hidden" id="active" name="active" value="<?php echo 
    <input type="hidden" id="date" name= "date" value="<?php echo $date; ?>">
    <input type="hidden" id="img_id" name="img_id" value="<?php echo  ($result)?>">$status; ?>">
    <input type="submit" id="submit" name="submit" value="Submit"/>
</form>

</body>

最佳答案

请输入<input type="file" name="userfile" id="fileToUpload" value=""/>而不是<input type="file" name="fileToUpload" id="fileToUpload" value=""/>并确保您的uploads目录放置在您的 application 之外文件夹

关于php - 如何将图像上传到codeigniter中的文件夹?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39324169/

相关文章:

php - 是否所有 MD5 十六进制哈希表示都以数字开头?

mysql - 实现 MySQL NDB Cluster 的限制是什么?

mysql - 我应该使用 mysql 来保存日志,还是只转储到文本文件

mysql - 将数据从查询传递到 View

php - 如何获取并显示 sql 表中某个字段的所有值?

php - 从服务器检索的JSON对象上的SearchView

PHP 为不同目录中的脚本指定一个固定的包含源

php - foreach 循环中的 while 循环不正确循环

php - 匹配两个不同表中的变量

php - Laravel groupBy 和原始查询