Pear's HTTP_Upload class library provides an encapsulated html form file upload handler that uses Pear's error system.
Features: Can handle the upload of multiple files at one time. Easily check the upload status of files. Limit unwanted file uploads. Multilingual error message (not in Chinese yet, but can be expanded).
Single file upload example
index.htm
PLAIN TEXT
CODE:
<form action="./files.php"enctype="multipart/form-data">
File1: <input type="file"name="userfile"><br>
<input type="submit"name="submit"value="Upload!">
</form>
files.php
PLAIN TEXT
PHP:
<?php
require'HTTP/Upload.php';
$upload=newHTTP_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());
}
}
?>
Example of multiple file
uploadPLAIN TEXT
CODE:
<form action="files.php"enctype="multipart/form-data">
Image1: <input type="file"name="userfile[]">
<br>Image2: <input type="file"name="userfile[]">
<br>Image3: <input type="file"name="userfile[]">
<br><input type="submit"name="sub"value="Upload!"></form>
PLAIN TEXT
PHP:
<?php
$files=$upload->getFiles();// returns an array of file objects or error
foreach($filesas$file){
if($file->isValid()){
...
}
}?>
download
http://pear.php.net/package/HTTP_Upload
Copyright statement: You can reprint it at will. When reprinting, please be sure to indicate the original source and author information of the article and this statement in the form of a hyperlink.
Author: volcano Published on August 30, 2006 at 9 :58 am
Copyright information: You can reprint at will. When reprinting, please be sure to indicate the original source and author information of the article andthe permanent link
of this statement in the form of a hyperlink
- http://www.ooso.net/index.php/archives/240