Save data from a web form in .txt on ftp server

0

I'm having trouble saving my data to a .txt on my FTP server.

Locally I got it quiet. Below is the code I am using locally:

$arquivo = 'msg.txt';     
$criar = fopen($arquivo, "a+"); 
$conteudo = "$nome; $idade; $sexo; $telefone;".PHP_EOL ; 
$escrever = fwrite($criar, $conteudo);

But I need to write to a .txt that is on my FTP server.

I have already seen that indicating the path of the file with HTTP will not work, but I did not find a solution of how I should link to the FTP server.

The code I am using to try to make online communication with .txt is the same, it only changes one line, which is the file path.

$arquivo = 'www.domain.net/pasta/arquivo.txt'; 

The following error message is displayed (with HTTP):

failed to open stream: HTTP wrapper does not support writeable connections
    
asked by anonymous 06.02.2018 / 14:52

1 answer

1

If it is from a form try this:

<?php
if( $_SERVER['REQUEST_METHOD']=='POST' )
{
        var_dump( $_FILES );//apenas para debug

        $servidor = 'host';
        $caminho_absoluto = '/httpdocs/uploads/'; //diretorio do FTP que pode variar dependendo da hospedagem
        $arquivo = $_FILES['arquivo'];

        $con_id = ftp_connect($servidor) or die( 'Não conectou em: '.$servidor );
        ftp_login( $con_id, 'usuario', 'senha' );

        ftp_put( $con_id, $caminho_absoluto.$arquivo['name'], $arquivo['tmp_name'], FTP_BINARY );
}
?>
        <form action="" method="post" enctype="multipart/form-data">
                <input type="file" name="arquivo" />
                <input type="submit" name="enviar" value="Enviar" />
        </form>

This example is to upload the file, but you can make changes to the form and PHP to be a writer in TEXTAREA

Now if you want to upload / transfer a file from your hosting directly to the server:

<?php
$file = 'somefile.txt'; //arquivo de origem na hospedagem
$remote_file = 'readme.txt'; //nome do arquivo de destino, nome gerado ao enviar para FTP

$conn_id = ftp_connect("host ftp");
$login_result = ftp_login($conn_id, "usuario", "senha");

// Enviar arquivo
if (ftp_put($conn_id, $remote_file, $file, FTP_ASCII)) {
 echo "successfully uploaded $file\n";
} else {
 echo "Houve um problema ao enviar o arquivo <i><u>$file</u></i>\n";
}

// Encerra acesso ao FTP
ftp_close($conn_id);
?>

If you want to delete the file generated in the hosting after sending the copy to FTP use this command:

unlink($file);
    
06.02.2018 / 15:39