/var/www/(Del)gepgroup.hk/webadmin/function_file_upload.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
function file_upload_closure_source(&$message$attribute$closure_source$destination_dir$file_name_func=NULL)
{
    
$destination NULL;
    
    if (
$file_name_func === NULL)
    {
        
$file_name_func = function($file_info)
        {
            return 
$file_info['filename'];
        };
    }
    
    
$tmp_file_info $_FILES[$attribute];
    
    if (
is_uploaded_file($closure_source($tmp_file_info['tmp_name'])))
    {
        
$destination false;
        
        
$is_valid true;
        if (!
file_exists($destination_dir))
        {
            
$is_valid mkdir($destination_dir0775true);
            if (!
$is_valid)
                
$message "Create directory fail.";
        }
        
        if (
$is_valid)
        {
            
//$file_name = $_FILES[$attribute]['name'];
            
            
$file_info pathinfo($closure_source($tmp_file_info['name']));
            
            
//$dirname = $file_info['dirname'];
            //$basename = $file_info['basename'];
            //$extension = $file_info['extension'];
            //$filename = $file_info['filename'];
            
            
$file_name $file_name_func($file_info);
            
$file_destination "$destination_dir/$file_name";
            
            
// If no Permission, that means temp file need linux root account, then use following code
            //if (!copy($_FILES[$attribute]['tmp_name'], $file_destination))
            //    $message = 'Could not copy file: ' . $file_name;
            
            
if (move_uploaded_file($closure_source($tmp_file_info['tmp_name']), $file_destination))
                
$destination $file_destination;
            else
            {
                
$upload_errors = array(
                    
UPLOAD_ERR_OK => "No errors.",
                    
UPLOAD_ERR_INI_SIZE => "Larger than upload_max_filesize.",
                    
UPLOAD_ERR_FORM_SIZE => "Larger than form MAX_FILE_SIZE.",
                    
UPLOAD_ERR_PARTIAL => "Partial upload.",
                    
UPLOAD_ERR_NO_FILE => "No file.",
                    
UPLOAD_ERR_NO_TMP_DIR => "No temporary directory.",
                    
UPLOAD_ERR_CANT_WRITE => "Can't write to disk.",
                    
UPLOAD_ERR_EXTENSION => "File upload stopped by extension.",
                    
UPLOAD_ERR_EMPTY => "File is empty." // add this to avoid an offset
                
); 
                
$message $upload_errors[$closure_source($tmp_file_info['error'])];
            }
        }
    }
    
    return 
$destination;
}

function 
file_upload(&$message$attribute$destination_dir$file_name_func=NULL)
{
    return 
file_upload_closure_source($message$attribute,
        function(
$tmp_file_info) {
            return 
$tmp_file_info;
        }, 
$destination_dir$file_name_func);
}

function 
image_upload_closure_source(&$message$attribute$closure_source$destination_dir$file_name_func=NULL$types=NULL)
{
    
$destination NULL;
    
$is_input_types_empty $types === NULL;
    
    if (
$is_input_types_empty)
    {
        
$types = array(
            
"image/bmp",
            
"image/BMP",
            
"image/gif",
            
"image/GIF",
            
"image/jpg",
            
"image/JPG",
            
"image/jpeg",
            
"image/JPEG",
            
"image/pjpeg",
            
"image/PJEG",
            
"image/png",
            
"image/x-png",
            
"image/PNG",
            
"image/X-PNG",
        );
    }
    
    
$tmp_file_info $_FILES[$attribute];
    if (
is_uploaded_file($closure_source($tmp_file_info['tmp_name'])))
    {
        
$destination false;
        if (
in_array($closure_source($tmp_file_info['type']), $types))
            
$destination file_upload_closure_source($message$attribute$closure_source$destination_dir$file_name_func$types);
        elseif (
$is_input_types_empty)
            
$message "Files must be BMP, JPEG, GIF, or PNG";
        else
            
$message "File type not supported";
    }
    
    return 
$destination;
}

function 
image_upload(&$message$attribute$destination_dir$file_name_func=NULL$types=NULL)
{
    return 
image_upload_closure_source($message$attribute,
        function(
$tmp_file_info) {
            return 
$tmp_file_info;
        }, 
$destination_dir$file_name_func$types);
}
?>