Skip to content

Commit 8ac51b4

Browse files
committed
Added get_file_size method to workaround filesize() limitations.
Thanks to Julien Moreau (@PixEye) for his report and fix. Also replaced tabs with spaces.
1 parent 5ffa5df commit 8ac51b4

File tree

1 file changed

+52
-41
lines changed

1 file changed

+52
-41
lines changed

server/php/upload.class.php

+52-41
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/*
3-
* jQuery File Upload Plugin PHP Class 5.12
3+
* jQuery File Upload Plugin PHP Class 5.13
44
* https://github.com/blueimp/jQuery-File-Upload
55
*
66
* Copyright 2010, Sebastian Tschan
@@ -86,13 +86,13 @@ function __construct($options=null) {
8686

8787
protected function get_full_url() {
8888
$https = !empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off';
89-
return
90-
($https ? 'https://' : 'http://').
91-
(!empty($_SERVER['REMOTE_USER']) ? $_SERVER['REMOTE_USER'].'@' : '').
92-
(isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : ($_SERVER['SERVER_NAME'].
93-
($https && $_SERVER['SERVER_PORT'] === 443 ||
94-
$_SERVER['SERVER_PORT'] === 80 ? '' : ':'.$_SERVER['SERVER_PORT']))).
95-
substr($_SERVER['SCRIPT_NAME'],0, strrpos($_SERVER['SCRIPT_NAME'], '/'));
89+
return
90+
($https ? 'https://' : 'http://').
91+
(!empty($_SERVER['REMOTE_USER']) ? $_SERVER['REMOTE_USER'].'@' : '').
92+
(isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : ($_SERVER['SERVER_NAME'].
93+
($https && $_SERVER['SERVER_PORT'] === 443 ||
94+
$_SERVER['SERVER_PORT'] === 80 ? '' : ':'.$_SERVER['SERVER_PORT']))).
95+
substr($_SERVER['SCRIPT_NAME'],0, strrpos($_SERVER['SCRIPT_NAME'], '/'));
9696
}
9797

9898
protected function set_file_delete_url($file) {
@@ -104,12 +104,25 @@ protected function set_file_delete_url($file) {
104104
}
105105
}
106106

107+
protected function get_file_size($file_path, $clear_stat_cache = false) {
108+
if ($clear_stat_cache) {
109+
clearstatcache();
110+
}
111+
$size = filesize($file_path);
112+
if ($size < 0) {
113+
// Fix for overflowing signed 32 bit integers,
114+
// works for files up to 2^32-1 bytes (4 GiB - 1) in size:
115+
$size += 2.0 * (PHP_INT_MAX + 1);
116+
}
117+
return $size;
118+
}
119+
107120
protected function get_file_object($file_name) {
108121
$file_path = $this->options['upload_dir'].$file_name;
109122
if (is_file($file_path) && $file_name[0] !== '.') {
110123
$file = new stdClass();
111124
$file->name = $file_name;
112-
$file->size = filesize($file_path);
125+
$file->size = $this->get_file_size($file_path);
113126
$file->url = $this->options['upload_url'].rawurlencode($file->name);
114127
foreach($this->options['image_versions'] as $version => $options) {
115128
if (is_file($options['upload_dir'].$file_name)) {
@@ -210,7 +223,7 @@ protected function validate($uploaded_file, $file, $error, $index) {
210223
return false;
211224
}
212225
if ($uploaded_file && is_uploaded_file($uploaded_file)) {
213-
$file_size = filesize($uploaded_file);
226+
$file_size = $this->get_file_size($uploaded_file);
214227
} else {
215228
$file_size = $_SERVER['CONTENT_LENGTH'];
216229
}
@@ -292,32 +305,32 @@ protected function handle_form_data($file, $index) {
292305
}
293306

294307
protected function orient_image($file_path) {
295-
$exif = @exif_read_data($file_path);
308+
$exif = @exif_read_data($file_path);
296309
if ($exif === false) {
297310
return false;
298311
}
299-
$orientation = intval(@$exif['Orientation']);
300-
if (!in_array($orientation, array(3, 6, 8))) {
301-
return false;
302-
}
303-
$image = @imagecreatefromjpeg($file_path);
304-
switch ($orientation) {
305-
case 3:
306-
$image = @imagerotate($image, 180, 0);
307-
break;
308-
case 6:
309-
$image = @imagerotate($image, 270, 0);
310-
break;
311-
case 8:
312-
$image = @imagerotate($image, 90, 0);
313-
break;
314-
default:
315-
return false;
316-
}
317-
$success = imagejpeg($image, $file_path);
318-
// Free up memory (imagedestroy does not delete files):
319-
@imagedestroy($image);
320-
return $success;
312+
$orientation = intval(@$exif['Orientation']);
313+
if (!in_array($orientation, array(3, 6, 8))) {
314+
return false;
315+
}
316+
$image = @imagecreatefromjpeg($file_path);
317+
switch ($orientation) {
318+
case 3:
319+
$image = @imagerotate($image, 180, 0);
320+
break;
321+
case 6:
322+
$image = @imagerotate($image, 270, 0);
323+
break;
324+
case 8:
325+
$image = @imagerotate($image, 90, 0);
326+
break;
327+
default:
328+
return false;
329+
}
330+
$success = imagejpeg($image, $file_path);
331+
// Free up memory (imagedestroy does not delete files):
332+
@imagedestroy($image);
333+
return $success;
321334
}
322335

323336
protected function handle_file_upload($uploaded_file, $name, $size, $type, $error, $index = null) {
@@ -329,8 +342,7 @@ protected function handle_file_upload($uploaded_file, $name, $size, $type, $erro
329342
$this->handle_form_data($file, $index);
330343
$file_path = $this->options['upload_dir'].$file->name;
331344
$append_file = !$this->options['discard_aborted_uploads'] &&
332-
is_file($file_path) && $file->size > filesize($file_path);
333-
clearstatcache();
345+
is_file($file_path) && $file->size > $this->get_file_size($file_path);
334346
if ($uploaded_file && is_uploaded_file($uploaded_file)) {
335347
// multipart/formdata uploads (POST method uploads)
336348
if ($append_file) {
@@ -350,20 +362,19 @@ protected function handle_file_upload($uploaded_file, $name, $size, $type, $erro
350362
$append_file ? FILE_APPEND : 0
351363
);
352364
}
353-
$file_size = filesize($file_path);
365+
$file_size = $this->get_file_size($file_path, $append_file);
354366
if ($file_size === $file->size) {
355-
if ($this->options['orient_image']) {
356-
$this->orient_image($file_path);
357-
}
367+
if ($this->options['orient_image']) {
368+
$this->orient_image($file_path);
369+
}
358370
$file->url = $this->options['upload_url'].rawurlencode($file->name);
359371
foreach($this->options['image_versions'] as $version => $options) {
360372
if ($this->create_scaled_image($file->name, $options)) {
361373
if ($this->options['upload_dir'] !== $options['upload_dir']) {
362374
$file->{$version.'_url'} = $options['upload_url']
363375
.rawurlencode($file->name);
364376
} else {
365-
clearstatcache();
366-
$file_size = filesize($file_path);
377+
$file_size = $this->get_file_size($file_path, true);
367378
}
368379
}
369380
}

0 commit comments

Comments
 (0)