16 PHPExcel使用

这是用来读取上传的excel文件的,至于如何生成我还没细细研究,大家可以baidu
下载地址http://phpexcel.codeplex.com/

<?php
		if ($_FILES["uploadFile"]["size"] < 20000000){
	  		if ($_FILES["uploadFile"]["error"] > 0){
	    		echo "Return Code: " . $_FILES["uploadFile"]["error"] . "<br />";
	    	}else{
					$uploadFileTime	=	time();
		      		move_uploaded_file($_FILES["uploadFile"]["tmp_name"],"temp_excel/".$uploadFileTime.".xls");
	    	}
	   }
 
		require_once 'Classes/PHPExcel.php';
		$fileName 	= $uploadFileTime.".xls";
		$path 		= 'temp_excel/';
		$filePath 	= $path . $fileName;
		$PHPExcel 	= new PHPExcel ();
		$PHPReader 	= new PHPExcel_Reader_Excel2007 ();
		PHPExcel_CachedObjectStorageFactory::cache_to_discISAM;  
		if (! $PHPReader->canRead ( $filePath )) {
			$PHPReader = new PHPExcel_Reader_Excel5 ();
			if (! $PHPReader->canRead ( $filePath )) {
				echo 'Read Excel Failed! Please Contact Admin';
			}
		}
		$PHPExcel 		= $PHPReader->load ( $filePath );
		$currentSheet 	= $PHPExcel->getSheet ( 0 );
		// get cols
		$allColumn 		= $currentSheet->getHighestColumn ();
		// get rows
		$allRow 		= $currentSheet->getHighestRow () ;	
		for($currentRow = 1; $currentRow <= $allRow; $currentRow ++) {
			for($currentColumn = 'A'; $currentColumn <= $allColumn; $currentColumn ++) {
				$address = $currentColumn . $currentRow;
				$destArr[]	=	$currentSheet->getCell ($address)->getValue ();
			}
		}
?>

15 PHPmailer使用

这个例子也是从官方网站上面找来的,大家有空可以看看官网的例子
http://phpmailer.worxware.com/index.php?pg=examples

require_once('../class.phpmailer.php');
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded
 
$mail = new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch
 
$mail->IsSMTP(); // telling the class to use SMTP
 
try {
  $mail->Host       = "mail.yourdomain.com"; // SMTP server
  $mail->SMTPDebug  = 2;                     // enables SMTP debug information (for testing)
  $mail->SMTPAuth   = true;                  // enable SMTP authentication
  $mail->Host       = "mail.yourdomain.com"; // sets the SMTP server
  $mail->Port       = 26;                    // set the SMTP port for the GMAIL server
  $mail->Username   = "yourname@yourdomain"; // SMTP account username
  $mail->Password   = "yourpassword";        // SMTP account password
  $mail->AddReplyTo('name@yourdomain.com', 'First Last');
  $mail->AddAddress('whoto@otherdomain.com', 'John Doe');
  $mail->SetFrom('name@yourdomain.com', 'First Last');
  $mail->AddReplyTo('name@yourdomain.com', 'First Last');
  $mail->Subject = 'PHPMailer Test Subject via mail(), advanced';
  $mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
  $mail->MsgHTML(file_get_contents('contents.html'));
  $mail->AddAttachment('images/phpmailer.gif');      // attachment
  $mail->AddAttachment('images/phpmailer_mini.gif'); // attachment
  $mail->Send();
  echo "Message Sent OK<p></p>\n";
} catch (phpmailerException $e) {
  echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
  echo $e->getMessage(); //Boring error messages from anything else!
}

另外最近在公司用mail函数发送邮件时,配置相关参数,不让其乱码.

//php的mail函数发邮件时,配置相关参数,不让其乱码
$headers       = 'MIME-Version: 1.0' . "\r\n";
$headers     .= 'Content-type: text/html; charset=utf-8' . "\r\n";
// Additional headers
//$headers     .= 'To: sms-grp <test@test.com>' . "\r\n";
$headers     .= 'From: sms-grp <test@test.com>' . "\r\n";
$headers     .= 'Reply-To: sms-grp <test@test.com>' . "\r\n"; 
$subject     =     "=?UTF-8?B?".base64_encode('你好')."?=";
$message    =    "你好,<br />今晚有空吗?";

另外一种,改装版

function sendEmail($subject,$content,$toEmailArr,$attachArr='',$isHtml=true,$from='test@qq.com',$fromName='testName',$smtpUserName="",$smtpPass="")
{
        //include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded
        $mail = new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch
        $mail->IsSMTP(); // telling the class to use SMTP
 
        try
        {
            $mail->SMTPDebug  =     2;                             // enables SMTP debug information (for testing)
            $mail->SMTPAuth   =     true;                          // enable SMTP authentication
            $mail->Host       =     "smtp.qq.com";                 // sets the SMTP server
            $mail->Port       =     25;                            // set the SMTP port for the GMAIL server
            $mail->Username   =     $smtpUserName;                 // SMTP account username
            $mail->Password   =     $smtpPass;                    // SMTP account password
            $mail->Subject       =     $subject;
 
            $mail->AddReplyTo    ($from, $fromName);
            $mail->SetFrom        ($from, $fromName);
            if($isHtml==true)
            {
                $mail->MsgHTML($content);    
            }
            else
            {
                $mail->Body  =         $content;
            }
 
            if(!empty($toEmailArr) && is_array($toEmailArr))
            {
                foreach ($toEmailArr as $toEmail)
                {
                    $mail->AddAddress    ($toEmail,'');
                }
            }
            if(!empty($attachArr) && is_array($attachArr))
            {
                foreach ($attachArr as $attach)
                {
                    $mail->AddAttachment($attach);
                }
            }
            $mail->Send();
            return true;
        }
        catch (phpmailerException $e)
        {
            echo $e->errorMessage(); //Pretty error messages from PHPMailer
        }
        catch (Exception $e)
        {
            echo $e->getMessage(); //Boring error messages from anything else!
        }
        return false;
}

14 php验证码

<?php
//session_start();
//
//$captchas	=	new captcha();
//$word		=	$captchas->generate_word(8);
//$image		=	$captchas->generate_image();
//if($image==false){
//	echo 'false';
//}
//其中存储的SESSION值使用base64_encode加密
class captcha
{
    /**
     * 背景图片所在目录
     *
     * @var string  $folder
     */
    var $folder     = "data/captcha/";
 
    /**
     * 图片的文件类型
     *
     * @var string  $img_type
     */
    var $img_type   = 'png';
 
    /*------------------------------------------------------ */
    //-- 存在session中的名称
    /*------------------------------------------------------ */
    var $session_word = 'captcha_word';
 
    /**
     * 背景图片以及背景颜色
     *
     * 0 => 背景图片的文件名
     * 1 => Red, 2 => Green, 3 => Blue
     * @var array   $themes
     */
    var $themes_jpg = array(
        1 => array('captcha_bg1.jpg', 255, 255, 255),
        2 => array('captcha_bg2.jpg', 0, 0, 0),
        3 => array('captcha_bg3.jpg', 0, 0, 0),
        4 => array('captcha_bg4.jpg', 255, 255, 255),
        5 => array('captcha_bg5.jpg', 255, 255, 255),
    );
 
    var $themes_gif = array(
        1 => array('captcha_bg1.gif', 255, 255, 255),
        2 => array('captcha_bg2.gif', 0, 0, 0),
        3 => array('captcha_bg3.gif', 0, 0, 0),
        4 => array('captcha_bg4.gif', 255, 255, 255),
        5 => array('captcha_bg5.gif', 255, 255, 255),
    );
 
    /**
     * 图片的宽度
     *
     * @var integer $width
     */
    var $width      = 130;
 
    /**
     * 图片的高度
     *
     * @var integer $height
     */
    var $height     = 20;
 
    /**
     * 构造函数
     *
     * @access  public
     * @param   string  $folder     背景图片所在目录
     * @param   integer $width      图片宽度
     * @param   integer $height     图片高度
     * @return  bool
     */
    function captcha($folder = '', $width = 145, $height = 20)
    {
        if (!empty($folder))
        {
            $this->folder = $folder;
        }
 
        $this->width    = $width;
        $this->height   = $height;
 
        /* 检查是否支持 GD */
        if (PHP_VERSION >= '4.3')
        {
 
            return (function_exists('imagecreatetruecolor') || function_exists('imagecreate'));
        }
        else
        {
 
            return (((imagetypes() & IMG_GIF) > 0) || ((imagetypes() & IMG_JPG)) > 0 );
        }
    }
 
    /**
     * 构造函数
     *
     * @access  public
     * @param
     *
     * @return void
     */
    function __construct($folder = '', $width = 145, $height = 20)
    {
        $this->captcha($folder, $width, $height);
    }
 
 
    /**
     * 检查给出的验证码是否和session中的一致
     *
     * @access  public
     * @param   string  $word   验证码
     * @return  bool
     */
    function check_word($word)
    {
        $recorded = isset($_SESSION[$this->session_word]) ? base64_decode($_SESSION[$this->session_word]) : '';
        $given    = $this->encrypts_word(strtoupper($word));
 
        return (preg_match("/$given/", $recorded));
    }
 
    /**
     * 生成图片并输出到浏览器
     *
     * @access  public
     * @param   string  $word   验证码
     * @return  mix
     */
    function generate_image($word = false)
    {
        if (!$word)
        {
            $word = $this->generate_word();
        }
 
        /* 记录验证码到session */
        $this->record_word($word);
 
        /* 验证码长度 */
        $letters = strlen($word);
 
        /* 选择一个随机的方案 */
        mt_srand((double) microtime() * 1000000);
 
        if (function_exists('imagecreatefromjpeg') && ((imagetypes() & IMG_JPG) > 0))
        {
            $theme  = $this->themes_jpg[mt_rand(1, count($this->themes_jpg))];
        }
        else
        {
            $theme  = $this->themes_gif[mt_rand(1, count($this->themes_gif))];
        }
		//print_r($theme);
        if (!file_exists($this->folder . $theme[0]))
        {
            return false;
        }
        else
        {
            $img_bg    = (function_exists('imagecreatefromjpeg') && ((imagetypes() & IMG_JPG) > 0)) ?
                            imagecreatefromjpeg($this->folder . $theme[0]) : imagecreatefromgif($this->folder . $theme[0]);
            $bg_width  = imagesx($img_bg);
            $bg_height = imagesy($img_bg);
 
            $img_org   = ((function_exists('imagecreatetruecolor')) && PHP_VERSION >= '4.3') ?
                          imagecreatetruecolor($this->width, $this->height) : imagecreate($this->width, $this->height);
 
            /* 将背景图象复制原始图象并调整大小 */
            if (function_exists('imagecopyresampled') && PHP_VERSION >= '4.3') // GD 2.x
            {
                imagecopyresampled($img_org, $img_bg, 0, 0, 0, 0, $this->width, $this->height, $bg_width, $bg_height);
            }
            else // GD 1.x
            {
                imagecopyresized($img_org, $img_bg, 0, 0, 0, 0, $this->width, $this->height, $bg_width, $bg_height);
            }
            imagedestroy($img_bg);
 
            $clr = imagecolorallocate($img_org, $theme[1], $theme[2], $theme[3]);
 
            /* 绘制边框 */
            //imagerectangle($img_org, 0, 0, $this->width - 1, $this->height - 1, $clr);
 
            /* 获得验证码的高度和宽度 */
            $x = ($this->width - (imagefontwidth(5) * $letters)) / 2;
            $y = ($this->height - imagefontheight(5)) / 2;
            imagestring($img_org, 5, $x, $y, $word, $clr);
 
            header('Expires: Thu, 01 Jan 1970 00:00:00 GMT');
 
            // HTTP/1.1
            header('Cache-Control: private, no-store, no-cache, must-revalidate');
            header('Cache-Control: post-check=0, pre-check=0, max-age=0', false);
 
            // HTTP/1.0
            header('Pragma: no-cache');
            if ($this->img_type == 'jpeg' && function_exists('imagecreatefromjpeg'))
            {
                header('Content-type: image/jpeg');
                imageinterlace($img_org, 1);
                imagejpeg($img_org, false, 95);
            }
            else
            {
                header('Content-type: image/png');
                imagepng($img_org);
            }
 
            imagedestroy($img_org);
 
            return true;
        }
    }
 
    /*------------------------------------------------------ */
    //-- PRIVATE METHODs
    /*------------------------------------------------------ */
 
    /**
     * 对需要记录的串进行加密
     *
     * @access  private
     * @param   string  $word   原始字符串
     * @return  string
     */
    function encrypts_word($word)
    {
        return substr(md5($word), 1, 10);
    }
 
    /**
     * 将验证码保存到session
     *
     * @access  private
     * @param   string  $word   原始字符串
     * @return  void
     */
    function record_word($word)
    {
        $_SESSION[$this->session_word] = base64_encode($this->encrypts_word($word));
    }
 
    /**
     * 生成随机的验证码
     *
     * @access  private
     * @param   integer $length     验证码长度
     * @return  string
     */
    function generate_word($length = 4)
    {
        $chars = '23456789ABCDEFGHJKLMNPQRSTUVWXYZ';
 
        for ($i = 0, $count = strlen($chars); $i < $count; $i++)
        {
            $arr[$i] = $chars[$i];
        }
 
        mt_srand((double) microtime() * 1000000);
        shuffle($arr);
 
        return substr(implode('', $arr), 5, $length);
    }
}
 
?>