<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Logo_save extends CI_Controller {

	function __construct(){
		        parent::__construct();
        	    $this->load->helper('url');
        	    $this->load->helper('form');
                $this->load->library('session'); //加载session
		        $this->load->model('CsdjDB');
		        $this->load->model('CsdjSkins');
		        $this->load->model('CsdjUser');
		        $this->CsdjUser->User_Login();
	}

    //头像修改
	public function index()
	{


                //if(!empty($_POST)){ //FLASH上传

                    $tempFile = file_get_contents("php://input");
		            $picname=$this->session->userdata('cs_id').".jpg"; 
		            $filename = "./attachment/logo/".$this->session->userdata('cs_id').".jpg"; 

		            if (!empty($tempFile)) {

	    	    		if($handle=fopen($filename,"w+")) {   
		    	    		if(!fwrite($handle,$tempFile)==FALSE)
		    	    		{   
		    	    			fclose($handle);
		    	    		}
	    	    		} 
 
                        //生成小图
		                $this->load->model('CsdjPic');
						$this->CsdjPic->resize($filename,160,160);
                        if($this->CsdjPic->resize($filename.'.small.jpg',80,80)){

                                    if(FTP_Fun==1){  //开启远程附件
                                          $this->load->library('ftp');
                                          $FTPUP['hostname'] = FTP_Server;
                                          $FTPUP['port']     = FTP_Port;
                                          $FTPUP['username'] = FTP_Name;
                                          $FTPUP['password'] = FTP_Pass;
                                          $FTPUP['passive']  = FTP_Ive;
                                          $this->ftp->connect($FTPUP);

                                          $targetPath = './attachment/logo/';
                                          //----------创建目录------------------------
                                          $Dirs=FTP_Dir;
                                          if(substr($Dirs,0,1)=='/') $Dirs=substr($Dirs,1);
                                          if(substr($Dirs,-1)=='/') $Dirs=substr($Dirs,0,-1);
                                          if(!empty($Dirs)) $Dirs.='/';
                                          $All=explode("/",$targetPath);
                                          $Dir1='./'.$Dirs.$All[1].'/';
                                          $this->ftp->mkdir($Dir1, DIR_WRITE_MODE); 
                                          $Dir2=$Dir1.$All[2].'/';
                                          $this->ftp->mkdir($Dir2, DIR_WRITE_MODE); 

                                          //----------上传文件------------------------
                                          $this->ftp->mkdir($targetPath, DIR_WRITE_MODE); 
                                          $filenames=$Dir2.$this->session->userdata('cs_id').".jpg";
                                          $this->ftp->upload($filename.'.small.jpg', $filenames);
                                          $this->ftp->upload($filename.'.small.jpg.small.jpg', $filenames.'.small.jpg');
                                          $this->ftp->close();

	                                  @unlink($filename);
									  @unlink($filename.'.small.jpg');
	                                  @unlink($filename.'.small.jpg.small.jpg');

                                     }

                                     //写入数据库
                                     $this->db->query("update ".CS_SqlPrefix."user set cs_logo='".$this->session->userdata('cs_id').".jpg.small.jpg' where cs_id='".$this->session->userdata('cs_id')."'");
                                     exit('UploadPicSucceed');
                                }
		            } else {
			            exit('UploadPicError');
		            }

                //}
                
	}
}