I solved the problem with class FTP
of codeigniter
which is the framework I'm using.
I set the second connection of the database to my home and soon after doing the insertion, editing or delete the data, php saves the file in the hosting in a temporary folder and I execute the following script:
$caminho_temp= "Arquivos/TEMP/DE/{$this->input->post('numero')}_{$this->input->post('ano')}{$arquivo['file_ext']}";
$source = $caminho_temp;
$this->load->library('ftp');
//FTP configuration
$ftp_config['hostname'] = '###.150.##.123';
$ftp_config['username'] = '@@@@@@';
$ftp_config['password'] = '@@@@@@';
$ftp_config['debug'] = FALSE;
//Connect to the remote server
if($this->ftp->connect($ftp_config) == TRUE){
//File upload path of remote server
$destino = "/Arquivos/DE/{$fileName}{$arquivo['file_ext']}";
$destino_url = "Arquivos/DE/{$fileName}{$arquivo['file_ext']}";
//Upload file to the remote server
$this->ftp->upload($source, ".".$destino);
//Close FTP connection
$this->ftp->close();
@unlink($source);
}else{
$this->session->set_flashdata('error','Problema de conexão com o servidor de documentos.');
@unlink($source);
redirect(base_url()."admin/atos/de/editar/".$this->input->post('id'));
}
After uploading via FTP I delete the file from the temporary folder in the hosting or if the upload failed, I also delete it.
Process can get a bit slow for files larger than 5mb.