Codelgniter将图像上传到服务器

时间:2016-01-30 10:29:36

标签: php codeigniter file-upload image-uploading

我在Codelgniter框架中建立了我的网站,我使用上传图片库,我的代码在localhost中运行正常,但在实时服务器中的图像,有时上传很好,有时不工作,当代码不工作没有错误消息显示,但只是重定向到主控制器www.mywebsite.com/admino。 这是我的代码

    class admino extends CI_Controller {
private $Data;
public function create_bar()
    { 
      $this->IsLoginAdmin();
      $this->CommunData();
      $p=$this->input->get('p',TRUE);
      $type=$this->input->get('type',TRUE);
      $this->Data['position']=$p;
      $this->Data['type']=$type;
      $oldpub= $this->m_admin->GetPubByPosition($p);
      $this->Data['pub']=$oldpub;
      switch($type)
      {
        case 'img':
        $this->load->view('admin/upload_pub',$this->Data);
        break;
        case 'script':
        if(iSset($_POST['upload']))   
        {
          $pub_script = $this->input->post('pub_script');
          $this->m_admin->add_pub($pub_script,$type,$p);
          redirect('admino/create_bar/?p='.$p.'&type=script','refresh');
        } else 
        {
          $this->load->view('admin/upload_pub',$this->Data);
        }
        break;    
      }
    }
public function upload_pub()
    {
     $this->IsLoginAdmin();
     $status = "";
     $msg = "";
     $logo = "";
     $p=$this->input->get('p',TRUE);
     $type=$this->input->get('type',TRUE);
     $this->Data['position']=$p;
     $this->Data['type']=$type;
     $oldpub= $this->m_admin->GetPubByPosition($p);
     $this->Data['pub']=$oldpub;
     if ($status != "error")
     {
        $this->load->helper('url');
        $this->load->library('image_lib');
        $config['upload_path'] = './images/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = 1024 * 8;
        $config['encrypt_name'] = true;
        $this->load->library('upload', $config);
        if (!$this->upload->do_upload('userfile'))
        {
            $status = 'error';
            $msg = $this->upload->display_errors('','');
            $this->session->set_flashdata('upload_state',$msg);
        }
        else
        {
            $data = $this->upload->data();
            var_dump($data);
            $this->m_admin->add_pub($data['file_name'],$type,$p);
            if(!empty($oldpub))
             {
             $filestring = PUBPATH.'/img/'.$oldpub[0]['pub_object'];
             unlink($filestring);
             }
            $status = "success";
            $msg = "Publicité à été ajouté";
            $this->session->set_flashdata('upload_state',$msg);
        }
        @unlink($_FILES['upload']);
     }
     redirect('admino/create_bar/?p='.$p.'&type=img','refresh');
    }
}

0 个答案:

没有答案