Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Used the adjusted onFileSuccess and onFileFail events as well as hand…

…led the different sent-in array name in that varies depending on the browsers.
  • Loading branch information...
commit 550ae9fff240e3dcc8cab8c0914f2133f6c26ee2 1 parent 607feeb
Cindy Li authored
View
22 css/styles.css
@@ -18,7 +18,23 @@
#demo-notes {
margin-left: 1em;
- clear: left;
- width: 40%;
- font-size: medium;
+ clear: left;
+ width: 40%;
+ font-size: medium;
+}
+
+#server-error {
+ margin-left: 1em;
+ margin-bottom: 1em;
+ clear: left;
+ width: 40%;
+ font-size: medium;
+ font-weight: bold;
+ color: #FF0000;
+}
+
+.image-frame {
+ float: left;
+ margin: 0.5em;
+ height: 150px;
}
View
5 include/config.inc.php
@@ -1,5 +0,0 @@
-<?php
-/* Where the images are saved */
-define('FLUID_IG_TEMP_DIR', 'C:\\Development\\image_gallery\\temp\\');
-
-?>
View
92 include/vitals.inc.php
@@ -3,29 +3,32 @@
define('FLUID_IG_DEVEL', 1);
-/* get the base url */
+// get the protocol
if (isset($_SERVER['HTTPS']) && (strtolower($_SERVER['HTTPS']) == 'on')) {
$server_protocol = 'https://';
} else {
$server_protocol = 'http://';
}
+// Calculate the base href
$dir_deep = substr_count(FLUID_IG_INCLUDE_PATH, '..');
$url_parts = explode('/', $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);
$_base_href = array_slice($url_parts, 0, count($url_parts) - $dir_deep-1);
$_base_href = $server_protocol . implode('/', $_base_href).'/';
-//if (($temp = strpos($_base_href, AT_PRETTY_URL_HANDLER)) > 0){
-// $endpos = $temp;
-//} else {
- $endpos = strlen($_base_href);
+$endpos = strlen($_base_href);
-//}
$_base_href = substr($_base_href, 0, $endpos);
$_base_path = substr($_base_href, strlen($server_protocol . $_SERVER['HTTP_HOST']));
define('FLUID_IG_BASE_HREF', $_base_href);
+/**
+ * Get the list of all the sub-directories in the given directory
+ * @access public
+ * @param string $directory the directory to search in
+ * @return an array of all the sub-directories
+ */
function getAllDirs($directory) {
$result = array();
$handle = opendir($directory);
@@ -44,13 +47,13 @@ function getAllDirs($directory) {
}
/**
-* Enables deletion of directory if not empty
+* Enables the deletion of a directory even if it is not empty
* @access public
-* @param string $dir the directory to delete
+* @param string $directory the directory to delete
* @return boolean whether the deletion was successful
*/
-function remove_dir($dir) {
- if(!$opendir = @opendir($dir)) {
+function remove_dir($directory) {
+ if(!$opendir = @opendir($directory)) {
return false;
}
@@ -60,13 +63,13 @@ function remove_dir($dir) {
clearstatcache(); /* especially needed for Windows machines: */
- if (is_file($dir.'/'.$readdir)) {
- if(!@unlink($dir.'/'.$readdir)) {
+ if (is_file($directory.'/'.$readdir)) {
+ if(!@unlink($directory.'/'.$readdir)) {
return false;
}
- } else if (is_dir($dir.'/'.$readdir)) {
+ } else if (is_dir($directory.'/'.$readdir)) {
/* calls itself to clear subdirectories */
- if(!remove_dir($dir.'/'.$readdir)) {
+ if(!remove_dir($directory.'/'.$readdir)) {
return false;
}
}
@@ -75,48 +78,51 @@ function remove_dir($dir) {
@closedir($opendir);
- if(!@rmdir($dir)) {
+ if(!@rmdir($directory)) {
return false;
}
return true;
}
/**
- * Scan through the given $folder, remove the sub-folders that are older than the given seconds.
- * @param $folder - string, the path to the folder
- * $secs_to_alive integer, seconds to keep the subfolder alive
+ * Scan through the given $directory, remove the sub-folders that are older than the given seconds.
+ * @access public
+ * @param string $directory the path to the folder
+ * integer $secs_to_live the seconds that the folder should not be deleted since its creation
+ * @return boolean
*/
-function clean_history($folder, $secs_to_alive) {
- $check_point = strtotime("-".$secs_to_alive." seconds");
+function clean_history($directory, $secs_to_live) {
+ $check_point = strtotime("-".$secs_to_live." seconds");
- $allDirs = getAllDirs($folder);
+ $allDirs = getAllDirs($directory);
$highestKnown = 0;
- foreach ($allDirs as $dir) {
- $currentValue = filectime($dir);
- $currentMValue = filemtime($dir);
+ foreach ($allDirs as $one_dir) {
+ $currentValue = filectime($one_dir);
+ $currentMValue = filemtime($one_dir);
if ($currentMValue > $currentValue) {
$currentValue = $currentMValue;
}
if ($currentValue < $check_point) {
- remove_dir($dir);
+ remove_dir($one_dir);
}
}
return true;
}
/**
- * Ensure the uniqueness of the file name ($file_name) in folder ($folder).
- * If the file with the same name already exists in the folder, attach suffix "-N" to the name.
+ * Ensure the uniqueness of the file name, $file_name, in the directory $directory.
+ * If the file with the same name already exists in the directory, attach suffix "-N" to the name.
+ * N is a incremented number calculated base on the existing file names.
* For instance, if "1.jpg" already exists, return "1-1.jpg"; if "1-1.jpg" already exists, return "1-2.jpg"
- * @param $file_name: the file name to check
- * $folder: the folder that the file resides
- * @return a unique file name
+ * @param string $file_name the file name to check
+ * string $directory the folder that the file resides
+ * @return string, a unique file name
*/
-function get_unique_name($file_name, $folder){
- if (file_exists($folder.$file_name)) {
+function get_unique_name($file_name, $directory){
+ if (file_exists($directory.$file_name)) {
$prefix = substr($file_name, 0, strrpos($file_name, '.'));
$extension = substr($file_name, strrpos($file_name, '.') + 1);
@@ -131,7 +137,7 @@ function get_unique_name($file_name, $folder){
// The first renamed file
$rtn = $prefix.'-1.'.$extension;
}
- return $rtn;
+ return get_unique_name($rtn, $directory);
} else {
return $file_name;
}
@@ -139,15 +145,19 @@ function get_unique_name($file_name, $folder){
/**
* Return error msg with http status code 400
+ * @access public
+ * @param string err_string the error message
*/
function return_error($err_string) {
header("HTTP/1.0 400 Bad Request");
header("Status: 400");
- echo "<html><body><p>".$errString."</p></body></html>";
+ echo $err_string;
}
/**
* Return success msg with http status code 200
+ * @access public
+ * @param $success_string the success message
*/
function return_success($success_string) {
echo "<html><body><p>".$success_string."</p></body></html>";
@@ -155,10 +165,9 @@ function return_success($success_string) {
/**
* This function is used for printing variables for debugging.
- * @access public
- * @param mixed $var The variable to output
- * @param string $title The name of the variable, or some mark-up identifier.
- * @author Joel Kronenberg
+ * @access public
+ * @param mixed $var The variable to output
+ * @param string $title The name of the variable, or some mark-up identifier.
*/
function debug($var, $title='') {
if (!defined('FLUID_IG_DEVEL') || !FLUID_IG_DEVEL) {
@@ -188,16 +197,15 @@ function debug($var, $title='') {
/**
* This function is used for printing variables into log file for debugging.
* @access public
- * @param mixed $var The variable to output
- * @param string $log The location of the log file. If not provided, use the default one.
- * @author Cindy Qi Li
+ * @param mixed $var The variable to output
+ * @param string $log The location of the log file. If not provided, use the default one.
*/
function debug_to_log($var, $log='') {
if (!defined('FLUID_IG_DEVEL') || !FLUID_IG_DEVEL) {
return;
}
- if ($log == '') $log = FLUID_IG_TEMP_DIR. 'debug.log';
+ if ($log == '') $log = 'temp/debug.log';
$handle = fopen($log, 'a');
fwrite($handle, "\n\n");
View
39 js/uploader.js
@@ -1,17 +1,3 @@
-/*
-Copyright 2008-2009 University of Cambridge
-Copyright 2008-2009 University of Toronto
-Copyright 2008-2009 University of California, Berkeley
-Copyright 2010 OCAD University
-
-Licensed under the Educational Community License (ECL), Version 2.0 or the New
-BSD license. You may not use this file except in compliance with one these
-Licenses.
-
-You may obtain a copy of the ECL 2.0 License and BSD License at
-https://source.fluidproject.org/svn/LICENSE.txt
-*/
-
/*global jQuery*/
/*global fluid*/
/*global demo*/
@@ -23,23 +9,32 @@ var demo = demo || {};
var sessionID = Math.random().toString(16).substring(2);
// Load the Uploader's template via AJAX and inject it into this page.
- var templateURLSelector = "../infusion/src/webapp/components/uploader/html/Uploader.html .fl-uploader";
+ var templateURLSelector = "../infusion-mlam/src/webapp/components/uploader/html/Uploader.html .fl-uploader";
$("#uploader-contents").load(templateURLSelector, null, function () {
- // Initialize the Uploader in demo mode.
+ // Initialize the Uploader
fluid.uploader(".flc-uploader", {
+ components: {
+ strategy: {
+ options: {
+ flashMovieSettings: {
+ flashURL: "../infusion-mlam/src/webapp/lib/swfupload/flash/swfupload.swf",
+ flashButtonImageURL: "../infusion-mlam/src/webapp/components/uploader/images/browse.png"
+ }
+ }
+ }
+ },
queueSettings: {
// Set the uploadURL to the URL for posting files to your server.
uploadURL: "uploader.php?session="+sessionID
},
listeners: {
- onFileSuccess: function (file, serverData){
- // example assumes that the server code passes the new image URL in the serverData
- //$('#image-space').append('<img src="' + serverData + '" alt="' + file.name +'" />');
- },
- onFileError: function (file, message){
+ onFileSuccess: function (file, responseText, xhr){
+ // the server code passes the new image URL in the responseText
+ $('#image-space').append('<img src="' + responseText + '" alt="' + file.name + '" class="image-frame" />');
},
- afterUploadComplete: function () {
+ onFileError: function (file, error, status, xhr){
+ $('#server-error').append(file.name + " - " + xhr.responseText + "<br />");
}
}
});
View
56 uploader.html
@@ -4,34 +4,34 @@
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Uploader</title>
- <link rel="stylesheet" type="text/css" href="../infusion/src/webapp/framework/fss/css/fss-reset.css" />
- <link rel="stylesheet" type="text/css" href="../infusion/src/webapp/framework/fss/css/fss-layout.css" />
- <link rel="stylesheet" type="text/css" href="../infusion/src/webapp/components/uploader/css/Uploader.css" />
+ <link rel="stylesheet" type="text/css" href="../infusion-mlam/src/webapp/framework/fss/css/fss-reset.css" />
+ <link rel="stylesheet" type="text/css" href="../infusion-mlam/src/webapp/framework/fss/css/fss-layout.css" />
+ <link rel="stylesheet" type="text/css" href="../infusion-mlam/src/webapp/components/uploader/css/Uploader.css" />
<!-- Fluid and jQuery Dependencies -->
- <script type="text/javascript" src="../infusion/src/webapp/lib/jquery/core/js/jquery.js"></script>
- <script type="text/javascript" src="../infusion/src/webapp/lib/jquery/ui/js/jquery.ui.core.js"></script>
- <script type="text/javascript" src="../infusion/src/webapp/framework/core/js/jquery.keyboard-a11y.js"></script>
- <script type="text/javascript" src="../infusion/src/webapp/lib/jquery/plugins/scrollTo/js/jquery.scrollTo.js"></script>
- <script type="text/javascript" src="../infusion/src/webapp/lib/swfobject/js/swfobject.js"></script>
- <script type="text/javascript" src="../infusion/src/webapp/lib/swfupload/js/swfupload.js"></script>
+ <script type="text/javascript" src="../infusion-mlam/src/webapp/lib/jquery/core/js/jquery.js"></script>
+ <script type="text/javascript" src="../infusion-mlam/src/webapp/lib/jquery/ui/js/jquery.ui.core.js"></script>
+ <script type="text/javascript" src="../infusion-mlam/src/webapp/framework/core/js/jquery.keyboard-a11y.js"></script>
+ <script type="text/javascript" src="../infusion-mlam/src/webapp/lib/jquery/plugins/scrollTo/js/jquery.scrollTo.js"></script>
+ <script type="text/javascript" src="../infusion-mlam/src/webapp/lib/swfobject/js/swfobject.js"></script>
+ <script type="text/javascript" src="../infusion-mlam/src/webapp/lib/swfupload/js/swfupload.js"></script>
- <script type="text/javascript" src="../infusion/src/webapp/framework/core/js/Fluid.js"></script>
- <script type="text/javascript" src="../infusion/src/webapp/framework/core/js/DataBinding.js"></script>
- <script type="text/javascript" src="../infusion/src/webapp/framework/core/js/FluidIoC.js"></script>
- <script type="text/javascript" src="../infusion/src/webapp/framework/core/js/FluidDocument.js"></script>
- <script type="text/javascript" src="../infusion/src/webapp/framework/core/js/FluidView.js"></script>
- <script type="text/javascript" src="../infusion/src/webapp/framework/enhancement/js/ProgressiveEnhancement.js"></script>
+ <script type="text/javascript" src="../infusion-mlam/src/webapp/framework/core/js/Fluid.js"></script>
+ <script type="text/javascript" src="../infusion-mlam/src/webapp/framework/core/js/DataBinding.js"></script>
+ <script type="text/javascript" src="../infusion-mlam/src/webapp/framework/core/js/FluidIoC.js"></script>
+ <script type="text/javascript" src="../infusion-mlam/src/webapp/framework/core/js/FluidDocument.js"></script>
+ <script type="text/javascript" src="../infusion-mlam/src/webapp/framework/core/js/FluidView.js"></script>
+ <script type="text/javascript" src="../infusion-mlam/src/webapp/framework/enhancement/js/ProgressiveEnhancement.js"></script>
<!-- Uploader dependencies -->
- <script type="text/javascript" src="../infusion/src/webapp/components/uploader/js/Uploader.js"></script>
- <script type="text/javascript" src="../infusion/src/webapp/components/uploader/js/FileQueue.js"></script>
- <script type="text/javascript" src="../infusion/src/webapp/components/progress/js/Progress.js"></script>
- <script type="text/javascript" src="../infusion/src/webapp/components/uploader/js/FileQueueView.js"></script>
- <script type="text/javascript" src="../infusion/src/webapp/components/uploader/js/FlashUploaderSupport.js"></script>
- <script type="text/javascript" src="../infusion/src/webapp/components/uploader/js/Flash9UploaderSupport.js"></script>
- <script type="text/javascript" src="../infusion/src/webapp/components/uploader/js/HTML5UploaderSupport.js"></script>
- <script type="text/javascript" src="../infusion/src/webapp/components/uploader/js/DemoUploadManager.js"></script>
+ <script type="text/javascript" src="../infusion-mlam/src/webapp/components/uploader/js/Uploader.js"></script>
+ <script type="text/javascript" src="../infusion-mlam/src/webapp/components/uploader/js/FileQueue.js"></script>
+ <script type="text/javascript" src="../infusion-mlam/src/webapp/components/progress/js/Progress.js"></script>
+ <script type="text/javascript" src="../infusion-mlam/src/webapp/components/uploader/js/FileQueueView.js"></script>
+ <script type="text/javascript" src="../infusion-mlam/src/webapp/components/uploader/js/FlashUploaderSupport.js"></script>
+ <script type="text/javascript" src="../infusion-mlam/src/webapp/components/uploader/js/Flash9UploaderSupport.js"></script>
+ <script type="text/javascript" src="../infusion-mlam/src/webapp/components/uploader/js/HTML5UploaderSupport.js"></script>
+ <script type="text/javascript" src="../infusion-mlam/src/webapp/components/uploader/js/DemoUploadManager.js"></script>
<!-- The Uploader demo -->
<link rel="stylesheet" type="text/css" href="css/styles.css" />
@@ -49,10 +49,14 @@
<h2>Uploaded images:</h2>
<div id="image-space"></div>
</div>
-
+
+ <div id="server-error">
+ <!-- The returned server error message will be displayed in this container. -->
+ </div>
+
<div id="demo-notes">
- 1. Your current session is valid for an hour. All uploaded images will be deleted from the server once the session is time out.<br />
- 2. Allowed uploaded file extension (case insensitive): jpg, png, gif, tif
+ 1. Your session will stay active for an hour. After one hour, all the uploaded images will be erased from the server.<br />
+ 2. Allowed uploaded file types (case insensitive): jpg, png, gif, tif
</div>
<script type="text/javascript">
View
81 uploader.php
@@ -8,19 +8,18 @@
$secs_to_timeout = 3600; // The seconds to keep the uploaded images
$temp_dir = 'temp/';
-//debug_to_log($_FILES);
-//debug_to_log($_REQUEST);
// Removed all the folders that are older than 60 mins
clean_history($temp_dir, $secs_to_timeout);
// Error checkings:
-// 1. if there is file received;
-// 2. if session id is provided;
-// 3. if the file extension is valid.
-// 4. the existence of $temp_dir (@see include/config.inc.php)
-
+// 1. wether the file is received;
+// 2. wether session id is provided;
+// 3. wether the file extension is allowed;
+// 4. the existence of $temp_dir.
+debug_to_log($_FILES);
// 1. Return error if there is no file received
-if (!isset($_FILES['fileData']) || !is_array($_FILES['fileData'])) {
+if (count($_FILES) == 0) {
+ debug_to_log("no file received");
return_error("No file is received at server.");
exit;
}
@@ -31,38 +30,38 @@
exit;
}
-$file_data = $_FILES['fileData'];
-
-// 3. Return error if the file extension is not in the list that is allowed
-$file_name = $file_data['name'];
-$file_extension = strtolower(substr($file_name, strrpos($file_name, '.') + 1));
-
-if (!in_array($file_extension, $allowed_file_extensions)) {
- return_error('File extension <span style="font: bold">'.$file_extension.'</span> is not allowed.');
- exit;
-}
-
-// 4. Return error if $temp_dir does not exist
-if (!file_exists($temp_dir)) {
- return_error('Temp folder <span style="font: bold">'.$temp_dir.'</span> does not exist.');
- exit;
-}
-
-// Find or even create the image folder for this round of upload
-$image_folder = $temp_dir . $_REQUEST['session'].'/';
-if (!file_exists($image_folder) && !mkdir($image_folder)) {
- return_error('Cannot create image folder <span style="font: bold">'.$image_folder.'</span>.');
- exit;
+foreach ($_FILES as $name => $file_data) {
+ // 3. Return error if the file extension is not in the list that is allowed
+ $file_name = $file_data['name'];
+ $file_extension = strtolower(substr($file_name, strrpos($file_name, '.') + 1));
+
+ if (!in_array($file_extension, $allowed_file_extensions)) {
+ return_error('File extension <span style="font: bold">'.$file_extension.'</span> is not allowed.');
+ exit;
+ }
+
+ // 4. Return error if $temp_dir does not exist
+ if (!file_exists($temp_dir)) {
+ return_error('Temp folder <span style="font: bold">'.$temp_dir.'</span> does not exist.');
+ exit;
+ }
+
+ // Find or even create the image folder for this round of upload
+ $image_folder = $temp_dir . $_REQUEST['session'].'/';
+ if (!file_exists($image_folder) && !mkdir($image_folder)) {
+ return_error('Cannot create image folder <span style="font: bold">'.$image_folder.'</span>.');
+ exit;
+ }
+ // END OF error checking
+
+ // Get a unique file name in case the file with the same name has been uploaded before
+ $file_name = get_unique_name($file_name, $image_folder);
+
+ $destination = $image_folder.$file_name;
+
+ // Copy the uploaded file into the image folder
+ move_uploaded_file($file_data['tmp_name'], $destination);
+
+ echo FLUID_IG_BASE_HREF.$destination;
}
-// END OF error handling
-
-// Get a unique file name in case the file with the same name has been uploaded before
-$file_name = get_unique_name($file_name, $image_folder);
-
-$destination = $image_folder.$file_name;
-
-// Copy the uploaded file into the image folder
-move_uploaded_file($file_data['tmp_name'], $destination);
-
-echo FLUID_IG_BASE_HREF.$destination;
?>
Please sign in to comment.
Something went wrong with that request. Please try again.