Pear::HTTP_Upload简介

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

特点

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

单个文件上传的例子

index.htm

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

files.php

<?php
require 'HTTP/Upload.php';
$upload = new HTTP_Upload('es');
// Language for error messages
$file = $upload->getFiles('userfile');
// return a file object or error
if (PEAR::isError($file)) {
	die ($file->getMessage());
}
// Check if the file is a valid upload
if ($file->isValid()) {    // this method will return the name of the file you moved,
	// useful for example to save the name in a database
	$file_name = $file->moveTo('./uploads_dir/');
	if (PEAR::isError($file_name)) {
		die ($file_name->getMessage());
	}
}
?>

多文件上传的例子

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

下载

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

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

作者: 发表于August 30, 2006 at 9:58 am

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

Tags: ,

4 条评论 »

  1. 晓风寒墨 于 2006-09-08 @ 08:26:48 留言

    呵呵,亲爱的灿灿,好久没有看到你了,今天溜到你的网站来看看,发现有好多东西可以让我学啊,哈哈。简直就是一个php宝库。最近工作如何?什么时候回长沙休息的时候一定要记得联系我啦,电话13308427477。有空也可以去我的博客去留言(www.coooo.net)

  2. volcano 于 2006-09-10 @ 11:07:32 留言

    原来你用的是百度空间,哈哈

  3. Binlle 于 2006-09-11 @ 16:18:48 留言

    怎么还没有新贴,来了几次还是这张老脸.楼主度假了?

  4. volcano 于 2006-09-11 @ 23:07:58 留言

    这阵有点犯懒,对不住了。哈哈

RSS 为此帖反馈评论 · 反向跟踪 网站

留条评论