Send file to another server using Codeigniter

Asked

Viewed 233 times

0

I have a Web System that I’ll switch to a hosting. But I have the system with 2 banks (one for the site and the other is in my house for the path of the files, documents, logs, etc), the script will be in the hosting and the path of the files on the server in my house. The problem and the following, for security and space consumption reasons I can not send these files to the hosting. Then the user will send the document through the form, PHP connects with the database in my home and saves the document data and sends the file to my home.

How can I do that?

I’ve been thinking of sending via Ajax request to my local server and passing the return to error validation. What do you think?

  • Just one thing. If you think the files are safer in your home, you have a lot to learn about security. A hosting service will have a whole professional team taking care of their network security, while on your personal PC you just commit a slip that you are chipped. And by swipe I mean contracting a virus that does something nasty like Wannacry, leaving the shared root directory on the network by mistake etc. Besides, if your PC breaks, it’s gone. In hosting you will get at least cloud replication with daily backups.

  • My friend, yes. At the moment of development I am doing so before putting into production. More where I will implement has a server backed up 4 times a day. Hosting and only to keep the news site and services running. As around 3000 documents a year have no way to stay in the accommodation.

1 answer

1

I solved the problem with the class FTP of codeigniter which is the framework I’m using. I set the second connection of the database to my home and right after inserting, editing or deleting the data, php saves the file in the hosting in a temporary folder and I run 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 sending by FTP I delete the file from the temporary folder in the hosting or in case of failed sending also delete it.

Process can get a little slow for files larger than 5mb.

Browser other questions tagged

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