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

文件上传和使用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??

图片转代码服务由CSDN问答提供 功能建议

我在我的Album_Model中有这个功能:

  公共功能上传($ album_id,$ album){
 $ album = strtolower($ album); 
 
 $ upload_config = array(
'upload_path'=&gt;'./uploads /'.$ album,\  n'allow_types'=&gt;'jpg | jpeg | gif | png',
'max_size'=&gt;'2000',
'max_width'=&gt;'680',
'max_height'=&gt;'  435',
); 
 
 $ this-&gt; load-&gt; library('upload',$ upload_config); 
 
 //创建一个相册(如果上传中尚未存在)dir 
 // 如果在没有错误的情况下完成此部分,并且在上传之前完成此部分将不会更有意义吗?
 if(!is_dir('uploads')){
 mkdir('./ uploads',0777,true)  ; 
} 
 if(!is_dir('uploads /'.$ album)){
 mkdir('./ uploads /'.$ album,0777,true); 
} 
 
 if(!  $ this-&gt; upload-&gt; do_upload('imgfile')){
 // u  pload failed 
返回数组('error'=&gt;  $ this-&gt; upload-&gt; display_errors('&lt; span&gt;','&lt; / span&gt;')); 
} else {
 //上传成功
 $ upload_data = $ this-&gt; upload  - &gt; data(); 
返回true; 
} 
} 
   
 
 

所以这基本上是将图像上传到相册。 此外,如果文件夹专辑已经不存在,它会创建一个新专辑。

我正在做一些测试,发现可能是一个小错误的东西。 当我强迫我的表单执行一些无效的上传尝试并且上传失败时,正如预期的那样,但仍然创建了相册文件夹(假设相册文件夹不存在)。

如果在上传图像之前没有错误,那么创建相册文件夹是不是更有意义?如果是,我该如何修复? < / DIV>

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

3条回答 默认 最新

相关推荐 更多相似问题