Skip to content

Commit

Permalink
Added option "delete_type", which can be set to "POST" if the server …
Browse files Browse the repository at this point in the history
…does not support DELETE requests.

Change method and property visibility to protected, to allow access for
extending classes.
  • Loading branch information
blueimp committed Feb 14, 2012
1 parent 67b9dcd commit 94d6df8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 18 deletions.
8 changes: 6 additions & 2 deletions server/php/index.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/*
* jQuery File Upload Plugin PHP Example 5.6.1
* jQuery File Upload Plugin PHP Example 5.7
* https://github.com/blueimp/jQuery-File-Upload
*
* Copyright 2010, Sebastian Tschan
Expand Down Expand Up @@ -32,7 +32,11 @@
$upload_handler->get();
break;
case 'POST':
$upload_handler->post();
if (isset($_REQUEST['_method']) && $_REQUEST['_method'] === 'DELETE') {
$upload_handler->delete();
} else {
$upload_handler->post();
}
break;
case 'DELETE':
$upload_handler->delete();
Expand Down
40 changes: 24 additions & 16 deletions server/php/upload.class.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/*
* jQuery File Upload Plugin PHP Class 5.6
* jQuery File Upload Plugin PHP Class 5.7
* https://github.com/blueimp/jQuery-File-Upload
*
* Copyright 2010, Sebastian Tschan
Expand All @@ -12,14 +12,17 @@

class UploadHandler
{
private $options;
protected $options;

function __construct($options=null) {
$this->options = array(
'script_url' => $this->getFullUrl().'/',
'upload_dir' => dirname($_SERVER['SCRIPT_FILENAME']).'/files/',
'upload_url' => $this->getFullUrl().'/files/',
'param_name' => 'files',
// Set the following option to 'POST', if your server does not support
// DELETE requests. This is a parameter sent to the client:
'delete_type' => 'DELETE',
// The php.ini settings upload_max_filesize and post_max_size
// take precedence over the following max_file_size setting:
'max_file_size' => null,
Expand Down Expand Up @@ -55,7 +58,7 @@ function __construct($options=null) {
}
}

function getFullUrl() {
protected function getFullUrl() {
return
(isset($_SERVER['HTTPS']) ? 'https://' : 'http://').
(isset($_SERVER['REMOTE_USER']) ? $_SERVER['REMOTE_USER'].'@' : '').
Expand All @@ -65,7 +68,16 @@ function getFullUrl() {
substr($_SERVER['SCRIPT_NAME'],0, strrpos($_SERVER['SCRIPT_NAME'], '/'));
}

private function get_file_object($file_name) {
protected function set_file_delete_url($file) {
$file->delete_url = $this->options['script_url']
.'?file='.rawurlencode($file->name);
$file->delete_type = $this->options['delete_type'];
if ($file->delete_type !== 'DELETE') {
$file->delete_url .= '&_method=DELETE';
}
}

protected function get_file_object($file_name) {
$file_path = $this->options['upload_dir'].$file_name;
if (is_file($file_path) && $file_name[0] !== '.') {
$file = new stdClass();
Expand All @@ -78,22 +90,20 @@ private function get_file_object($file_name) {
.rawurlencode($file->name);
}
}
$file->delete_url = $this->options['script_url']
.'?file='.rawurlencode($file->name);
$file->delete_type = 'DELETE';
$this->set_file_delete_url($file);
return $file;
}
return null;
}

private function get_file_objects() {
protected function get_file_objects() {
return array_values(array_filter(array_map(
array($this, 'get_file_object'),
scandir($this->options['upload_dir'])
)));
}

private function create_scaled_image($file_name, $options) {
protected function create_scaled_image($file_name, $options) {
$file_path = $this->options['upload_dir'].$file_name;
$new_file_path = $options['upload_dir'].$file_name;
list($img_width, $img_height) = @getimagesize($file_path);
Expand Down Expand Up @@ -146,7 +156,7 @@ private function create_scaled_image($file_name, $options) {
return $success;
}

private function has_error($uploaded_file, $file, $error) {
protected function has_error($uploaded_file, $file, $error) {
if ($error) {
return $error;
}
Expand Down Expand Up @@ -176,7 +186,7 @@ private function has_error($uploaded_file, $file, $error) {
return $error;
}

private function trim_file_name($name, $type) {
protected function trim_file_name($name, $type) {
// Remove path information and dots around the filename, to prevent uploading
// into different directories or replacing hidden system files.
// Also remove control characters and spaces (\x00..\x20) around the filename:
Expand All @@ -189,7 +199,7 @@ private function trim_file_name($name, $type) {
return $file_name;
}

private function orient_image($file_path) {
protected function orient_image($file_path) {
$exif = exif_read_data($file_path);
$orientation = intval(@$exif['Orientation']);
if (!in_array($orientation, array(3, 6, 8))) {
Expand All @@ -215,7 +225,7 @@ private function orient_image($file_path) {
return $success;
}

private function handle_file_upload($uploaded_file, $name, $size, $type, $error) {
protected function handle_file_upload($uploaded_file, $name, $size, $type, $error) {
$file = new stdClass();
$file->name = $this->trim_file_name($name, $type);
$file->size = intval($size);
Expand Down Expand Up @@ -262,9 +272,7 @@ private function handle_file_upload($uploaded_file, $name, $size, $type, $error)
$file->error = 'abort';
}
$file->size = $file_size;
$file->delete_url = $this->options['script_url']
.'?file='.rawurlencode($file->name);
$file->delete_type = 'DELETE';
$this->set_file_delete_url($file);
} else {
$file->error = $error;
}
Expand Down

0 comments on commit 94d6df8

Please sign in to comment.