Read Remote file through FTP with PHP

2

Hello, I would like your help to solve the following problem. And if possible help me understand what happens

Code:     

$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!");

$user['Nome'] = 'ftp://usuario:[email protected]/diretorio/arquivo.ini'; //aqui está o meu problema.

if (!file_exists($user['Nome'])) {
     echo 'Arquivo Inexistente!';
} else
{
    echo 'Arquivo Existente!';
}

Well, this is the code that should work, but it does not work. If I type in my browser:      ftp: // username: [email protected]/directional.file.ini The file opens and shows the data it contains. If I use it in PHP it does not even recognize that the file is there, I would like to know how I can resolve it.

To check if the file exists I could use:

$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_login($cHandle,"usuario","senha");

$dir = "/diretorio/";

$arq= "arquivo.ini";

$check_file_exist = $dir.$arq;

$contents_on_server = ftp_nlist($cHandle, $dir);

if (!in_array($check_file_exist, $contents_on_server)) 
{
    echo 'Arquivo Inexistente';
}else 
{
    echo ' Arquivo Existente';
}

It works, except that in this second form, it only checks the existence of the file and I would like it to check the file as it would open in the first option, I already searched, I tried, and I could not do it if someone could help me thank you.

So .. How can I make php open and read the file via ftp, the same way it opens the first try above?

    
asked by anonymous 31.07.2014 / 00:14

1 answer

2

To open an existing file on an FTP server, you would need to have it locally. Then you will use PHP to download the file from the 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);

After this the file can be opened, only locally. You can direct the user to the address of the file.

header("Location: diretorio_destino/arquivo.ini")

I hope I have helped. More questions you can take at: link

    
31.07.2014 / 14:52