Last active
August 29, 2015 14:16
-
-
Save vik0803/e5bc89fdefef60ec5666 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE HTML> | |
<!-- | |
/* | |
* Remote File Copy Demo 1.0 | |
* | |
* Copyright 2012, Sebastian Tschan | |
* https://blueimp.net | |
* | |
* Licensed under the MIT license: | |
* http://www.opensource.org/licenses/MIT | |
*/ | |
--> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<title>Remote File Copy Demo</title> | |
<meta name="viewport" content="width=device-width"> | |
<!-- Bootstrap CSS Toolkit styles --> | |
<link rel="stylesheet" href="http://blueimp.github.com/cdn/css/bootstrap.min.css"> | |
<!-- Bootstrap styles for responsive website layout, supporting different screen sizes --> | |
<link rel="stylesheet" href="http://blueimp.github.com/cdn/css/bootstrap-responsive.min.css"> | |
<!-- Bootstrap CSS fixes for IE6 --> | |
<!--[if lt IE 7]><link rel="stylesheet" href="http://blueimp.github.com/cdn/css/bootstrap-ie6.min.css"><![endif]--> | |
<!-- Shim to make HTML5 elements usable in older Internet Explorer versions --> | |
<!--[if lt IE 9]><script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script><![endif]--> | |
</head> | |
<body> | |
<div class="container"> | |
<div class="page-header"> | |
<h1>Remote File Copy Demo</h1> | |
</div> | |
<form class="form-inline" id="remote-file-copy"> | |
<div> | |
<input type="text" class="input-xlarge" placeholder="URL"> | |
<button type="submit" class="btn btn-primary"> | |
<i class="icon-upload icon-white"></i> | |
<span>Start</span> | |
</button> | |
</div> | |
<br> | |
<div class="progress progress-success progress-striped"> | |
<div class="bar" style="width:0%;"></div> | |
</div> | |
<ul class="files"></ul> | |
</form> | |
</div> | |
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> | |
<script> | |
var progressbar = $('#remote-file-copy .progress .bar'), | |
list = $('#remote-file-copy .files'), | |
callback = function (message) { | |
$.each(message, function (key, value) { | |
switch (key) { | |
case 'send': | |
progressbar.addClass('active'); | |
break; | |
case 'progress': | |
progressbar.css('width', parseInt(value.loaded / value.total * 100, 10) + '%'); | |
break; | |
case 'done': | |
progressbar.removeClass('active'); | |
$('<li>').text(value.name).appendTo(list); | |
break; | |
} | |
}); | |
}; | |
$('#remote-file-copy').on('submit', function (e) { | |
e.preventDefault(); | |
var url = $(this).find('input').val(), | |
iframe = $('<iframe src="javascript:false;" style="display:none;"></iframe>'); | |
if (url) { | |
iframe | |
.prop('src', 'remote-file-copy.php?url=' + encodeURIComponent(url)) | |
.appendTo(document.body); | |
} | |
}); | |
</script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Remote File Copy PHP Script 1.0 | |
* | |
* Copyright 2012, Sebastian Tschan | |
* https://blueimp.net | |
* | |
* Licensed under the MIT license: | |
* http://www.opensource.org/licenses/MIT | |
*/ | |
$upload_dir = getcwd()."/files"; | |
$url = !empty($_REQUEST["url"]) && preg_match("|^http(s)?://.+$|", stripslashes($_REQUEST["url"])) ? | |
stripslashes($_REQUEST["url"]) : null; | |
$callback = !empty($_REQUEST["callback"]) && preg_match("|^\w+$|", $_REQUEST["callback"]) ? | |
$_REQUEST["callback"] : "callback"; | |
$use_curl = defined("CURLOPT_PROGRESSFUNCTION"); | |
$temp_file = tempnam(sys_get_temp_dir(), "upload-"); | |
$fileinfo = new stdClass(); | |
$fileinfo->name = trim(basename($url), ".\x00..\x20"); | |
// 1KB of initial data, required by Webkit browsers: | |
echo "<span>".str_repeat("0", 1000)."</span>"; | |
function event_callback ($message) { | |
global $callback; | |
echo "<script>parent.".$callback."(".json_encode($message).");</script>"; | |
} | |
function get_file_path () { | |
global $upload_dir, $temp_file; | |
return $upload_dir."/".basename($temp_file); | |
} | |
function stream_notification_callback ($notification_code, $severity, $message, $message_code, $bytes_transferred, $bytes_max) { | |
global $fileinfo; | |
switch($notification_code) { | |
case STREAM_NOTIFY_FILE_SIZE_IS: | |
$fileinfo->size = $bytes_max; | |
break; | |
case STREAM_NOTIFY_MIME_TYPE_IS: | |
$fileinfo->type = $message; | |
break; | |
case STREAM_NOTIFY_PROGRESS: | |
if (!$bytes_transferred) { | |
event_callback(array("send" => $fileinfo)); | |
} | |
event_callback(array("progress" => array("loaded" => $bytes_transferred, "total" => $bytes_max))); | |
break; | |
} | |
} | |
function curl_progress_callback ($download_size, $downloaded_size, $upload_size, $uploaded_size) { | |
global $fileinfo; | |
if (!$downloaded_size) { | |
if (!isset($fileinfo->size)) { | |
$fileinfo->size = $download_size; | |
event_callback(array("send" => $fileinfo)); | |
} | |
} | |
event_callback(array("progress" => array("loaded" => $downloaded_size, "total" => $download_size))); | |
} | |
if (!$url) { | |
$success = false; | |
} else if ($use_curl) { | |
$fp = fopen($temp_file, "w"); | |
$ch = curl_init($url); | |
curl_setopt($ch, CURLOPT_NOPROGRESS, false ); | |
curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, "curl_progress_callback"); | |
curl_setopt($ch, CURLOPT_FILE, $fp); | |
$success = curl_exec($ch); | |
$curl_info = curl_getinfo($ch); | |
curl_close($ch); | |
fclose($fp); | |
$fileinfo->size = $curl_info["size_download"]; | |
$fileinfo->type = $curl_info["content_type"]; | |
} else { | |
$ctx = stream_context_create(); | |
stream_context_set_params($ctx, array("notification" => "stream_notification_callback")); | |
$success = copy($url, $temp_file, $ctx); | |
} | |
if ($success) { | |
rename($temp_file, get_file_path()); | |
event_callback(array("done" => $fileinfo)); | |
} else { | |
unlink($temp_file); | |
$err = error_get_last(); | |
if (!$err) { | |
$err = array("message" => "Invalid url parameter"); | |
} | |
event_callback(array("fail" => $err["message"])); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment