【问题标题】:Codeigniter 2.1 - upload images first and then create gallery in dbCodeigniter 2.1 - 先上传图片,然后在数据库中创建图库
【发布时间】:2013-03-25 16:06:46
【问题描述】:

我需要一些建议,我该怎么做: 我制作了通过 HTML5 拖放功能上传图像的脚本,它正在工作。我想创建一个系统,用户在其中放置要上传的图像(并上传),写下画廊的名称和描述,然后单击提交,需要一个函数来创建画廊,提供 ID 并将该 ID 提供给图像并将图像传输到指定文件夹(画廊名称)。目前我不知道我该怎么做。此外,如果用户决定不创建图库,则需要删除上传的图像(我想这可以通过 cron job 完成)。

图库数据库如下:

gallery
    id_gallery
    name
    description
    date_created

gallery_images
    id_image
    gallery_id
    path

【问题讨论】:

    标签: php mysql codeigniter codeigniter-2


    【解决方案1】:

    您无需将文件移动到另一个文件夹。只要应用程序可以找到它们,它们住在哪里都没关系(请参阅:path)。

    您对数据库的计划是什么?您展示了架构,但提到您不知道如何使用它?

    无论如何...如果您将active 字段添加到图库表中,则可以在提交时将其标记为活动。

    gallery
        id_gallery int
        name text
        description text
        date_created date
        active default 0
    
    gallery_images
        id_image int
        gallery_id int
        path text
    

    如果您在上传第一张图片时(或在某个时间点)实例化一个图库,并为每张上传的图片在 gallery_images 中插入一行,您可以像这样提交:

    function submit_gallery($id = NULL, $active = NULL)
    {
        if($id AND $active === 1)
        {
            $this->db->where('id', $id);
            $this->db->update('gallery', array('active' => 1));
        }
    
        //could have a "cancel" button call this. or use a cron job
        elseif($id AND $active === 0)
        {
    
            $this->db->where('gallery_id', $id);
            $imgs = $this->db->get('gallery_images');
    
            foreach($imgs->result() as $img)
            {
                //delete the img files
                unlink($img->path);
            }
    
            $this->db->where('gallery_id', $id);
            $this->db->delete('gallery_images');
    
        }
    }
    

    【讨论】:

    • 此方法在首次上传图片时创建图库时有效,但在我的情况下,用户首先上传图片(拖放它们),然后(在写入有关图库的数据后)他单击 创建图库。这种方式在创建图像之前上传图像。目前我正在尝试使用另一个临时包含图像的表,并且在创建图库时,该表中的数据将被复制到 gallery_images 并从临时表中删除。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-19
    • 2012-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多