|
<? Php
Class files {
/**
* Upload
*
* File Upload
*
* @ Param String $ path e.g. Zend_Registry: get ('upload ')
* @ Param Array $ files e.g. $ _ FILES ['filedata']
* @ Param String $ dir e.g. $ _ POST ['dir']
*
* Return Array $ msg e.g. if ($ msg ['error'])
*/
Static function upload ($ path, $ files, $ dir)
{
$ Msg = array ();
// Path of the file storage directory
$ Save_path = $ path;
// File Save directory URL
$ Save_url = $ path;
// Define the file extension that can be uploaded
$ Ext_arr = array (
'Image' => array ('gif', 'jpg ', 'jpeg', 'png ', 'bmp '),
'Flash' => array ('swf ', 'flv '),
'Media '=> array ('swf', 'flv', 'mp3', 'wav ', 'wm
A', 'wmv ', 'mid', 'av', 'mpg', 'asf', 'rm ', 'rmvb '),
'File' => array ('Doc', 'docx', 'xls ', 'xlsx', 'pp
T', 'htm', 'html', 'txt ', 'zip', 'rar ', 'gz', 'bz2 '),
);
// Maximum File Size
$ Max_size = 1000000;
$ Save_path = realpath ($ save_path ).'/';
// When a file is uploaded
If (empty ($ _ FILES) === false ){
// Original file name
$ File_name = $ files ['name'];
// Temporary file name on the server
$ Tmp_name = $ files ['tmp _ name'];
// File Size
$ File_size = $ files ['SIZE'];
// Directory name
$ Dir_name = empty ($ dir )? 'Image': trim ($ dir );
// Check the file name
If (! $ File_name ){
$ Msg ['error'] = "select a file. ";
}
// Check the Directory
Else if (@ is_dir ($ save_path) === false ){
$ Msg ['error'] = "the upload directory does not exist. Contact the Administrator ";
}
// Check the directory write permission
Else if (@ is_writable ($ save_path) === false ){
$ Msg ['error'] = "the upload directory has no write permission. Contact the Administrator ";
}
// Check whether the object has been uploaded
Else if (@ is_uploaded_file ($ tmp_name) === false ){
$ Msg ['error'] = "temporary files may not be uploaded. Please upload again ";
}
// Check the file size
Else if ($ file_size> $ max_size ){
$ Msg ['error'] = "the size of the uploaded file exceeds the limit. ";
}
// Check the directory name
Else if (empty ($ ext_arr [$ dir_name]) {
$ Msg ['error'] = "the directory name is incorrect. ";
}
Else
{
// Obtain the file extension
$ Temp_arr = explode (".", $ file_name );
$ File_ext = array_pop ($ temp_arr );
$ File_ext = trim ($ file_ext );
$ File_ext = strtolower ($ file_ext );
// Check the extension
If (in_array ($ file_ext, $ ext_arr [$ dir_name]) === false ){
$ Msg ['error'] = "the File Upload extension is not allowed.
N only supports the ". implode (", ", $ ext_arr [$ dir_name])." format. ";
}
Else
{
// Create a folder
$ Dbsave = ""; // path stored in the database
If ($ dir_name! = ''){
$ Save_path. = $ dir_name ."/";
$ Save_url. = $ dir_name ."/";
$ Dbsave = $ dir_name .'/';
If (! File_exists ($ save_path )){
Mkdir ($ save_path );
}
}
$ Y = date ("Y ");
$ M = date ("m ");
$ D = date ("d ");
$ Save_path. = $ y ."/";
$ Save_url. = $ y ."/";
$ Dbsave. = $ y .'/';
If (! File_exists ($ save_path )){
Mkdir ($ save_path );
}
$ Save_path. = $ m ."/";
$ Save_url. = $ m ."/";
$ Dbsave. = $ m .'/';
If (! File_exists ($ save_path )){
Mkdir ($ save_path );
}
$ Save_path. = $ d ."/";
$ Save_url. = $ d ."/";
$ Dbsave. = $ d .'/';
If (! File_exists ($ save_path )){
Mkdir ($ save_path );
}
// New file name
$ New_file_name = date ("YmdHis"). '_'. rand (1000
0, 99999). '.'. $ file_ext;
// Move the file
$ File_path = $ save_path. $ new_file_name;
If (move_uploaded_file ($ tmp_name, $ file_path) === false ){
$ Msg ['error'] = "An error occurred while uploading the file. ";
}
// The final storage file of the database
$ Dbsave. = $ new_file_name;
@ Chmod ($ file_path, 0644 );
$ File_url = $ save_url. $ new_file_name;
$ Msg ['file _ url'] = $ file_url;
$ Msg ['file _ size'] = $ file_size;
$ Msg ['db _ path'] = $ dbsave;
} // Check the extension
} // Directory correctness
Return $ msg;
}
}
// File Upload
}
?> |