How to upload files from an ftp server on my website? [closed]

0

I need to show on my website a list of the files that are on an FTP server, and that these can also be opened from the web. Can someone help me with this?

    
asked by Jade 20.03.2018 в 20:06
source

1 answer

1

If I do not understand correctly in one of the comments, the ftp is in another server which is not possible to access normally with open_dir. In this case I can think of two solutions:

Mount the FTP folder on the web server. Here you have a detailed explanation, although it will depend on your servers, if one of them or both are windows, it would be very different: link

Login with ftp_connect from php

<?php
    $ftp_user_name = "TU_USUARIO";
    $ftp_user_pass = "TU_PASSWORD";
    $ftp_server = "ftp.example.com";
    $conn_id = ftp_connect($ftp_server);
    $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
    if ((!$conn_id) || (!$login_result)) {
        die("La conexión FTP ha fallado!");
    }
    echo "Directorio actual: " . ftp_pwd($conn_id) . "\n";
    if (ftp_chdir($conn_id, "DIRECTORIO/DE/FICHEROS/")) {
        echo "El directorio actual es: " . ftp_pwd($conn_id) . "\n";
    } else {
        echo "No se pudo cambiar al directorio\n";
    }
    $contents = ftp_mlsd($conn_id, ".");
    var_dump($contents); // Aquí puedes recorrer el array devuelto para mostrar los ficheros.
    ftp_close($conn_id);
?>

Here you can access the complete documentation of ftp commands: link

To be able to show the files when connecting with FTP, it occurs to me to download them temporarily to the web server to later send it to the user. You could get it with an ajax request. Starting from the previously generated array, you show the files in HTML with an added download class:

<?php
    $contents = "EL ARRAY DEL FTP";
?>
<!doctype html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Listado</title>
    </head>
    <body>
        <ul>
            <?php
                foreach($contents as $e) {
                    foreach($e as $file) {
                        echo "<li class='download'>" . $file["name"] . "</li>";
                    }
                }
            ?>
        </ul>
        <script src="http://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
        <script>
            $(function() {
                $(".download").click(function() {
                    var file = $(this).html();
                    $.ajax({
                        method: "POST",
                        url: "download.php",
                        data: {
                            file: file
                        }
                    });
                });
            });
        </script>
    </body>
</html>

In the PHP part, you just have to connect to FTP, download the file with ftp_fget:

<?php
    $remote_file = $_POST["file"];
    $local_file = "CARPETA/TEMPORAL/" . $_POST["file"];
    $handle = fopen($local_file, 'w');
    $conn_id = ftp_connect($ftp_server);
    $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
    ftp_fget($conn_id, $handle, $remote_file, FTP_ASCII, 0);
    ftp_close($conn_id);
    fclose($handle);
?>

With this you already have the file in $ local_file with what you can do a simple header ("Location:". $ local_file); for example (or from the AJAX part with the .done, having returned the file path or using the file variable), changing the local path to the accessible one from the browser, logically the local folder you have to make it accessible. Another important thing is that if the file to be downloaded is passed through POST or GET, you can request to download anything from the server, so it is important to take it into account to protect that attack vector.

    
answered by 20.03.2018 в 22:05