Automatic remote upload of zipped file in PHP

3

I have a PHP e-commerce system installed for several clients. However, every time I upgrade to the system, I have to manually upload the new files to all the clients.

I'd like to automate this. One solution I thought was to upload a ZIP file with the updates in one place, and put the "Refresh" function in each store's system. I would basically get the zipped file with the updates on my server, unzip the client server, overlay the old files.

The part I want help with is:

  • In remote access to copy the file (if necessary)
  • Unpacking it in a way that overlays the old files, without losing their attributes (755 for folder and 644 for files)

Any suggestions?

Thank you in advance!

    
asked by anonymous 01.07.2014 / 23:09

1 answer

5

The solution is to have on the clients side a file whose code is called when the client clicks a "Refresh" button.

This code when called to run will have to access the remote machine, download the file with updates and decompress it.

Access the remote machine

To access the remote machine, we can use cURL which, through the function below, we can use authentication or not depending on your case:

/**
 * cURL - Recolher ficheiro remoto
 *
 * Função recorrendo a cURL para recolher um ficheiro
 * numa máquina remota com ou sem autenticação.
 *
 * @param string $remotePath      Caminho remoto incluindo ficheiro
 * @param string $localPath       Caminho local incluindo ficheiro
 * @param array $access           Matriz com credenciais de acesso (usr;pwd)
 * @param boolean $overwrite      Se deve subscrever o ficheiro existente
 *
 * @return boolean                Estado da operação
 */
function recolherFicheiroRemotoCurl($remotePath, $localPath, $access = null, $overwrite = false) {

  // devolve TRUE se o ficheiro local existe e não é para subscrever
  if (is_file($remotePath) && !$overwrite) {

    return TRUE;
  }
  else {

    $curl_handle = curl_init();
    curl_setopt($curl_handle, CURLOPT_HEADER, 0);
    curl_setopt($curl_handle, CURLOPT_URL, $remotePath);
    curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 30);
    curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
    if ($access) {
      curl_setopt($curl_handle, CURLOPT_USERPWD, $access["username"].":".$access["password"]);
    }
    curl_setopt($curl_handle, CURLOPT_SSL_VERIFYPEER, false);
    $buffer = curl_exec($curl_handle);
    curl_close($curl_handle);

    if ($buffer != false) {
      file_put_contents($localPath, $buffer);
      return TRUE;
    }
  }

  return TRUE;
}

PHP cURL (English)

Unzip with files subscription

To run unzip by subscribing existing files, just pass the parameter -o :

exec ('unzip -o ' . $ficheiro);

Manpage for UNZIP (English)

Solution

We can then have a PHP on the side of each client that runs on the client's request to update the files:

<?php

// Onde fica
$localPath = "caminho/local/do/ficheiro.zip";

// Onde está
$remotePath = "caminho/remoto/do/ficheiro.zip";

// Ir buscar
if (recolherFicheiroRemotoCurl($remotePath, $localPath) {

  // Se ficheiro local existe
  if (is_file($localPath)) {

    // Correr unzip
    exec('unzip -o ' . $localPath, $output, $return_value);

   // podes aceder à variável $output para ver o output da execução
   // podes aceder à variável $return_value para ver o código devolvido
  }
}

?>

Notes:

In order for the code above to be used, it must be installed on each client:

  • PHP Client URL Library (cURL)
  • unzip
02.07.2014 / 01:12