Just wrote a file upload class. Welcome Commentary
<title>Uploading a file form</title>
Define (Upload_dir, ' d:\\appserv\\www\\test\\upload\\ ');
Define (Upload_web_dir, ' \\test\\upload\\ ');
Class upload_file_class{
var $input _name;//Upload the name of file
var $max _size;//max upload size unit kb
var $allow the allowed extensions for _ext;//
var $err _info; Error message
/**
* Constructor function
*
* @param $input _name
* @param $max _size
* @param $allow _ext
* @return
*/
function __construct ($input _name, $max _size=1024, $allow _ext=array (' jpg ', ' png ', ' gif ')) {
$this->input_name = $input _name;
$this->max_size = $max _size;
$this->allow_ext = $allow _ext;
$this->err_info_arr = Array (
1=> ' upload file size exceeds ini set value ',
2=> ' upload file size exceeds the maximum value set by the form ',
3=> ' files are only partially uploaded ',
4=> ' No files were uploaded ',
5=> ' upload file size exceeds the allowed set value ',
6=> ' is not an allowed upload file type ',
7=> ' failed to create directory, please verify that you have permission to create directory ',
8=> ' failed to create the file, please try again. ',
9=> ' File Upload error occurred, please try again. '
);
}
/**
* File Upload
*
* @return $file _path_arr Upload a successful file path array
*/
Public function upload () {
Global $_files;
if (!is_array ($this->input_name)) $this->input_name = Array ($this->input_name);
$file _path_arr = Array ();
foreach ($this->input_name as $k = = $input _name) {
$__f = $_files[$input _name];
if (!empty ($__f)) {
if ($__f[' error '] > 0) {
Client uploads generate errors
$this->set_err ($__f[' error ');
}
else{
$__f[' size ' = $__f[' size ']/1024;
if ($this->max_size >= $__f[' size ') {
if ($this->check_ext ($__f[' name ')) {
$save _dir_info = $this->check_dir ();
if ($save _dir_info) {
$rand _num = Date (' Ymdhis '). Rand (0,10000000);
$ext = $this->get_img_ext ($__f[' name ');
$new _file_name = $rand _num. ". {$ext} ";
$full _path = $save _dir_info[' Full_dir '). $new _file_name;
$full _web_path = $save _dir_info[' Full_web_dir '). $new _file_name;
if (!file_exists ($full _path)) {
if (Move_uploaded_file ($__f[' tmp_name '), $full _path)) {