Add upload file from url
This commit is contained in:
parent
360a7c112f
commit
673f0433ed
@ -8,6 +8,7 @@
|
||||
<title>Local Transfer</title>
|
||||
</head>
|
||||
<body>
|
||||
<input type="text" id="url">
|
||||
<div class="transfer">
|
||||
<div id="drop-area">
|
||||
<form class="my-form">
|
||||
|
20
js/script.js
20
js/script.js
@ -55,6 +55,26 @@ function handleFiles(files) {
|
||||
files.forEach(previewFile)
|
||||
}
|
||||
|
||||
//upload file from url
|
||||
$('#url').keypress(function (e) {
|
||||
if (e.which == 13) {
|
||||
var url = encodeURIComponent($('#url').val());
|
||||
$.get({
|
||||
url: 'uploadUrl.php',
|
||||
data: 'url=' + url,
|
||||
beforeSend: function(){
|
||||
lock = 1;
|
||||
},
|
||||
success: function(data, statut) {
|
||||
lock = 0;
|
||||
listFile();
|
||||
},
|
||||
error: error,
|
||||
});
|
||||
return false;
|
||||
}
|
||||
});
|
||||
|
||||
function uploadFile(file) {
|
||||
let url = 'upload.php'
|
||||
let formData = new FormData()
|
||||
|
59
uploadUrl.php
Normal file
59
uploadUrl.php
Normal file
@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
error_reporting(E_ALL);
|
||||
ini_set('display_errors', TRUE);
|
||||
ini_set('display_startup_errors', TRUE);
|
||||
|
||||
include 'fonction.php';
|
||||
|
||||
$max_size = convertToBytes('2GB');
|
||||
|
||||
$root = $_SERVER["DOCUMENT_ROOT"];
|
||||
|
||||
$target_dir = "$root/uploads/";
|
||||
|
||||
if(! isset($_GET["url"])) {
|
||||
header("HTTP/1.1 400 No given url");
|
||||
exit();
|
||||
}
|
||||
|
||||
$url = $_GET["url"];
|
||||
|
||||
$headers = get_headers($url, true);
|
||||
|
||||
$name = basename($url);
|
||||
|
||||
$size = $headers['Content-Length'];
|
||||
|
||||
if ((folderSize($target_dir) + $size) > $max_size) {
|
||||
header("HTTP/1.1 400 " . $name . " file too large");
|
||||
exit();
|
||||
}
|
||||
|
||||
if (! extension_loaded('mbstring')) {
|
||||
header("HTTP/1.1 500 php module missing");
|
||||
exit();
|
||||
}
|
||||
|
||||
if(! mb_check_encoding($name)) {
|
||||
header("HTTP/1.1 400 Bad encode");
|
||||
exit();
|
||||
}
|
||||
|
||||
$target_file = $target_dir.$name;
|
||||
|
||||
// Check if file already exists
|
||||
if (file_exists($target_file)) {
|
||||
header("HTTP/1.1 400 This file already exists");
|
||||
echo "This file already exists.";
|
||||
}
|
||||
else {
|
||||
if(file_put_contents($target_file, fopen($url, 'r'))) {
|
||||
echo "The File ".$name." of size ".formatBytes($size)." has been uploaded.";
|
||||
} else {
|
||||
header("HTTP/1.1 500");
|
||||
echo "There was an error uploading your file.";
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
Loading…
x
Reference in New Issue
Block a user