Skip to content

Commit fa4a455

Browse files
committed
Added user_dirs and mkdir_mode options.
Added get_user_id and get_user_path methods. Added is_file_object and count_file_objects methods.
1 parent 2b696e0 commit fa4a455

File tree

1 file changed

+47
-10
lines changed

1 file changed

+47
-10
lines changed

server/php/upload.class.php

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ function __construct($options = null, $initialize = true) {
3838
'script_url' => $this->get_full_url().'/',
3939
'upload_dir' => dirname($_SERVER['SCRIPT_FILENAME']).'/files/',
4040
'upload_url' => $this->get_full_url().'/files/',
41+
'user_dirs' => false,
42+
'mkdir_mode' => 0755,
4143
'param_name' => 'files',
4244
// Set the following option to 'POST', if your server does not support
4345
// DELETE requests. This is a parameter sent to the client:
@@ -126,15 +128,29 @@ protected function get_full_url() {
126128
substr($_SERVER['SCRIPT_NAME'],0, strrpos($_SERVER['SCRIPT_NAME'], '/'));
127129
}
128130

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+
129143
protected function get_upload_path($file_name = null, $version = null) {
130144
$file_name = $file_name ? $file_name : '';
131145
$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;
133148
}
134-
149+
135150
protected function get_upload_url($file_name, $version = null) {
136151
$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);
138154
}
139155

140156
protected function set_file_delete_properties($file) {
@@ -163,12 +179,21 @@ protected function get_file_size($file_path, $clear_stat_cache = false) {
163179

164180
}
165181

166-
protected function get_file_object($file_name) {
182+
protected function is_file_object($file_name) {
167183
$file_path = $this->get_upload_path($file_name);
168184
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)) {
169192
$file = new stdClass();
170193
$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+
);
172197
$file->url = $this->get_upload_url($file->name);
173198
foreach($this->options['image_versions'] as $version => $options) {
174199
if (!empty($version)) {
@@ -186,19 +211,27 @@ protected function get_file_object($file_name) {
186211
return null;
187212
}
188213

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+
}
190219
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)
193222
)));
194223
}
195224

225+
protected function count_file_objects() {
226+
return count($this->get_file_objects('is_file_object'));
227+
}
228+
196229
protected function create_scaled_image($file_name, $version, $options) {
197230
$file_path = $this->get_upload_path($file_name);
198231
if (!empty($version)) {
199232
$version_dir = $this->get_upload_path(null, $version);
200233
if (!is_dir($version_dir)) {
201-
mkdir($version_dir, 0755);
234+
mkdir($version_dir, $this->options['mkdir_mode']);
202235
}
203236
$new_file_path = $version_dir.'/'.$file_name;
204237
} else {
@@ -298,7 +331,7 @@ protected function validate($uploaded_file, $file, $error, $index) {
298331
return false;
299332
}
300333
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'])
302335
) {
303336
$file->error = $this->get_error_message('max_number_of_files');
304337
return false;
@@ -405,6 +438,10 @@ protected function handle_file_upload($uploaded_file, $name, $size, $type, $erro
405438
$file->type = $type;
406439
if ($this->validate($uploaded_file, $file, $error, $index)) {
407440
$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+
}
408445
$file_path = $this->get_upload_path($file->name);
409446
$append_file = $content_range && is_file($file_path) &&
410447
$file->size > $this->get_file_size($file_path);

0 commit comments

Comments
 (0)