Download FTP via PHP

Asked

Viewed 1,156 times

2

I need to remove a file from an FTP server using PHP. How to do this?

  • 5

    You say you’ve tried several codes, which ones? It’s important for us to know which ones to help... Already tried: http://php.net/manual/en/function.ftp-get.php ?

  • 1

    If it were . Net has the Webclient.Downloadfile method (URL, Save)

  • 2

    The problem is to download the FTP file to your web server, or from the web server to the browser? Really without code and more details will not give to answer.

2 answers

1

Your question is not very clear, but let’s see if I can help you. The following code can be used to fetch a file from the FTP server.

$server = "endereco_do_servidor.com.br";
$FTP_HOST = "ftp.exemplo.com";
$FTP_USER = "usuario";
$FTP_PASS = "senha";
$cHandle = ftp_connect($FTP_HOST) or die("O Servidor não pode se conectar ao FTP");
$login_result = ftp_login($cHandle, $FTP_USER, $FTP_PASS) or die("O Servidor não pode logar-se no FTP!");
ftp_get($cHandle, "diretorio_destino/arquivo.ini", "diretorio_origem/arquivo.ini", FTP_BINARY);

More questions you can ask in: http://php.net/manual/en/book.ftp.php

0

maybe this helps not to forget the permisation in the folder where the code will run

<?php

// define some variables
$local_file = 'teste.pdf'; //nome do arquivo sera salvo 
$server_file = './teste/teste1.pdf'; //aonde esta o arquivo
$ftp_server="204.145.90.95"; 
$ftp_user_name="loginusuario";
$ftp_porta="22";
$ftp_user_pass="senhausuario";

$conn_id = ftp_ssl_connect($ftp_server,$ftp_porta);

// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

// turn passive mode on
ftp_pasv($conn_id, true);

// try to download $server_file and save to $local_file
if (ftp_get($conn_id, $local_file, $server_file, FTP_BINARY)) {
    echo "Successfully written to $local_file\n";
}
else {
    echo "There was a problem\n";
}
// close the connection
ftp_close($conn_id);

?>

Browser other questions tagged

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