Smarty error: unable to read resource,Smarty的错误 : 无法读取资源

一般出现这种错误是smarty配置的问题,路径没有配置好!如下面这段代码是将会出现这样的错误:

Warning: Smarty error: unable to read resource: “templates\smtl.html” inF:\wamp\www\shangage\smarty\Smarty.class.phpon line1095,无法读取资源,即无法读取smtl.html模版文件,

<?php
 define(‘base_path’,$_SERVER['DOCUMENT_ROOT']);//定义服务器的绝对路径
 define(‘smarty_path’,'\shangage\smarty\\’);//定义smarty目录的绝对路径
 require base_path.smarty_path.’Smarty.class.php’;//加载Smarty类库文件
 $smarty = new Smarty;//实例化一个smarty对象
 //定义个目录的路径
 $smarty->template_dir = base_path.smarty_path.’tmeplates\\’;
 $smarty->compile_dir = base_path.smarty_path.’templates_c\\’;
 $smarty->config_dir = base_path.smarty_path.’config\\’;
 $smarty->cache_dir = base_path.smarty_path.’cache\\’;
 
 $arr = array(1=>’杨’,2=>’君’,3=>’华’,4=>’杨君华’);
 $smarty->assign(‘title’,'smarty的第一次使用’);
 $smarty->assign(‘arr’,$arr);
 $smarty->display(‘templates\smtl.html’)
?>

把templates\smtl.html改写成..\templates\smtl.html,就可以调用模版文件。输出结果为:

 还可以把代码写成如下:

<?php
 define(‘base_path’,$_SERVER['DOCUMENT_ROOT']);//定义服务器的绝对路径
 define(‘smarty_path’,‘/shangage/smarty/’);//定义smarty目录的绝对路径
 require base_path.smarty_path.’Smarty.class.php’;//加载Smarty类库文件
 $smarty = new Smarty;//实例化一个smarty对象
 //定义个目录的路径
 $smarty->template_dir = base_path.smarty_path.‘tmeplates/’;
 $smarty->compile_dir = base_path.smarty_path.‘templates_c/’;
 $smarty->config_dir = base_path.smarty_path.‘config/’;
 $smarty->cache_dir = base_path.smarty_path.‘cache/’;
  
 $arr = array(1=>’杨’,2=>’君’,3=>’华’,4=>’杨君华’);
 $smarty->assign(‘title’,'smarty的第一次使用’);
 $smarty->assign(‘arr’,$arr);
 $smarty->display(‘../templates/smtl.html’)
?>

注意:正斜杠 /     与反斜杠  \   是一样的,但是要注意:反斜杠 \  在PHP中为转义字符,没有意义。如要输出单引号 ‘   则应该写成 \ ‘,因此 \\ ‘  实际上就是  / ‘ 。注意,模版文件要与调用模版文件的字符集编码一样,否则也会出现上面的错误!

 

PHP获取IP地址实现不同地区城市跳转的功能

日志

PHP获取IP地址

  这个比较简单了,利用PHP自带函数就可以了,PHP中文手册看一下,都有现成的例子,就不过多说明了,直接上代码,A段:
  1. <?
  2. //PHP获取当前用户IP地址方法
  3. $xp_UserIp = ($_SERVER["HTTP_VIA"]) ? $_SERVER["HTTP_X_FORWARDED_FOR"] : $_SERVER["REMOTE_ADDR"];
  4. $xp_UserIp = ($xp_UserIp) ? $xp_UserIp : $_SERVER["REMOTE_ADDR"];
  5. ?>

php根据ip跳转到不同的地区城市

PHP通过IP地址判断用户所在城市

   上文已经获得了用户IP地址,接下来,我们就是根据这个IP地址获得用户所在城市了。开始之前,我们需要下载一个现成的数据库QQ IP数据库。

php mysql 导出excel,支持大数据量

日志

 

在日常的应用开发中,导出数据,是非常普通的需求,几乎成了必备项。在导出的文件格式中,常见的有 excel, txt, pdf, csv等。通常情况下EXCEL的格式,就是利用 CSV 的形式来实现的。

对于PHP的应用,操作EXCEL,有一些开源的类库,例如:PHPExcel

本文介绍的不是使用第三方的类库,而是使用PHP自带的功能,那就是 fputcsv 这个函数,fputcsv是PHP5引入的函数。

如果你使用的是PHP4,那么请使用自定义函数

<?php
// 输出Excel文件头
header('Content-Type: application/vnd.ms-excel;charset=gbk');
header('Content-Disposition: attachment;filename="文件名.csv"');
header('Cache-Control: max-age=0');

// 从数据库中获取数据
$sql = 'select * from `table` where ……';
$stmt = @mysql->query($sql);

// PHP文件句柄,php://output 表示直接输出到浏览器
$fp = fopen('php://output', 'a');

// 输出Excel列头信息
$head = array('姓名', '性别', '年龄', 'Email', '电话', '……');
foreach ($head as $i => $v) {
    // CSV的Excel支持GBK编码,一定要转换,否则乱码
    $head[$i] = iconv('utf-8', 'gbk', $v);
}

// 写入列头
fputcsv($fp, $head);

// 计数器
$cnt = 0;
// 每隔$limit行,刷新一下输出buffer,节约资源
$limit = 100000;

// 逐行取出数据,节约内存
while ($row = $stmt->fetch(FETCH_NUM)) {

    $cnt ++;
    if ($limit == $cnt) { //刷新一下输出buffer,防止由于数据过多造成问题
        ob_flush();
        flush();
        $cnt = 0;
    }

    foreach ($row as $i => $v) {
        $row[$i] = iconv('utf-8', 'gbk', $v);
    }
    fputcsv($fp, $row);
}

dirname(__FILE__) 用法

日志

dirname(__FILE__) 就是取得当前文件所在的目录
通常在配置文件路径的时候用dirname(__FILE__)是非常有效的方法,但是因为__FILE__的路径是当前代码所在文件(而不是url所在文件)完整路径,所以定义配置文件通常要放在根目录下定义网站的根地址,但是下面的方法可以解决配置文件的存放问题。
dirname(dirname(__FILE__)); // 假设__FILE__为 /home/web/config/config.php ,输出为 /home/web
dirname(dirname(__FILE__)); 得到的是文件上一层目录名
dirname(__FILE__); 得到的是文件所在层目录名

php跳转的三种方法

日志

最近用header函数跳转在服务器上有限制。就去找了php的 代码,发现以下几种跳转的方法:

方法一:使用PHP自带函数
Header(“Location:网址“);
说明:必须在网页没有任何输出的时候执行,要特别要注意空格。

方法二:利用meta
echo “<meta. http-equiv=refresh content=’0; url=网址’>”;
说明:没有方法一的限制,但是如果前面有输出,则输出的内容会闪烁一下然后进入 跳转到的页面。

方法三:利用Javascript语言
echo “<script. language=’javascript’>”;
echo ” location=’网址’;”;
echo “</script>”;

Warning: session_start() [function.session-start]: Cannot send session…

日志

Warning: session_start() [function.session-start]: Cannot send session cache limiter – headers already sent (output started at D:\orther object\phptest\session.php:1) in D:\orther object\phptest\session.php on line 2

错误原因:大家如果用dreamwever的话;请在某个页面上右键==>页面属性==>标题/编码

就会看到一个“包括Unicode 签名(BOM)(S)”的复选框;把它取消选择;然后应用即可····

Listen.Im

日志

  1. 开发了差不多60%了..难题差不多全攻克了。
  2. mac打不开了,郁闷,可惜了我的代码。
  3. 今天解决了Listen.Im上传图片的同时生成缩略图,减少了页面的加载时间。

    Listen.Im

C# 在类库文件无法使用Server.MapPath

C# 在类库文件无法使用Server.MapPath

如果你从Page类继承的类中执行这条语句,才可以简单地使用
 

DataBase = Server.MapPath("data.mdb");

否则写全命名空间:

 

System.Web.HttpContext.Current.Server.MapPath();

注意:如果是在一个类库下的一个类中、要先添加引入using System.Web;因为新建一个类库时默认是不引入using System.Web的!