Upload FTP - PHP Progress Bar

Asked

Viewed 214 times

0

I have a code ready here, with upload to database and to a local folder that has a javascript progress bar, but what I wanted was to use this progress bar to upload to a folder on an FTP and also to a database, but I’m not able to change the code to make the connection with FTP. What I need to change to get sent to FTP instead of localhost. Follow the code below:

<body>
    <div class="container">
        <h1>Cadastrar Imagem</h1>
        <?php
        if(isset($_SESSION['msg'])){
            echo $_SESSION['msg'];
            unset($_SESSION['msg']);
        }
        ?>
        <hr>
        <form action="#" class="form-horizontal"> 
            <div class="form-group">
                <label class="col-sm-2 control-label">Titulo</label>
                <div class="col-sm-10">
                    <input type="text" name="titulo" class="form-control" placeholder="Digite o titulo">
                </div>
            </div>

            <div class="form-group">
                <label class="col-sm-2 control-label">Imagem</label>
                <div class="col-sm-10">
                    <input type="file" name="arquivo" class="form-control">
                </div>
            </div>

            <div class="form-group">
                <label class="col-sm-2 control-label"></label>
                <div class="col-sm-10">
                    <div class="progress progress-striped active">
                        <div class="progress-bar" style="width: 0%">
                        </div>
                    </div>
                </div>
            </div>

            <div class="form-group">
                <div class="col-sm-offset-2 col-sm-10">
                    <button type="submit" class="btn btn-success upload">Cadastrar</button>
                </div>
            </div>
        </form>
    </div>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script src="js/bootstrap.min.js"></script>
    <script>
        $(document).on('submit', 'form', function (e) {
            e.preventDefault();
            //Receber os dados
            $form = $(this);                
            var formdata = new FormData($form[0]);

            //Criar a conexao com o servidor
            var request = new XMLHttpRequest();

            //Progresso do Upload
            request.upload.addEventListener('progress', function (e) {
                var percent = Math.round(e.loaded / e.total * 100);
                $form.find('.progress-bar').width(percent + '%').html(percent + '%');
            });

            //Upload completo limpar a barra de progresso
            request.addEventListener('load', function(e){
                $form.find('.progress-bar').addClass('progress-bar-success').html('upload completo...');
                //Atualizar a página após o upload completo
                setTimeout("window.open(self.location, '_self');", 1000);
            });

            //Arquivo responsável em fazer o upload da imagem
            request.open('post', 'processa.php');
            request.send(formdata);
        });
    </script>
</body>

parses.php:

include_once('conexao.php')

$tmp_name = $_FILES['arquivo']['tmp_name'];
$name = $_FILES['arquivo']['name'];
$titulo = $_POST['titulo'];


move_uploaded_file($tmp_name, '/public_html/softwares/'. $name);

$result_imagem = "INSERT INTO imagens (nome_imagem, titulo) VALUES ('$name', '$titulo')";
$resultado_imagem = mysqli_query($conn, $result_imagem);

if(mysqli_insert_id($conn)){
    $_SESSION['msg'] = "<div class='alert alert-success'>Imagem cadastrada com sucesso!</div>";
}else{
    $_SESSION['msg'] = "<div class='alert alert-danger'>Erro ao cadastrar a imagem!</div>";
}

php connection.

<?php
$servidor = "localhost";
$usuario = "root";
$senha = "";
$dbname = "teste";

//Criar a conexao
$conn = mysqli_connect($servidor, $usuario, $senha, $dbname);
  • Have you tried using the documentation examples? http://php.net/manual/en/function.ftp-put.php

  • So, I have another php file that does this neat upload, but I’m not able to adapt it to work with this progress bar I mentioned above. This file uses this ftp-put function.

  • By default ftp_put does not accept callback, IE, you could not catch the progress of it... maybe if you create another file with ftp_nb_get, and you do the query in it via ajax, ai vc can implement the progress bar... "download" in parallel and returns pro browser, ai in browser vc calculates the % of the original size with which it was "uploaded/downloaded"... Got it?

  • so the way I’m using there’s no way to put the progress bar right? This ftp_nb_get function is very complicated to use ?

  • http://php.net/manual/en/function.ftp-nb-fget.php has the examples in the documentation

  • so I googled that, it’s not as trivial as a form upload

  • just to get it right, I would have to use ftp_put to uproot the file and ftp_nb_get to "download" the information to the progress bar ?

  • yes, the solutions I saw were in that footprint...

Show 3 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.