Although you can run FTP commands using WebSockets,
the simpler solution is listing your files using opendir
in server side (PHP
), and "spitting" it into the HTML source-code, so it will be available to client side.
The following code will do just that,
Optionally -
<a>
tag to present a link.query for more information using server side (PHP
),
for example a file size,
PHP filesize TIP: also you can easily overcome the 2GB limit of PHP's filesize using: AJAX + HEAD request + .htaccess rule to allow Content-Length
access from client-side.
A Fully Working example can be found at my github repository: download.eladkarako.com
The following is a trimmed-down (simplified) example:
<?php
/* taken from: https://github.com/eladkarako/download.eladkarako.com */
$path = 'resources';
$files = [];
$handle = @opendir('./' . $path . '/');
while ($file = @readdir($handle))
("." !== $file && ".." !== $file) && array_push($files, $file);
@closedir($handle);
sort($files); //uksort($files, "strnatcasecmp");
$files = json_encode($files);
unset($handle,$ext,$file,$path);
?>
<!DOCTYPE html>
<html lang="en-US" dir="ltr">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=Edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<div data-container></div>
<script>
/* you will see (for example): 'var files = ["1.bat","1.exe","1.txt"];' if your folder containes those 1.bat 1.exe 1.txt files, it will be sorted too! :) */
var files = <?php echo $files; ?>;
files = files.map(function(file){
return '<a data-ext="##EXT##" download="##FILE##" href="http://download.eladkarako.com/resources/##FILE##">##FILE##</a>'
.replace(/##FILE##/g, file)
.replace(/##EXT##/g, file.split('.').slice(-1) )
;
}).join("\n<br/>\n");
document.querySelector('[data-container]').innerHTML = files;
</script>
</body>
</html>
DOM result will look like that:
<html lang="en-US" dir="ltr"><head>
<meta http-equiv="X-UA-Compatible" content="IE=Edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<div data-container="">
<a data-ext="bat" download="1.bat" href="http://download.eladkarako.com/resources/1.bat">1.bat</a>
<br/>
<a data-ext="exe" download="1.exe" href="http://download.eladkarako.com/resources/1.exe">1.exe</a>
<br/>
<a data-ext="txt" download="1.txt" href="http://download.eladkarako.com/resources/1.txt">1.txt</a>
<br/>
</div>
<script>
var files = ["1.bat","1.exe","1.txt"];
files = files.map(function(file){
return '<a data-ext="##EXT##" download="##FILE##" href="http://download.eladkarako.com/resources/##FILE##">##FILE##</a>'
.replace(/##FILE##/g, file)
.replace(/##EXT##/g, file.split('.').slice(-1) )
;
}).join("\n<br/>\n");
document.querySelector('[data-container').innerHTML = files;
</script>
</body></html>