2012-10-17 63 views
1

我一直在尝试使用CodeIgniter的文件上传类上传图片。该图像用于用户的头像。这里是我的代码片段:codeigniter图片上传成功,但文件未上传

//upload file 
      if($_FILES['image']['name'] != ''){ 
       if (!file_exists($dirpath)) 
        mkdir($dirpath); 

       $this->load->library('upload'); 
       $config['upload_path'] = $dirpath; 
       $config['allowed_types'] = 'gif|jpg|jpeg|png|GIF|JPG|JPEG|PNG'; 
       $config['max_size'] = '0'; 
       $config['max_width'] = '0'; 
       $config['max_height'] = '0'; 
       $config['file_name'] = $_FILES['image']['name']; 
       $config['overwrite'] = true; 
       $config['remove_spaces'] = false; 

       $this->upload->initialize($config); 
       $data = ""; 
       $error = ""; 

       $photo_result = $this->upload->do_upload('image'); 
       if ($photo_result) 
        if($action == 'update'){ 
         foreach (glob($dirpath.'/'.$old_image.'*') as $file) 
          if (file_exists($file))unlink ($file); 
        } 
        if($new_resto->resized == 0){ 
         if(resize_image($widths,$theuser)) 
          $this->usermodel->update($theuser->id, array('resized'=>1), 'object',$authorization); 
        } 
       echo json_encode(array('config'=>$config,'upload_data'=>$this->upload->data())); 
       } 
      } 

文件上载据说是成功的,因为当我打印出来$this->upload->data(),这表明上传数据不为空。奇怪的是,这段代码适用于创建新用户,但是当我尝试更新用户时,文件不会被上传。我已经尝试使用chmod($dirpath, 0777)之前上传,并且chmod($dirpath, 0755)之后,仍然没有运气。

任何想法?谢谢。

回答

2

我的错误,我在更新数据后分配了$old_image,所以$old_image包含当前图像名称,导致它在我拨打unlink($file)时被删除。

问题解决了:)

+0

有趣的是更新您的upload.php的课,我只是碰到了完全相同的神秘的问题,它在我的竟然是一个错误“删除旧的代码。感谢你的回答。 – Citizen

0

请以前版本的CodeIgniter