进入 PHPChina 首页
当前位置:PHPChina 开源社区门户 - PHP编程技巧 - 正文
[字号:  ]

Pear::HTTP_Upload简介

发布时间:2008-4-16 10:43   作者: volcano   信息来源: ooso.net  [我来说两句(1700条)]

Pear的HTTP_Upload类库提供了一个封装好的html表单文件上传处理程序,使用Pear的error系统。

特点

  • 能一次处理多个文件的上传
  • 容易校验文件的上传状态,限制不期望的文件上传
  • 多语种的报错提示信息(还没有中文,不过可以扩展)

单个文件上传的例子

index.htm

CODE:
  1. <form action="./files.php"enctype="multipart/form-data">
  2. File1: <input type="file"name="userfile"><br>
  3. <input type="submit"name="submit"value="Upload!">
  4. </form>

files.php

PHP:
  1. <?php
  2. require'HTTP/Upload.php';
  3. $upload=newHTTP_Upload('es');
  4. // Language for error messages
  5. $file=$upload->getFiles('userfile');
  6. // return a file object or error
  7. if(PEAR::isError($file)){
  8.    die($file->getMessage());
  9. }
  10. // Check if the file is a valid upload
  11. if($file->isValid()){   // this method will return the name of the file you moved,   
  12.    // useful for example to save the name in a database   
  13.    $file_name=$file->moveTo('./uploads_dir/');
  14.    if(PEAR::isError($file_name)){ 
  15.        die($file_name->getMessage());
  16.    }
  17. }
  18. ?>

多文件上传的例子

CODE:
  1. <form action="files.php"enctype="multipart/form-data">
  2. Image1: <input type="file"name="userfile[]">
  3. <br>Image2: <input type="file"name="userfile[]">
  4. <br>Image3: <input type="file"name="userfile[]">
  5. <br><input type="submit"name="sub"value="Upload!"></form>
PHP:
  1. <?php
  2. $files=$upload->getFiles();// returns an array of file objects or error
  3. foreach($filesas$file){
  4.    if($file->isValid()){ 
  5.       ...   
  6.    }
  7. }?>

下载

http://pear.php.net/package/HTTP_Upload

版权声明:可以任意转载,转载时请务必以超链接形式标明文章原始出处和作者信息及本声明

作者:volcano发表于8月 30, 2006 at 9:58 am

版权信息:可以任意转载, 转载时请务必以超链接形式标明文章原始出处作者信息及此声明

字号:   | 推荐给好友

[我来说两句(1700条)]
现在有0人对本文发表评论 查看全部评论>>评论区

 

评分:0

验证码: seccode