首页 > phper

php简单文件上传代码

<html>
<head>
<title>filemanage</title>
</head>
<body>
<center>
<p><h2>文件管理</h2></p><br>
<table border=1><tr size=18>
<td>文件ID</td><td>文件名称</td><td>文件大小</td><td>上传时间</td><td>下载</td><td>删除</td></tr>
<?php
include("conn.php");
//echo '<table border=1>';
$rs=$db->query("select * from filem order by f_id DESC");
$i=1;
while($row = $rs->fetch_assoc())      
{
 $size=$row['f_size']/1024;
echo "<tr><td>".$i++."</td><td>".$row['f_name']."</td><td>".number_format($size, 2, '.', '')."KB</td><td>".$row['f_date']."</td><td><a href=".$row['f_url']." target=_blank>下载</a></td><td><a href=del.php?id=".$row['f_id'].">删除</a></td></tr>"; 
}
echo '</table>';
unset($rs);
$db->close();
?>
</center>
<br><br><hr><br>
<b><h2>uploadfile</h2></b>
<br>
<form enctype="multipart/form-data" action="" method="post">
选择上传文件:<br><input name="userfile" type="file"><br>
<input type="submit" value="发送">
</form>
<?php
 if(!$_FILES["userfile"]["name"])  exit;
//echo $_FILES['userfile']['type'];
if ($_FILES['userfile']['error'] > 0)
  {
    echo 'Problem: ';
    switch ($_FILES['userfile']['error'])
    {
      case 1:  echo 'File exceeded upload_max_filesize';  break;
      case 2:  echo '不能超过800M';  break;
      case 3:  echo 'File only partially uploaded';  break;
      case 4:  echo 'No file uploaded';  break;
    }
    exit;
  }
else
{ //检查上传文件是否在允许上传的类型
   $tp = array("gif","jpeg","png","txt","doc","rar","zip","xls","bmp","wmv","mp3","flv","rmvb","avi");
if (!in_array(strtolower(substr(strrchr($_FILES['userfile']['name'], '.'),1)), $tp))

阅读全文

apache rewrite 讲解

Apache Rewrite实例2007-03-04 11:25<VirtualHost *:80>    ServerAdmin host@discuz.com    DocumentRoot D:/www    ServerName www.xiaojia.com    ServerAlias xiaojia.com xiaojia.net    ErrorLog D:/www/logs/xiaojia.com-error_log    CustomLo...
阅读全文

php分页函数 [经典]


<?php
/*
  分页类
  design by xqbar
  qq:174171262
  email:wxddong@163.com
  blog:http://www.xqbar.com
  考虑输入参数
  size(每页记录数目)
  url(http://pic1.phprm.com/2015/07/04/拥刂?jpg)
  style(分页样式)
  sql(运行语句)
*/
class page{
   public  $sql='';//分页sql语句
   public  $url='';//分页传递过来的下页地址,可以另外加参数用来搜索使用
   public  $size=10;//默认每页显示数据10条
   public  $style=1;//默认分页显示风格
   private $totalsize=0;//数据总数
   private $totalpage=0;//总页数
   private $page=1;//当前页
   private $link;//数据库连接点
   private $flag=false;//初始化标志点
   private $offset=0;//分页偏移值 limit $offset,$size;
   //构造函数 传递数据库连接点
   function __construct($link){
      $this->page=(isset($_GET['page'])&&!empty($_GET['page'])&&(int)($_GET['page']))?(int)$_GET['page']:1;//获取当前page参数值,确定当前是第几分页
      $this->link=$link;
   }
   //析构函数
   function __destruct(){
  @mysql_close($this->link);
  $this->link=NULL;
   }
  //构造函数兼顾<php5
   function page($link){
     $this->__construct($link);
   }
   //初始化
   function init(){
     if(empty($this->sql)){$this->halt("错误警告:<font color='red'>sql语句不能为空!</font>");exit();}//判断sql是否存在
  $this->sql=str_replace(sql);//">'@_',DB_PREFIX,$this->sql);//替换sql中的表前缀
     $this->size=($this->size<=0)?10:$this->size;//初始每页显示数据条数
  $this->page=($this->page<1)?1:$this->page;//当前页码值
     #大于第一页后优化查询总记录数
  //这里我使用了第一次浏览分页时查询数据库得出数据库总数,点下页时传递并不再查询数据库,数据量多时可能会节省些(自己想的)
  if($this->page==1&&!isset($_GET['tsize'])){
     sql">$result=@mysql_query($this->sql);
     $this->totalsize=@mysql_num_rows($result);
     @mysql_free_result($result);
  }else{
       //上面的问题唯一缺点就是用户手动在地址栏更改了tsize值后分页将不再正确
     $this->totalsize=(int)$_GET['tsize'];
    }
     $this->url=(empty($this->url))?$_SERVER['PHP_SELF'].'?tsize='.$this->totalsize:$this->url.'&tsize='.$this->totalsize;//传递totalsize给下一页
  $this->totalpage=ceil($this->totalsize/$this->size);//获取总页数
  $this->page=($this->page>$this->totalpage)?$this->totalpage:$this->page;
  $this->offset=($this->page-1)*$this->size;//获取偏移值
   }
  //查询数据库返回结果给二维数组
   function fetch_array($result_type=MYSQL_BOTH){
     if(!$this->flag){$this->init();$this->flag=true;}
     if($this->totalsize<=0){return NULL;}
  $this->sql.=" limit $this->offset,$this->size";
  sql,$this->link">$result=@mysql_query($this->sql,$this->link);
  while($row=mysql_fetch_array($result,$result_type)){$rows[]=$row;}
  @mysql_free_result($result);
  return $rows;
   }
  //获取总数据数
   function getsize(){
     if(!$this->flag){$this-init();$this->flag=true;}//判断是否已经在此之前初始化过,是则直接返回 (下同)
     return $this->totalsize;
   }
//获取总页数
   function getpage(){
     if(!$this->flag){$this-init();$this->flag=true;}
     return $this->totalpage;
   }
//根据传入参数输出分页链接
   function outpage(){
     if(!$this->flag){$this-init();$this->flag=true;}
  switch($this->style){
    #1 第x页 共x页 首页 上一页 下一页 尾页 x条/页 共x页
    case 1:
           $pagehtml="<span class='page'>第<i>{$this->page}</i>页 共<i>{$this->totalpage}</i>页";
     $pagehtml.=($this->page==1)?" 首页 上一页":" <a href='{$this->url}&page=1'>首页</a> <a href='{$this->url}&page=".($this->page-1)."'>上一页</a>";
           $pagehtml.=($this->page==$this->totalpage)?" 下一页 尾页":" <a href='{$this->url}&page=".($this->page+1)."'>下一页</a> <a href='{$this->url}&page={$this->totalpage}'>尾页</a>";
     $pagehtml.=" 共<i>{$this->totalsize}</i>条</span>";
           break;
    #1 首页 上一页 下一页 尾页 x条/页 共x页
    case 2:
           $pagehtml="<span class='page'>";
     $pagehtml.=($this->page==1)?" 首页 上一页":" <a href='{$this->url}&page=1'>首页</a> <a href='{$this->url}&page=".($this->page-1)."'>上一页</a>";
           $pagehtml.=($this->page==$this->totalpage)?" 下一页 尾页":" <a href='{$this->url}&page=".($this->page+1)."'>下一页</a> <a href='{$this->url}&page={$this->totalpage}'>尾页</a>";
     $pagehtml.=" 共<i>{$this->totalsize}</i>条</span>";
           break;
    #2 1 2 3 4 5
    case 3:
           $offset=(ceil($this->page/10)-1)*10;
     $pagehtml="<span class='page'>";
     if($offset!=0){$pagehtml.="<a href='{$this->url}&page=1'>|<<</a> <a href='{$this->url}&page=".($this->page-1)."'><<</a>";}
     for($i=1;$i<=10;$i++){
       $n=$i+$offset;
       if($n>$this->totalpage){break;}
       if($n==$this->page){$pagehtml.=" <a href='{$this->url}&page=$n'><i>$n</i></a> ";}
       else{$pagehtml.=" <a href='{$this->url}&page=$n'><b>$n</b></a> ";}
     }
     if($n<$this->totalpage){$pagehtml.=" <a href='{$this->url}&page=".($this->page+1)."'>>></a> <a href='{$this->url}&page=$this->totalsize'>>>|</a>";}
     unset($offset,$i,$n);
           break;   
    #1
    default:
           $pagehtml="第{$this->page}页 共{$this->totalpage}页";
     $pagehtml.=($this->page==1)?" 首页 上一页":" <a href='{$this->url}&page=1'>首页</a> <a href='{$this->url}&page=".($this->page-1)."'>上一页</a>";
           $pagehtml.=($this->page==$this->totalpage)?" 下一页 尾页":"  <a href='{$this->url}&page=".($this->page+1)."'>下一页</a> <a href='{$this->url}&page={$this->totalpage}'>尾页</a>";
           break;   
  }
    return $pagehtml;
   }
//错误输出
   function halt($msg){
  echo "<html>";
  echo "<head>";
  echo "<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>rn";
  echo "<title>Error</title>";
  echo "</head>";
  echo "<body><p style='line-helght:150%;font-size:10pt'>";
  echo $msg;
  echo "</p></body>";
  echo "</html>";
}
}
?>

阅读全文

结合使用PHP和RSS

内容摘要:RSS 聚合最近非常流行,因此至少对 RSS 及其工作方式有所了解是一名 PHP 开发人员的迫切需要。本文介绍了 RSS 基础知识、RSS 众多用途中的一些用途、如何使用 PHP 从数据库创建 RSS 提要,以及如何使用 XML_RSS 模块读取现有 RSS 提要并将其转换为 HTML。

阅读全文

搭建 PHP 环境


所需软件:
Apache2.2.8    PHP 5.2.5    Mysql 5.0

************************

1. 安装 Apache
(安装包格式)
在 Network Domain 和 Server Name 里填入 127.0.0.1,右下角可以控制 Apache 服务器的启动与关闭。安装好后,在浏览器里输入 http://localhost/,如果出现 It works!" 字样,则说明安装成功了!

2. 安装 PHP
(解压包格式)
解压文件到 C:PHP 下

3. 安装 Mysql
(解压包格式)
解压文件到 C:mysql 下,从命令提示符里进入到 bin 目录下,执行 "mysql-nt -install" 进行安装,安装成功后,会显示 "Service successfully installed." 然后启动服务 "net start mysql",最后更改管理密码 "mysqladmin -u root -p password <new password>",会提示输入旧密码,由于原来没有设置过密码,所以直接回车。
其它相关命令:
mysqld-nt -remove    卸载服务
net stop mysql    停止服务

4. 软件配置
首先停止 Apache 服务,然后用记事本打开 C:pacheconfhttpd.conf 文件,找到 "#LoadModule ssl_module modules/mod_ssl.so",在它下面添加 "LoadModule php5_module c:/php/php5apache2_2.dll"。然后找到 "AddType application/x-gzip .gz .tgz",在下面添加 "AddType application/x-httpd-php .php"。

在 C:php 目录下找到 php.ini-dist 和 php5ts.dll 文件,把 php.ini-dist 复制到 Windows 目录下,并且重命名为 php.ini,用记事本打开找到 "extension=php_mysql.dll",去掉前面的分号。把 php5ts.dll 复制到 system32 目录里,并且把 C:phpext 目录下的 php_mysql.dll 文件复制到 Windows 文件夹下。

5. 配置网站
再次修改 httpd.conf,找到 "DocumentRoot "C:/Apache/htdocs" "、"<Directory "C:/Apache/htdocs">"、"DirectoryIndex Index.hrml",然后做相应修改即可!

6. 搭建完毕
启动 Apache 服务,配置完成!

阅读全文

WSF/PHP调用带有WS-Security支持的Web Service时的注意事项

目前在PHP中调用带有WS-Security支持的Web Service解决方案还是比较少的,WSF/PHP是一个不错的选择,官方首页为http://wso2.org/projects/wsf/php,下面就介绍下在运用WSF/PHP的时候需要注意的一些地方;
1、WSF/PHP【WSO2 Web Services Framework for PHP】 是WSO2.ORG提供的专门针对PHP调用Web Service的一个优秀框架,使用非常简单;但是WSO2不仅仅提供针对PHP的ws框架,而且还对其他很多中语言提供开发框架,比如基于Spring、Perl、Ruby……,除此之外还提供很多其他框架和培训服务,具体可查询wso2.org和wso2.com;
2、WSF/PHP模块的安装需要很多其他的支持包,在windows下面体现为一些dll文件,主要包括Libxml2、iconv、Openssl、zlib,而不仅仅是Libxml2【GNOME XML Library】;安装过程中需要将这些依赖dll全部都集中到wsf_c/lib/目录中;这些依赖包可以到http://www.zlatkovic.com/pub/libxml/下载,下载完成之后对各压缩包解压,dll文件一般位于/bin/目录中;
3、WSF所依赖的这些dll文件一定需要copy到/windows/system32/中才行,否则加载模块不成功;这点和官方文档中说的直接将wsf_c/lib/加入到path中即可是不一致的;当然这点可能有其他方式可以解决,因为我不精通PHP,但是和一些同事沟通过,但没结果;
4、验证是否正常加载了WSF模块,只用php -m命令查看列表是不够的,即时WSF模块没有加载成功,但是php.ini中设置了WSF模块,那php -m的列表中也会包括WSF;最保险的做法是查看服务器日志比如Apache;
5、在申明WSSecurityToken实例的时候,参数receiverCertificate是用来加密Soap消息的,而不是用来验证服务端返回的签名信息的,所以若只是需要签名,那该参数无需设置;WSF/PHP目前还没有提供验证服务端返回的签名信息功能的;
6、WSF/PHP调用WS时默认使用SOAP 1.2协议,这可能会对某些服务端ws调用时出错,若需要设置使用SOAP版本,则可以在申请WSClient实例时设置参数"useSOAP" => "1.1"实现;

阅读全文

Windows 下的 PHP 扩展编程

PHP 尽管提供了大量有用的函数,但是在特殊情况下还可能需要进行扩展编程,比如大量的 PECL(PHP Extension Community Library)就是以扩展的形式提供的(动态链接库dll文件),它们比 PEAR 的运行效率要高很多。
    PHP 扩展是用 C 或 C++ 编写的,需要编译成动态连接库 dll 文件后在 PHP 环境下注册后才能使用。
    编写 PHP 扩展的软件要求:
      VC++6.0 或 VC++.NET 环境。
      PHP 的源代码,需要编译。
    如果不愿意编译 PHP 的源代码,可以再下载 PHP 的已经编译成功的二进制代码(就是我们部署 PHP 运行环境的那些文件包)。注意分别下载的源文件包和已编译包,它们的版本必须一致。

阅读全文