I have a code ready here, with upload to database and to a local folder that has a progress bar in javascript, however what I wanted was to use this progress bar to upload to a folder in an FTP and also to a database, but I am not able to change the code to make the connection to FTP. What I need to change to be sent to FTP instead of localhost. Here is 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><scriptsrc="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>
processa.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>";
}
connection.php
<?php
$servidor = "localhost";
$usuario = "root";
$senha = "";
$dbname = "teste";
//Criar a conexao
$conn = mysqli_connect($servidor, $usuario, $senha, $dbname);