dp411805872 2013-12-18 07:56
浏览 240
已采纳

文件上传和使用Codeigniter创建上传文件夹

I'm having this function inside my Album_Model:

public function upload($album_id, $album){
        $album= strtolower($album);

        $upload_config = array(
            'upload_path'   =>  './uploads/'.$album,
            'allowed_types' =>  'jpg|jpeg|gif|png',
            'max_size'      =>  '2000',
            'max_width'     =>  '680',
            'max_height'    =>  '435', 
        );

        $this->load->library('upload', $upload_config);

        // create an album if not already exist in uploads dir
// wouldn't make more sence if this part is done if there are no errors and right before the upload ??
        if (!is_dir('uploads')) {
            mkdir('./uploads', 0777, true);
        }
        if (!is_dir('uploads/'.$album)) {
            mkdir('./uploads/'.$album, 0777, true);
        }

        if (!$this->upload->do_upload('imgfile')) { 
            // upload failed
            return array('error' => $this->upload->display_errors('<span>', '</span>'));
        } else {
            // upload success
            $upload_data = $this->upload->data();
            return true;
        }
    }

So what this basicly does is to upload images to an album. Also if the folder album does not exist already, it creates a new album.

I was doing some tests and found out something that might be a small bug. While I was forcing my form to do some invalid upload attempts and the upload fails, as expected, but the album folder (suppose the album folder doesn't exist) is still created.

Whouldn't make more sence to create the album folder if there are no erros and right before uploading the image and if yes how can I fix this??

  • 写回答

3条回答 默认 最新

  • dongzha5934 2013-12-18 08:13
    关注

    You have set a flag for checking the directory existence. I have changed your code to make it work as per your need.

    <?php
    
    public function upload($album_id, $album)
    {
        $album = strtolower($album);
    
        $upload_config = array('upload_path' => './uploads/' . $album, 'allowed_types' =>
            'jpg|jpeg|gif|png', 'max_size' => '2000', 'max_width' => '680', 'max_height' =>
            '435', );
    
        $this->load->library('upload', $upload_config);
    
        // create an album if not already exist in uploads dir
        // wouldn't make more sence if this part is done if there are no errors and right before the upload ??
        if (!is_dir('uploads'))
        {
            mkdir('./uploads', 0777, true);
        }
        $dir_exist = true; // flag for checking the directory exist or not
        if (!is_dir('uploads/' . $album))
        {
            mkdir('./uploads/' . $album, 0777, true);
            $dir_exist = false; // dir not exist
        }
        else{
    
        }
    
        if (!$this->upload->do_upload('imgfile'))
        {
            // upload failed
            //delete dir if not exist before upload
            if(!$dir_exist)
              rmdir('./uploads/' . $album);
    
            return array('error' => $this->upload->display_errors('<span>', '</span>'));
        } else
        {
            // upload success
            $upload_data = $this->upload->data();
            return true;
        }
    }
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教