Automatic remote upload of zipped file in PHP

Asked

Viewed 915 times

3

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

I’d like to automate that. One solution that I thought was to go up in a single location a ZIP file with the updates, and put in the system of each store the "Update" function. Basically I would get the file zipped with the updates on my server, unzip on the client server, overwriting the old files.

The part where I want help is:

  • No remote access for file copy (if necessary)
  • Unzip the same in a way that overwrites the old files, without losing the attributes of the same (755 for folder and 644 for files)

Any suggestions?

Grateful from now on!

1 answer

5


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

This code when called for execution will have to access the remote machine, download the file with the updates and uncompress it.

Accessing the remote host

To access the remote machine, we can use Curl that 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 file subscription

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

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

Manpage to the UNZIP (English)


Solution

We can then have a PHP on each client’s side that runs at 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 above code to be used, it must be installed in each client:

  • PHP Client URL Library (Curl)
  • unzip
  • 1

    Thank you so much! !

  • @Szag-Ot Always happy to help :)

  • Partner, a problem: I can only run the unzip command via PHP exec if the folders are 777 enabled. This is not good. How do I run the command with the root user (I have the password) or give the user full power to write the files?

  • @Szag-Ot If you want to run the command with high privileges, it is ideal to establish the connection with authentication, making use of user access beliefs with the necessary privileges. I really don’t recommend anything that uses PHP exec() with parameters to raise script permissions to run, it is a serious security problem.

  • Agree, but leave the folders with 777 permission as well. ?

  • @Szag-Ot You don’t need to have folders with 777, you need the user who will handle them to be the same who created them or is a user of the same group.

  • The user is from the same group/owner. Still exec returns an error unless folders and files are in 777... then it allows the change

  • I see what the problem is. With "ls -l" you can see that the files created by php are being saved with Owner "nobody", and not with Owner "tmwe" which is the owners of the test account. Do you know how to configure the Owner on which PHP runs? The Platform is WHM/Cpanel.

  • See in detail the problem here: http://answall.com/questions/23734/servor-configured-comodso-qual-a-bestalternativa-para-permiss%C3%B5es-de-past

  • 1

    @Szag-Ot Apache runs with nobody by default, you can however access /etc/apache2/httpd.conf and change the User and Group for what you want. You need to do re-start after the amendment. You should also update the Ownership files and folders so that everything is in order.

  • But in this case I would have a problem, since on my dedicated server I have several accounts (one for each client). This solution would solve for only 1 of these accounts, but not for the others that have different owners

  • @Szag-Ot Once you have opened a question to deal with this problem (and you have done well), I will elaborate a more detailed answer with a solution to your case.

Show 7 more comments

Browser other questions tagged

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