Save data from a web form to . txt on the ftp server

Asked

Viewed 751 times

0

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

Locally managed 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 saw that indicating the path of the file with HTTP will not work, but I did not find a solution how to link to the FTP server.

The code I’m using to try to communicate online with . txt is the same, 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

1 answer

1


If you’re on 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>

In 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);
  • I didn’t understand it very well. I guess I didn’t know how to define what I need. Come on! Need to save data from a web form to a file . txt on my FTP server. For example, on the page of my site when the user sends the information, the information should be entered in . txt so that later I can import to Excel. I don’t own BD, so the use of . txt, and because it is easy to manipulate this data in Excel. From what I understand, the example you gave me is to upload right? Actually I need to write in txt file when user click to send :/

  • But in the second example you do this. Generates the file in the hosting and then sends. Unless you do not know how to make a form.

  • I got it, thank you very much !

  • I forgot to present something interesting, but it is optional, if you want to delete the file generated in the hosting after already having the copy in FTP use unlink() - I edited the answer for you to analyze

Browser other questions tagged

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