php文件函数windows系统编码bug

最近在windows的php环境突然发现windows的php的文件函数文件名都是使用的GBK编码没有使用utf-8编码。于是需要手动转换一下编码。当然了linux系统都是使用的utf-8编码就没有这些问题啦。

我写了一些常用的函数的包装,供大家方便使用

$Server_System;
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
    $Server_System = 'windows';//filename will be changed into utf-8 in windows
} else {
    $Server_System = 'linux';
}

function my_file_exists($path)
{
    global $Server_System;
    if($Server_System == 'windows')
    {
        $path = iconv('utf-8','gbk',$path);
    }
    return file_exists($path);
}

function my_file_get_contents($path)
{
    global $Server_System;
    if($Server_System == 'windows')
    {
        $path = iconv('utf-8','gbk',$path);
    }
    return file_get_contents($path);
}

function my_filemtime($path)
{
    global $Server_System;
    if($Server_System == 'windows')
    {
        $path = iconv('utf-8','gbk',$path);
    }
    return filemtime($path);
}

function my_file_put_contents($path,$data)
{
    global $Server_System;
    if($Server_System == 'windows')
    {
        $path = iconv('utf-8','gbk',$path);
    }
    return file_put_contents($path,$data);
}

function my_is_dir($path)
{
    global $Server_System;
    if($Server_System == 'windows')
    {
        $path = iconv('utf-8','gbk',$path);
    }

    return is_dir($path);
}

function my_md5_file($path)
{
    global $Server_System;
    if($Server_System == 'windows')
    {
        $path = iconv('utf-8','gbk',$path);
    }
    
    return md5_file($path);
}

function my_sha1_file($path)
{
    global $Server_System;
    if($Server_System == 'windows')
    {
        $path = iconv('utf-8','gbk',$path);
    }
    
    return sha1_file($path);
}

轻量级php文件同步:LIGHTphpCDN

嘛,很不要脸的来推广自己的项目了。一个开源的轻量级的PHP写的文件同步系统。当然目的是为了管理CDN的了,所以名字也就叫CDN了,至于是不是CDN嘛,看你怎么用啦。

为什么要写这个项目呢,主要是前一段时间被我的php空间恶心到了,由于是虚拟主机,只能使用ftp进行上传。由于想利用php免费空间建立下载镜像,所以就一怒之下写了这个软件。

软件的说明什么的在项目目录里面,不过是英文写的罢了。

项目主页:https://github.com/manageryzy/Light-php-CDN

 

PHP OOP快速入门

http://www.phpchina.com/archives/view-11749-1.html

面向对象编程(OOP)是我们编程的一项基本技能,PHP4对OOP提供了良好的支持。如何使用OOP的思想来进行PHP的高级编程,对于提高PHP编程能力和规划好Web开发构架都是非常有意义的。下面我们就通过实例来说明使用PHP的OOP进行编程的实际意义和应用方法。

我们通常在做一个有数据库后台的网站的时候,都会考虑到程序需要适用于不同的应用环境。和其他编程语言有所不同的是,在PHP中,操作数据库的是一系列的具体功能函数(如果你不使用ODBC接口的话)。这样做虽然效率很高,但是封装却不够。如果有一个统一的数据库接口,那么我们就可以不对程序做任何修改而适用于多种数据库,从而使程序的移植性和跨平台能力都大大提高。

在PHP中要完成OOP,需要进行对象封装,也就是编写类。我们可以通过生成一个新的SQL类实现对数据库的简单封装。例如:

< ?
class SQL
{
var $Driver; //实际操作的数据库驱动子类
var $connection; //共用的数据库连接变量
function DriverRegister($d)
{
if($d!=””)
{
$include_path = ini_get(“include_path”);  字串8
$DriverFile = $include_path.”/”.$d.”.php”;
//驱动的存放路径必须在PHP.ini文件中设定的INCLUDE_PATH下
if( file_exists( $DriverFile)) //查找驱动是否存在
{
include($DriverFile);
$this->Driver = new $d();
// 根据驱动名称生成相应的数据库驱动类
return true;
}
}
return false; //注册驱动失败
}
function Connect($host,$user,$passwd,$database)//连接数据库的函数
{
$this->Driver->host=$host;
$this->Driver->user=$user;
$this->Driver->passwd=$pas
swd;
$this->Driver->database=$d
atabase;
$this->connection = $this->Driver->Connect();
}
function Close()//关闭数据库函数
{
$this->Driver->close($this->connection);
}
function Query($queryStr)//数据库字符串查询函数
{  字串5
return $this->Driver->query($queryStr,$this->connection);
}
function getRows($res)//查找行
{
return $this->Driver->getRows($res);
}
function getRowsNum($res)//取得行号
{
return $this->Driver-> getRowsNum ($res);
}
}
? >

我们以操作MySQL数据库为例。我们写一个数据库驱动类MySQL,在该类中,我们把有关MySQL数据库操作的函数都做进一步的封装。把包含该类,文件名为MySQL.php的文件放在PHP的系统 include_path下,就可以正常地使用了。注意编写数据库驱动文件时,文件名应和类名保持一致。

< ?
Class MySQL
{
var $host;
var $user;
var $passwd;
var $database;
function MySQL() //利用构造函数实现变量初始化
{
$host = “”;
$user = “”;
$passwd = “”;
$database = “”;

字串8
}
function Connect()
{
$conn = MySQL_connect($this->host, $this->user,$this->passwd) or
die(“Could not connect to $this->host”);
MySQL_select_db($this->database,$conn) or
die(“Could not switch to database $this->database;”);
return $conn;
}
function Close($conn)
{
MySQL_close($conn);
}

function Query($queryStr, $conn)
{
$res =MySQL_query($queryStr, $conn) or
die(“Could not query database”);
return $res;
}
function getRows($res)
{
$rowno = 0;
$rowno = MySQL_num_rows($res);
if($rowno>0)
{
for($row=0;$row<$rowno;$row++)
{
$rows[$row]=MySQL_fetch_row($res);
}
return $rows;  字串9
}
}
function getRowsNum($res)
{
$rowno = 0;
$rowno = mysql_num_rows($res);
return $rowno;
}
}
? >
同样我们要封装其他的“数据库驱动”到我们的SQL类中,只需要建立相应的类,并以同名命名驱动文件,放到PHP的include目录就可以了。

完成封装以后,就可以在PHP中按照OOP的思想来实现对数据库的编程了。

< ?
Include(“SQL.php”);
$sql = new SQL; //生成新的Sql对象
if($sql-> DriverRegister(“MySQL”)) //注册数据库驱动
{
$sql->Connect(“localhost”,”root”,””,”test”);
$res=$sql->query(“select * from test”); //返回查询记录集
$rowsnum = $sql->getRowsNum($res);
if($rowsnum > 0)
{
$rows = $sql->getRows($res);
foreach($rows as $row) //循环取出记录集内容
{  字串4
foreach($row as $field){
print $field;}
}
}
$sql->Close();
}
? >

在实际应用中,我们还可以根据实际需求对各种对象类做进一步扩展。在PHP中,还提供了一系列复杂的OOP方法,例如继承,重载,引用,串行化等等。充分调动各种方法并灵活运用,就能够使你的网站更合理和结构化,开发和维护也更容易。