UPload - Transferência de arquivos PHP

Come here to discuss FileZilla and FTP in general

Moderator: Project members

Post Reply
Message
Author
digitosistemas
504 Command not implemented
Posts: 6
Joined: 2023-08-15 02:22
First name: Dirceu
Last name: Morais

UPload - Transferência de arquivos PHP

#1 Post by digitosistemas » 2023-08-15 21:35

Estou problema aqui já faz alguns dias e não consigo resolver.

Tenho um aplicativo desenvolvido em PHP, CSS, HTML tudo misturado, rsrsrs, este aplicativo funcionou muito bem até meados de julho/2023. Eu estava usando para upload dos pedidos de vendas feito no tablet o comando: ftp_put($ftp, $ftp_arquivo, $local_arquivo, FTP_ASCII) e para download dos arquivos de produtos e clientes o ftp_get($conn_id,$local_file, $remote_file, FTP_ASCII).

1ª pergunta: Por que o ftp_get e ftp_put parou de funcionar?

Obs: Para ENVIO LOCAL, na mesma rede continua funcionando os dois comandos, mas estando usando outra rede lan não funciona. Aplicativo até conecta mas não consegue transferir os arquivos, nem para Download e Upload.

Para receber o arquivos necessários consegui resolver da seguinte forma:

Code: Select all

$curl = curl_init($ftp_server.$ftp_porta);
$fo = fopen("$local_file", 'w');
 
curl_setopt($curl, CURLOPT_URL, "ftp://$ftp_server:$ftp_porta/$remote_file"); #input
curl_setopt($curl, CURLOPT_TRANSFERTEXT, TRUE);
curl_setopt($curl, CURLOPT_FILE, $fo); #output
curl_setopt($curl, CURLOPT_USERPWD, "$ftp_user_name:$ftp_user_pass");
curl_setopt($curl, CURLOPT_FRESH_CONNECT, true);
if(file_exists($localFile)) {
    curl_setopt($curl, CURLOPT_RESUME_FROM, filesize($local_File));
}
 
curl_exec($curl);
 
fclose($fo);
 
curl_close($curl);
Mas para Enviar os arquivos não estou conseguindo, mudei o ftp_put para o ftp_raw da seguinte forma:

Code: Select all

$ftp = ftp_connect($servidor,$porta); // Retorno: true ou false
ftp_raw($ftp, "AUTH TLS");
ftp_raw($ftp, "AUTH SSL");
 
// Faz o login no servidor FTP
$login = ftp_login($ftp, $usuario, $senha); // Retorno: true ou false
//*****************************************************************//
ftp_raw($ftp, "CWD /backup");
ftp_raw($ftp, "TYPE A");
// Envia o arquivo pelo FTP em modo ASCII
$connect = ftp_raw($ftp, "PASV");
         
// parse the response and build the IP and port from the values
if (count($connect) > 0 && preg_match("/.*\((\d+),(\d+),(\d+),(\d+),(\d+),(\d+)\)/", $connect[0], $m)) {
    $address="{$m[1]}.{$m[2]}.{$m[3]}.{$m[4]}";
    $port=$m[5] * 256 + $m[6];
     
    print_r(ftp_raw($ftp, "STOR $ftp_arquivo"));
     
    $sock = socket_create(AF_INET, SOCK_STREAM, 0);
    if ($sock) {
        socket_connect($sock, $address, $port);
        socket_write($sock, file_get_contents($ftp_arquivo));
        socket_close($sock);
    }
}
ftp_raw($ftp,'QUIT');
Dessa forma que estou tentando transferir, quase que dá certo, executa todos os processos, aparece a mensagem que esta sendo transferido, mas dá erro conforme log abaixo:

Code: Select all

(000036)11/08/2023 16:38:50 - mymobile (179.54.xxx.xxx)> PASV
(000036)11/08/2023 16:38:50 - mymobile (179.54.xxx.xxx)> 227 Entering Passive Mode (xxx,xxx,xxx,xxx,19,171)
(000036)11/08/2023 16:38:50 - mymobile (179.54.xxx.xxx)> STOR pedidos1013110823-10.xml
(000036)11/08/2023 16:39:00 - mymobile (179.54.xxx.xxx)> 425 Can't open data connection.
(000036)11/08/2023 16:39:00 - mymobile (179.54.xxx.xxx)> 425 Can't open data connection.

O interessante é que, fazendo a transferência pelo FileZilla Client não dá erro o arquivo é transferido normalmente, conforme podemos ver no log abaixo.

Code: Select all

(000035)11/08/2023 16:37:36 - mymobile (179.54.xxx.xxx)> 200 Type set to A
(000035)11/08/2023 16:37:36 - mymobile (179.54.xxx.xxx)> PASV
(000035)11/08/2023 16:37:36 - mymobile (179.54.xxx.xxx)> 227 Entering Passive Mode (xxx,xxx,xxx,xxx,19,169)
(000035)11/08/2023 16:37:36 - mymobile (179.54.xxx.xxx)> STOR pedidos1013110823-10.xml
(000035)11/08/2023 16:37:36 - mymobile (179.54.xxx.xxx)> 150 Connection accepted
(000035)11/08/2023 16:37:36 - mymobile (179.54.xxx.xxx)> 226 Transfer OK

2ª pergunta: O que estou fazendo de errado? Existe outra solução?

Sobre o erro 425 eu já liberei as portas no modem e no fileZilla
Porta: 2121 no modem e no fileZilla
Porta passiva: 50000-50100 no modem e Filezilla

User avatar
boco
Contributor
Posts: 26940
Joined: 2006-05-01 03:28
Location: Germany

Re: UPload - Transferência de arquivos PHP

#2 Post by boco » 2023-08-15 21:51

These forums are English-only. Please ask your question in English.
No support requests over PM! You will NOT get any reply!!!
FTP connection problems? Please read Network Configuration.
FileZilla connection test: https://filezilla-project.org/conntest.php
FileZilla Pro support: https://customerforum.fileZilla-project.org

digitosistemas
504 Command not implemented
Posts: 6
Joined: 2023-08-15 02:22
First name: Dirceu
Last name: Morais

Re: UPload - Transferência de arquivos PHP

#3 Post by digitosistemas » 2023-08-16 11:29

TRANSLATE TOPIC

I've been having this problem for a few days and I can't solve it.

I have an application developed in PHP, CSS, HTML all mixed up, hehehe, this application worked very well until mid-July/2023. I was using the command: ftp_put($ftp, $ftp_file, $local_file, FTP_ASCII) to upload the sales orders made on the tablet and to download the product and customer files the ftp_get($conn_id,$local_file, $remote_file, FTP_ASCII ).

1st question: Why did ftp_get and ftp_put stop working?

Note: For LOCAL SEND, on the same network both commands continue to work, but when using another lan network it does not work. Application even connects, but cannot transfer files, not even for Download and Upload.

To receive the necessary files I managed to solve it as follows:

Code: Select all

$curl = curl_init($ftp_server.$ftp_porta);
$fo = fopen("$local_file", 'w');
 
curl_setopt($curl, CURLOPT_URL, "ftp://$ftp_server:$ftp_porta/$remote_file"); #input
curl_setopt($curl, CURLOPT_TRANSFERTEXT, TRUE);
curl_setopt($curl, CURLOPT_FILE, $fo); #output
curl_setopt($curl, CURLOPT_USERPWD, "$ftp_user_name:$ftp_user_pass");
curl_setopt($curl, CURLOPT_FRESH_CONNECT, true);
if(file_exists($localFile)) {
    curl_setopt($curl, CURLOPT_RESUME_FROM, filesize($local_File));
}
 
curl_exec($curl);
 
fclose($fo);
 
curl_close($curl);
But to send the files I'm not getting it, I changed the ftp_put to the ftp_raw as follows:

Code: Select all

$ftp = ftp_connect($servidor,$porta); // Retorno: true ou false
ftp_raw($ftp, "AUTH TLS");
ftp_raw($ftp, "AUTH SSL");
 
// Faz o login no servidor FTP
$login = ftp_login($ftp, $usuario, $senha); // Retorno: true ou false
//*****************************************************************//
ftp_raw($ftp, "CWD /backup");
ftp_raw($ftp, "TYPE A");
// Envia o arquivo pelo FTP em modo ASCII
$connect = ftp_raw($ftp, "PASV");
         
// parse the response and build the IP and port from the values
if (count($connect) > 0 && preg_match("/.*\((\d+),(\d+),(\d+),(\d+),(\d+),(\d+)\)/", $connect[0], $m)) {
    $address="{$m[1]}.{$m[2]}.{$m[3]}.{$m[4]}";
    $port=$m[5] * 256 + $m[6];
     
    print_r(ftp_raw($ftp, "STOR $ftp_arquivo"));
     
    $sock = socket_create(AF_INET, SOCK_STREAM, 0);
    if ($sock) {
        socket_connect($sock, $address, $port);
        socket_write($sock, file_get_contents($ftp_arquivo));
        socket_close($sock);
    }
}
ftp_raw($ftp,'QUIT'); 
That way I'm trying to transfer, it almost works, it executes all the processes, the message that is being transferred appears, but it gives an error according to the log below:

Code: Select all

(000036)11/08/2023 16:38:50 - mymobile (179.54.xxx.xxx)> PASV
(000036)11/08/2023 16:38:50 - mymobile (179.54.xxx.xxx)> 227 Entering Passive Mode (xxx,xxx,xxx,xxx,19,171)
(000036)11/08/2023 16:38:50 - mymobile (179.54.xxx.xxx)> STOR pedidos1013110823-10.xml
(000036)11/08/2023 16:39:00 - mymobile (179.54.xxx.xxx)> 425 Can't open data connection.
(000036)11/08/2023 16:39:00 - mymobile (179.54.xxx.xxx)> 425 Não é possível abrir conexão de dados.
The interesting thing is that, when transferring via FileZilla Client, there is no error, the file is transferred normally, as we can see in the log below.

Code: Select all

(000035)11/08/2023 16:37:36 - mymobile (179.54.xxx.xxx)> 200 Type set to A
(000035)11/08/2023 16:37:36 - mymobile (179.54.xxx.xxx)> PASV
(000035)11/08/2023 16:37:36 - mymobile (179.54.xxx.xxx)> 227 Entering Passive Mode (xxx,xxx,xxx,xxx,19,169)
(000035)11/08/2023 16:37:36 - mymobile (179.54.xxx.xxx)> STOR pedidos1013110823-10.xml
(000035)11/08/2023 16:37:36 - mymobile (179.54.xxx.xxx)> 150 Connection accepted
(000035)11/08/2023 16:37:36 - mymobile (179.54.xxx.xxx)> 226 Transfer OK
2nd question: What am I doing wrong? Is there another solution?

Regarding the error 425, I already released the ports on the modem and fileZilla
Port: 2121 on the modem and on fileZilla Passive port: 50000-50100 on the modem and Filezilla

User avatar
boco
Contributor
Posts: 26940
Joined: 2006-05-01 03:28
Location: Germany

Re: UPload - Transferência de arquivos PHP

#4 Post by boco » 2023-08-16 12:17

You said the ports 50000 - 50100 are forwarded/opened for Passive data transfers. However, in the logs, I see that they are NOT used/proposed.

Example:

Code: Select all

227 Entering Passive Mode (xxx,xxx,xxx,xxx,19,171)
The last numbers are the port proposed to the client to be used for the data connection, it calculates as follows:

Second-to-last number times 256 plus last number. In your case it would be 19 * 256 + 171 = 5035. This port is not in the 50000 - 50100 range. Could it be you made a mistake (forgot one 0) when entering them into FileZilla Server's Passive settings?
No support requests over PM! You will NOT get any reply!!!
FTP connection problems? Please read Network Configuration.
FileZilla connection test: https://filezilla-project.org/conntest.php
FileZilla Pro support: https://customerforum.fileZilla-project.org

User avatar
botg
Site Admin
Posts: 35566
Joined: 2004-02-23 20:49
First name: Tim
Last name: Kosse

Re: UPload - Transferência de arquivos PHP

#5 Post by botg » 2023-08-16 12:35

Which version of FileZilla Server are you using? It looks like you are using a very outdated version. Please update to the most recent version.

digitosistemas
504 Command not implemented
Posts: 6
Joined: 2023-08-15 02:22
First name: Dirceu
Last name: Morais

Re: UPload - Transferência de arquivos PHP

#6 Post by digitosistemas » 2023-08-16 20:24

You said the ports 50000 - 50100 are forwarded/opened for Passive data transfers. However, in the logs, I see that they are NOT used/proposed.
Thanks for the feedback. And that I got the old LOG and the current ports.
I go the current LOG with the current ports, they are:

Code: Select all

 227 Entering Passive Mode (192,168,1,21,195,86)
ports passive: 50000 - 50100 (image is attached).
Which version of FileZilla Server are you using? It looks like you are using a very outdated version. Please update to the most recent version.
FileZilla Server version 0.9.41 beta.

This is the version that is in xampp v3.2.2, I'll try to put the server on another computer to see if it solves it.




I'm still waiting for help.
Attachments
Prov.png
Prov.png (71.86 KiB) Viewed 5308 times

User avatar
boco
Contributor
Posts: 26940
Joined: 2006-05-01 03:28
Location: Germany

Re: UPload - Transferência de arquivos PHP

#7 Post by boco » 2023-08-16 22:25

The 0.9.x series is totally abandoned for years. Current version is 1.7.2.

Please note that we generally don't support any versions that come bundled with third-party packages. Please use the latest standalone version from here.
No support requests over PM! You will NOT get any reply!!!
FTP connection problems? Please read Network Configuration.
FileZilla connection test: https://filezilla-project.org/conntest.php
FileZilla Pro support: https://customerforum.fileZilla-project.org

digitosistemas
504 Command not implemented
Posts: 6
Joined: 2023-08-15 02:22
First name: Dirceu
Last name: Morais

Re: UPload - Transferência de arquivos PHP

#8 Post by digitosistemas » 2023-08-17 14:01

I uninstalled Xampp with the old version.
I installed version 1.7.2.

Now I'm not sure what's wrong as the file arrives at the server 0bytes.

See my code below:

Code: Select all

$ch = curl_init();
         $diretorioRemoto = '/backup/meuarquivo.txt';
         $localfile = '/pastenew/meuarquivo.txt';
         $remotefile = 'meuarquivo.txt';
         $fp = fopen($localfile, 'w');
              
         
         curl_setopt($ch, CURLOPT_URL, "ftp://$serverHost:$ftp_porta/$diretorioRemoto");
         curl_setopt($ch, CURLOPT_UPLOAD, 1);
         curl_setopt($ch, CURLOPT_INFILE, $fp);
         curl_setopt($ch, CURLOPT_INFILESIZE, filesize($fp));
         curl_setopt($ch, CURLOPT_USERPWD, "$serverUser:$serverPass");
         curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
         curl_setopt($ch, CURLOPT_RESUME_FROM, filesize($fp));
       
         curl_exec ($ch);
         $error_no = curl_errno($ch);
         curl_close ($ch);
         if ($error_no == 0) {
             $error = 'File uploaded succesfully.';
         } else {
             $error = 'File upload error.';
         }
         echo "<script language='JavaScript'>parent.alert('$error');</script>";
Note in the attached image that in the "Client" we have 13.5k and after the transmission in the "Server" it is 0k
Attachments
Prov.png
Prov.png (213.55 KiB) Viewed 5272 times
Prov.png
Prov.png (31.12 KiB) Viewed 5272 times

User avatar
botg
Site Admin
Posts: 35566
Joined: 2004-02-23 20:49
First name: Tim
Last name: Kosse

Re: UPload - Transferência de arquivos PHP

#9 Post by botg » 2023-08-17 17:15

curl_setopt($ch, CURLOPT_RESUME_FROM, filesize($fp));
Always resume uploads from the end of the source file? That by definition will get you nothing usable.

digitosistemas
504 Command not implemented
Posts: 6
Joined: 2023-08-15 02:22
First name: Dirceu
Last name: Morais

Re: UPload - Transferência de arquivos PHP

#10 Post by digitosistemas » 2023-08-17 23:31

I've tried going down the way it is below, but I can't travel data either:

Code: Select all

$ftp = ftp_connect($server,$port); // Retorno: true ou false
ftp_raw($ftp, "AUTH TLS");
ftp_raw($ftp, "AUTH SSL");
 
// Faz o login no servidor FTP
$login = ftp_login($ftp, $user, $password); // Retorno: true ou false
//*****************************************************************//
ftp_raw($ftp, "CWD /backup");
ftp_raw($ftp, "TYPE A");
$connect = ftp_raw($ftp, "PASV");
         
// parse the response and build the IP and port from the values
if (count($connect) > 0 && preg_match("/.*\((\d+),(\d+),(\d+),(\d+),(\d+),(\d+)\)/", $connect[0], $m)) {
    $address="{$m[1]}.{$m[2]}.{$m[3]}.{$m[4]}";
    $port=$m[5] * 256 + $m[6];
     
    ftp_raw($ftp, "STOR $ftp_arquivo");
     
    $sock = socket_create(AF_INET, SOCK_STREAM, 0);
    if ($sock) {
        socket_connect($sock, $address, $port);
        socket_write($sock, file_get_contents($ftp_arquivo));
        socket_close($sock);
    }
}
ftp_raw($ftp,'QUIT');

this way too

Code: Select all

$ftp = ftp_connect($server,$port); // Retorno: true ou false
ftp_raw($ftp, "AUTH TLS");
ftp_raw($ftp, "AUTH SSL");
$login = ftp_login($ftp, $user, $password); // Retorno: true ou false
ftp_raw($ftp, "CWD /backup");
ftp_raw($ftp, "TYPE A");
$connect = ftp_raw($ftp, "PASV"); 
ftp_raw($ftp, "STOR $ftp_arquivo");
ftp_raw($ftp,'QUIT');

User avatar
botg
Site Admin
Posts: 35566
Joined: 2004-02-23 20:49
First name: Tim
Last name: Kosse

Re: UPload - Transferência de arquivos PHP

#11 Post by botg » 2023-08-18 07:09

ftp_raw($ftp, "AUTH TLS");
ftp_raw($ftp, "AUTH SSL");
You need to check the response code and depending of the response, do the TLS handshake.
$address="{$m[1]}.{$m[2]}.{$m[3]}.{$m[4]}";
What do the curly braces do?

digitosistemas
504 Command not implemented
Posts: 6
Joined: 2023-08-15 02:22
First name: Dirceu
Last name: Morais

Re: UPload - Transferência de arquivos PHP

#12 Post by digitosistemas » 2023-08-25 14:20

What do the curly braces do?
is that it was "shooting" in all directions.
Everything I found on the internet I was putting to see if it worked.


Thank God I managed to solve the problem. Thanks [*]botg, [*]boco and to everyone else who read my topic (poorly translated, but I think we understand each other) and in a way thought to help me.[/b]

Well, let's go to the solution so you can help others who go through the same problem as me. I added the @ before the ftp_put and oddly enough that's all I needed to do. It stayed that way.

Code: Select all

<?php
// Dados do servidor = server data
$port = '21';
$host ='200.200.100.50'; 
$user ='dirceu';
$password='morais';

// Opens the connection to the FTP server
$ftp = ftp_connect($host, $port); 
if (!$ftp) {
   Echo "Unable to connect to the server";
   Exit;
}

// Login to the FTP server
$login = ftp_login($ftp, $user, $password); 
If (!$login) {
    Echo "unable to login to server";
    ftp_close($ftp);
    Exit;
}
// ======

// Define variables for file upload
$local_arquivo = '/storage/emulated/legacy/htdocs/pdvmobile/envia/meuarquivo.txt'; // Localização (local)
$ftp_pasta = '/backup/'; // Pasta (externa)
$ftp_arquivo = 'meudocumento.txt'; // Name file (externo)
if(file_exists($local_arquivo)) {
    Echo "Not file  \n\n";
} Else
    Echo "File  $local_arquivo found";

$tempHandle = fopen($local_arquivo, 'wb');
ftp_pasv($ftp,true);

// Send the file via FTP in ASCII mode
$envia = @ftp_put($ftp, $ftp_pasta.$ftp_arquivo, $local_arquivo, FTP_ASCII, 0); 

//Before I was using it the way it is commented below. without @. Note the small difference
//$envia = ftp_put($ftp, $ftp_pasta.$ftp_arquivo, $local_arquivo, FTP_ASCII, 0); 
// Close Connect Ftp
ftp_close($ftp);

if (!$envia) {
    Echo "I couldn't upload the file";
} Else
    echo 'File uploaded successfully!';
?>

Post Reply