@@ -38,6 +38,8 @@ function __construct($options = null, $initialize = true) {
38
38
'script_url ' => $ this ->get_full_url ().'/ ' ,
39
39
'upload_dir ' => dirname ($ _SERVER ['SCRIPT_FILENAME ' ]).'/files/ ' ,
40
40
'upload_url ' => $ this ->get_full_url ().'/files/ ' ,
41
+ 'user_dirs ' => false ,
42
+ 'mkdir_mode ' => 0755 ,
41
43
'param_name ' => 'files ' ,
42
44
// Set the following option to 'POST', if your server does not support
43
45
// DELETE requests. This is a parameter sent to the client:
@@ -126,15 +128,29 @@ protected function get_full_url() {
126
128
substr ($ _SERVER ['SCRIPT_NAME ' ],0 , strrpos ($ _SERVER ['SCRIPT_NAME ' ], '/ ' ));
127
129
}
128
130
131
+ protected function get_user_id () {
132
+ @session_start ();
133
+ return session_id ();
134
+ }
135
+
136
+ protected function get_user_path () {
137
+ if ($ this ->options ['user_dirs ' ]) {
138
+ return $ this ->get_user_id ().'/ ' ;
139
+ }
140
+ return '' ;
141
+ }
142
+
129
143
protected function get_upload_path ($ file_name = null , $ version = null ) {
130
144
$ file_name = $ file_name ? $ file_name : '' ;
131
145
$ version_path = empty ($ version ) ? '' : $ version .'/ ' ;
132
- return $ this ->options ['upload_dir ' ].$ version_path .$ file_name ;
146
+ return $ this ->options ['upload_dir ' ].$ this ->get_user_path ()
147
+ .$ version_path .$ file_name ;
133
148
}
134
-
149
+
135
150
protected function get_upload_url ($ file_name , $ version = null ) {
136
151
$ version_path = empty ($ version ) ? '' : rawurlencode ($ version ).'/ ' ;
137
- return $ this ->options ['upload_url ' ].$ version_path .rawurlencode ($ file_name );
152
+ return $ this ->options ['upload_url ' ].$ this ->get_user_path ()
153
+ .$ version_path .rawurlencode ($ file_name );
138
154
}
139
155
140
156
protected function set_file_delete_properties ($ file ) {
@@ -163,12 +179,21 @@ protected function get_file_size($file_path, $clear_stat_cache = false) {
163
179
164
180
}
165
181
166
- protected function get_file_object ($ file_name ) {
182
+ protected function is_file_object ($ file_name ) {
167
183
$ file_path = $ this ->get_upload_path ($ file_name );
168
184
if (is_file ($ file_path ) && $ file_name [0 ] !== '. ' ) {
185
+ return true ;
186
+ }
187
+ return false ;
188
+ }
189
+
190
+ protected function get_file_object ($ file_name ) {
191
+ if ($ this ->is_file_object ($ file_name )) {
169
192
$ file = new stdClass ();
170
193
$ file ->name = $ file_name ;
171
- $ file ->size = $ this ->get_file_size ($ file_path );
194
+ $ file ->size = $ this ->get_file_size (
195
+ $ this ->get_upload_path ($ file_name )
196
+ );
172
197
$ file ->url = $ this ->get_upload_url ($ file ->name );
173
198
foreach ($ this ->options ['image_versions ' ] as $ version => $ options ) {
174
199
if (!empty ($ version )) {
@@ -186,19 +211,27 @@ protected function get_file_object($file_name) {
186
211
return null ;
187
212
}
188
213
189
- protected function get_file_objects () {
214
+ protected function get_file_objects ($ iteration_method = 'get_file_object ' ) {
215
+ $ upload_dir = $ this ->get_upload_path ();
216
+ if (!is_dir ($ upload_dir )) {
217
+ mkdir ($ upload_dir , $ this ->options ['mkdir_mode ' ]);
218
+ }
190
219
return array_values (array_filter (array_map (
191
- array ($ this , ' get_file_object ' ),
192
- scandir ($ this -> get_upload_path () )
220
+ array ($ this , $ iteration_method ),
221
+ scandir ($ upload_dir )
193
222
)));
194
223
}
195
224
225
+ protected function count_file_objects () {
226
+ return count ($ this ->get_file_objects ('is_file_object ' ));
227
+ }
228
+
196
229
protected function create_scaled_image ($ file_name , $ version , $ options ) {
197
230
$ file_path = $ this ->get_upload_path ($ file_name );
198
231
if (!empty ($ version )) {
199
232
$ version_dir = $ this ->get_upload_path (null , $ version );
200
233
if (!is_dir ($ version_dir )) {
201
- mkdir ($ version_dir , 0755 );
234
+ mkdir ($ version_dir , $ this -> options [ ' mkdir_mode ' ] );
202
235
}
203
236
$ new_file_path = $ version_dir .'/ ' .$ file_name ;
204
237
} else {
@@ -298,7 +331,7 @@ protected function validate($uploaded_file, $file, $error, $index) {
298
331
return false ;
299
332
}
300
333
if (is_int ($ this ->options ['max_number_of_files ' ]) && (
301
- count ( $ this ->get_file_objects () ) >= $ this ->options ['max_number_of_files ' ])
334
+ $ this ->count_file_objects ( ) >= $ this ->options ['max_number_of_files ' ])
302
335
) {
303
336
$ file ->error = $ this ->get_error_message ('max_number_of_files ' );
304
337
return false ;
@@ -405,6 +438,10 @@ protected function handle_file_upload($uploaded_file, $name, $size, $type, $erro
405
438
$ file ->type = $ type ;
406
439
if ($ this ->validate ($ uploaded_file , $ file , $ error , $ index )) {
407
440
$ this ->handle_form_data ($ file , $ index );
441
+ $ upload_dir = $ this ->get_upload_path ();
442
+ if (!is_dir ($ upload_dir )) {
443
+ mkdir ($ upload_dir , $ this ->options ['mkdir_mode ' ]);
444
+ }
408
445
$ file_path = $ this ->get_upload_path ($ file ->name );
409
446
$ append_file = $ content_range && is_file ($ file_path ) &&
410
447
$ file ->size > $ this ->get_file_size ($ file_path );
0 commit comments