Archive for the 'PHP' Category

PHP导入导出Excel方法小结

PHP 无评论 »

最近因项目需要,需要开发一个模块,把系统中的一些数据导出成Excel,修改后再导回系统。就趁机对这个研究了一番,下面进行一些总结
基本上导出的文件分为两种:
1:类Excel格式,这个其实不是传统意义上的Excel文件,只是因为Excel的兼容能力强,能够正确打开而已。修改这种文件后再保存,通常会提示你是否要转换成Excel文件。
优点:简单。
缺点:难以生成格式,如果用来导入需要自己分别编写相应的程序。
2:Excel格式,与类Excel相对应,这种方法生成的文件更接近于真正的Excel格式。

如果导出中文时出现乱码,可以尝试将字符串转换成gb2312,例如下面就把$yourStr从utf-8转换成了gb2312:
$yourStr = mb_convert_encoding(”gb2312″, “UTF-8″, $yourStr);

下面详细列举几种方法。
一、PHP导出Excel

1:第一推荐无比风骚的PHPExcel,官方网站: http://www.codeplex.com/PHPExcel
导入导出都成,可以导出office2007格式,同时兼容2003。
下载下来的包中有文档和例子,大家可以自行研究。
抄段例子出来:
<?php
/**
* PHPExcel
*
* Copyright (C) 2006 - 2007 PHPExcel
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*
* @category   PHPExcel
* @package    PHPExcel
* @copyright  Copyright (c) 2006 - 2007 PHPExcel ( http://www.codeplex.com/PHPExcel)
* @license    http://www.gnu.org/licenses/lgpl.txt    LGPL
* @version    1.5.0, 2007-10-23
*/

/** Error reporting */
error_reporting(E_ALL);

/** Include path **/
set_include_path(get_include_path() . PATH_SEPARATOR . ‘../Classes/’);

/** PHPExcel */
include ‘PHPExcel.php’;

/** PHPExcel_Writer_Excel2007 */
include ‘PHPExcel/Writer/Excel2007.php’;

// Create new PHPExcel object
echo date(’H:i:s’) . ” Create new PHPExcel object\n”;
$objPHPExcel = new PHPExcel();

// Set properties
echo date(’H:i:s’) . ” Set properties\n”;
$objPHPExcel->getProperties()->setCreator(”Maarten Balliauw”);
$objPHPExcel->getProperties()->setLastModifiedBy(”Maarten Balliauw”);
$objPHPExcel->getProperties()->setTitle(”Office 2007 XLSX Test Document”);
$objPHPExcel->getProperties()->setSubject(”Office 2007 XLSX Test Document”);
$objPHPExcel->getProperties()->setDescrīption(”Test document for Office 2007 XLSX, generated using PHP classes.”);
$objPHPExcel->getProperties()->setKeywords(”office 2007 openxml php”);
$objPHPExcel->getProperties()->setCategory(”Test result file”);

// Add some data
echo date(’H:i:s’) . ” Add some data\n”;
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->setCellValue(’A1′, ‘Hello’);
$objPHPExcel->getActiveSheet()->setCellValue(’B2′, ‘world!’);
$objPHPExcel->getActiveSheet()->setCellValue(’C1′, ‘Hello’);
$objPHPExcel->getActiveSheet()->setCellValue(’D2′, ‘world!’);

// Rename sheet
echo date(’H:i:s’) . ” Rename sheet\n”;
$objPHPExcel->getActiveSheet()->setTitle(’Simple’);

// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);

// Save Excel 2007 file
echo date(’H:i:s’) . ” Write to Excel2007 format\n”;
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
$objWriter->save(str_replace(’.php’, ‘.xlsx’, __FILE__));

// Echo done
echo date(’H:i:s’) . ” Done writing file.\r\n”;

阅读全文 »

Tags »||

@139.com 邮箱实现服务器监测通知

PHP 无评论 »

当你手上管理着好几台服务器,需要及时获取这些服务器网络断开或死机的消息通知,那么手机短信通知将是最好的途径。这点小事情不需要开通什么SMS短信端口的,你只要注册一个 @139.com 邮箱即可,该邮箱带有手机短信通知功能(好像通过手机查看邮件内容是需要付费的 _-!! 但我只需知道 web 服务是否正常,就把服务器信息简单的写在标题即可)
使用 PHP 监测 web 服务 80 端口是否正常:
阅读全文 »

Tags »

PHP autoload机制详解 (转)

PHP 无评论 »

PHP autoload机制详解

2008.12.13注:关于SPL的autoload的函数手册,请参考 SPL函数手册:

http://www.phpinternals.com/blog/2008/12/translate-refs-spl-functions/

(1) autoload机制概述

在使用PHP的OO模式开发系统时,通常大家习惯上将每个类的实现都存放在一个单独的文件里,这样会很容易实现对类进行复用,同时将来维护时也很便利。 这也是OO设计的基本思想之一。在PHP5之前,如果需要使用一个类,只需要直接使用include/require将其包含进来即可。下面是一个实际的 例子:

/* Person.class.php */
<?php
class Person {
var $name, $age;

function __construct ($name, $age)
{
$this->name = $name;
$this->age = $age;
}
}
?>

/* no_autoload.php */
<?php
require_once (”Person.class.php”);

$person = new Person(”Altair”, 6);
var_dump ($person);
?>

在这个例子中,no-autoload.php文件需要使用Person类,它使用了require_once将其包含,然后就可以直接使用Person类来实例化一个对象。

但随着项目规模的不断扩大,使用这种方式会带来一些隐含的问题:如果一个PHP文件需要使用很多其它类,那么就需要很多的 require/include语句,这样有可能会造成遗漏或者包含进不必要的类文件。如果大量的文件都需要使用其它的类,那么要保证每个文件都包含正确 的类文件肯定是一个噩梦。

PHP5为这个问题提供了一个解决方案,这就是类的自动装载(autoload)机制。autoload机制可以使得PHP程序有可能在使用类时才自动包含类文件,而不是一开始就将所有的类文件include进来,这种机制也称为lazy loading。

阅读全文 »

Tags »|

ZF与FCKeditor集成完全攻略(包括上传、浏览服务器图像)

PHP 无评论 »

FCKedtior是目前最流行和最强大的HTML在线编辑器之一,非常好用。本文介绍如何集成Zend framework和FCKeditor。

首先基本配置的修改,网上很多文章都有写,在此不再多说,最权威的资料当然是FCKeditor官网的:
http://wiki.fckeditor.net/Developer%27s_Guide/Integration/PHP

最主要是修改fckconfig.js文件。

下面说说和ZF的集成,以及一些需要注意的地方:

1。FCKeditor的安装
FCKeditor需要放在public的目录,即通过网站可以访问到的目录下。比如wwwroot/js/FCKeditor。

新版的FCKeditor需要加载一些配置XML文件,所以在你的Apache配置中,XML文件不能转向到ZF的bootstrap。
另外,下面的一些功能需要用到一些PHP文件,而在ZF项目中通常禁用对PHP文件的直接访问。

这些你都可以通过修改Apache服务器的URL重写规则来搞定:
RewriteCond %{REQUEST_URI} !^.*(\.html|\.xml|\.css|\.js|\.gif|\.png|\.jpg|\.jpeg)$|.*(FCKeditor).*

这样,XML文件不转向至index.php,而访问地址中包含FCKeditor字符的php文件也能直接访问。

2。集成

在你的模板文件中(即ZF的View部分,例如views/article/articleForm.php),需要添加表单的地方加入以下代码:

PHP代码如下:

<?php
$oFCKeditor
= new FCKeditor(‘ArticleBody’) ;
$oFCKeditor->BasePath = “/js/FCKeditor/”;
$oFCKeditor->Width = ‘600′;
$oFCKeditor->Height = ‘500′;
if(
$this->article->ArticleBody!=){
$oFCKeditor->Value = $this->article->ArticleBody;
}else{
$oFCKeditor->Value = ;
}
$oFCKeditor->Create();
?>

这个表单可以同时用于添加和编辑文章,当文章内容不为空(即当前操作为编辑时),显示文章内容,否则为新增文章,表单内容为空。

提交本页面后,你可以使用$_POST['ArticleBody']来获取表单中的文字内容。

2。浏览服务器图像
在写文章的时候,希望可以直接浏览服务器上的图像,添加到文章里,就需要这个功能。
FCKeditor中本功能的实现在FCKeditor\editor\filemanager\browser\default\connectors\php目录的几个文件。

我们只需要修改
FCKeditor\editor\filemanager\browser\default\connectors\php\config.php

PHP代码如下:
$Config['Enabled'] = true ; //一定要设定成true,本功能才启用
$Config['UserFilesPath'] = ‘/UserFiles/’ ; //图像文件所在的目录,你可以根据自己的需要修改

设定好后你可以通过ftp上传图像文件到/UserFiles/image目录下,测试一下是否可以浏览。

注意:默认情况下,FCKeditor的图像文件要放在UserFiles下的image目录里,而不能直接放在UserFiles目录里。

3。图像上传

如果你想在写文章的时候,直接上传图像到服务器,然后插入到文章中,可以用这个功能。
FCKeditor中本功能的实现在FCKeditor\editor\filemanager\upload\php目录的几个文件。

我们也只需要修改该目录下的config.php

PHP代码如下:
$Config['Enabled'] = true ;
$Config['UserFilesPath'] = ‘/UserFiles/’ ; //上传目录的路径,通常和上面的浏览部分的路径设成一样的
$Config['UseFileType'] = true ; //不同上传文件类型是否分目录放置,图像文件会自动被上传到/UserFiles/image目录下,Flash则在/UserFiles/flash目录下

注意:UserFiles目录和其下的image目录要可写权限。

Tags »|

Xdebug的远程调试

PHP 无评论 »

首先这个 php.ini的配置中 不需要 加上extension=xdebug.so
用zend_extension=”/path/xdebug.so”  path是完整的路径
然后打开远程调试
xdebug.remote_enable=1

自动开始也打开
xdebug.remote_autostart=1

把所有变量都dump出来 (页面会很乱的噢)
xdebug.show_local_vars=1

这个填调试的这个客户端的地址,比如server是10.0.0.1  调试的IDE(我用的是NetBeans 6.5 for osx)的机子的IP是10.0.0.34 如果是本机调试的话填localhost  或者不设置这个选项就好了
xdebug.remote_host=10.0.0.1

其它的用默认的就可以了

如果还想用特定的编辑器打开发生错误的文件连接的话可以设置
xdebug.file_link_format = “txmt://open?url=file://%f&line=%l”

我用的连接是txmt  这个是用TextMate打开的协议吧,其它的没试过

重启apache就可以看见 Xdebug加载的样子了

接着打开NetBeans
使用工具栏的Debug(快捷键  ⌂⌘F5),就可以调试文件了.默认是 支持Xdebug的基本不用修改,除非9000的端口被占用了,更改的话,php.ini里头的 xdebug.remote_port也改成相应的端口号

Tags »

phptelnet for cakephp

PHP 无评论 »

最近有用到在PHP telnet到远程的服务器上校验用户密码的状态,在网上扒了一个telnet的类
然后改在cakephp上使用,多加了一些功能,比如登录的时候要输入域,还有判断登录成功的字符串.还可以自定义端口号
更新:
11.25
while (!$s['time_out'] && $s['unread_bytes']); //多加一个超时判断,这样就比较快了

测试环境:
OSX 10.5.5
Xampp(apache2.2 php5.2.6 mysql5.0.1)
telnetServer 华为,爱立信的某些告警交换机, Solaris5.9
Bug:
不支持Telnet到Windows
telnet到windows自带的Telnet Server有问题,总是不能登录.大概问题是返回的结果需要进行一定的协议处理.

阅读全文 »

Tags »|

PHP调用Webservice实例

PHP 无评论 »

NuSoap是PHP环境下的WebService编程工具,用于创建或调用WebService。它是一个开源软件,是完全采用PHP语言编写的、通过HTTP收发SOAP消息的一系列PHP类,由NuSphere Corporation(http://dietrich.ganx4.com/nusoap/ )开发。NuSOAP的一个优势是不需要扩展库的支持,这种特性使得NuSoap可以用于所有的PHP环境,不受服务器安全设置的影响。

方法一:直接调用

'username', 'strPassword'=>MD5('password'));

// 调用远程函数
$aryResult = $client->call('login',$aryPara);

//echo $client->debug_str;
/*
if (!$err=$client->getError()) {
  print_r($aryResult);
} else {
  print "ERROR: $err";
}
*/

$document=$client->document;
echo <<

   $document

SoapDocument;

?>

阅读全文 »

Tags »||

PHP5.2的php.ini详解中文配置

PHP 无评论 »

php5.2 的 php.ini 中文版
;;;;;;;;;;;;;;
;;    简介    ;;
;;;;;;;;;;;;;;
; 本文并非是对英文版 php.ini 的简单翻译,而是参考了众多资料以后,结合自己的理解,增加了许多内容,
; 包括在原有 php.ini 基础上增加了一些实用模块的配置说明,同时对文件内容的安排进行了调整。
; 由于作者不喜欢 no-free 的玩意儿,所以删除了除 MySQL 和 PostgreSQL 以外的其他数据库模块配置选项。
阅读全文 »

Tags »|

php内部字符串编码转换函数mb_convert_encoding使用方法

PHP 无评论 »

mb_convert_encoding 函数为php内部多字节字符串编码转换函数,可以在有需要的使用场合(如:解决在GB2312编码环境下使用Ajax产生的中文字乱码的问题)方便进行编码转换,以解决网页乱码的问题,使用非常方便,效率非常高,
几乎支持所有编码。PHP 4 >= 4.0.6、PHP 5 版本支持。
阅读全文 »

Tags »|

PHP搜索引擎——Zend_Search

PHP 无评论 »

2007 年08月15日 星期三 15:40Zend_Search_Lucene 是一个完全由 PHP 5 编写的通用文本搜索引擎。由于其将索引保存在文件系统中而不需要数据库支持,因此它几乎可以为任何由 PHP 驱动的网站增加搜索能力。Zend_Search_Lucene 支持下列特性:

具有排名功能的搜索——最符合要求的结果出现在最前面

许多强大的查询类型:短语查询、通配符查询、近似查询、范围查询等

搜索特定的字段,如标题、作者、内容,等等

Zend_Search_Lucene 来源于 Apache Lucene project。要了解关于 Lucene 的更多详情,请访问 http://lucene.apache.org/java/docs/

阅读全文 »

Tags »||

PHP负载均衡指南

PHP 无评论 »

过去当运行一个大的web应用时候意味着运行一个大型的web服务器。因为你的应用吸引了大量的用户,你将不得不在你的服务器里增加更多的内存和处理器。

今天,’大型服务器’模式已经过去,取而代之的是大量的小服务器,使用各种各样的负载均衡技术。这是一种更可行的方法,将使硬件成本降至最低。

‘更多小服务器’的优势超过过去的’大型服务器’模式体现在两个方面:
阅读全文 »

Tags »|

php中处理ip地址字符串

PHP 无评论 »

//根据ip地址和掩码位数获取网络地址
//192.168.10.2/24 —>> 192.168.10.0
阅读全文 »

Tags »

PHP下默认的日期格式设置

PHP 无评论 »

因为原本的模板是英文的,很多小地方都需要做一些改动调整,比如时间显示。顺手查了一下WordPress的时间函数,以及调用的时候的时间参数:

阅读全文 »

Tags »|

Apache-MySQL-PHP on my phone, and it wasn’t even hard

Apache, MySQL, PHP, 网络技术 无评论 »

Nokia E90 安装 PAMP

阅读全文 »

Tags »||||

Writing private attributes to a database using AMF in AS3 (IExternalizable)

AS3, Flex/Flash, PHP, Remoting 无评论 »

Yesterday may have been one of the most frustrating days of my entire career. The brief - simple; take a bunch of classes acting as models (with a single top-level model containing all others in a tree hierarchy) and write their contents to a database. Client-side technology is Flash CS3 with Actionscript 3, server-side technologies are PHP 5 and MySQL. After having a look at the problem I decided that the quickest and most elegant way of achieving this would be to use Flash Remoting with AMFPHP on the server and to use custom class mapping to effectively pass the top level model and all its children to the server so they arrive with the same class structure as they left.

阅读全文 »

Tags »||||