Problem while extracting zip file - Ziparchive

Asked

Viewed 55 times

1

I created a script to generate one (Backup/Restore) files on my system, it works perfectly on "location-windows" both the backup and the restore, however when going up in "hosting-linux" happens the following.

  1. First i Gero the local backup and go up on the hosting.
  2. in the hosting I restore this backup.

Only that it takes the address of the files and generates only one getting like this:

inserir a descrição da imagem aqui

instead of creating the structures it merges everything into one, to be even more specific see in line 3 in the image, it was to create the application | Configuration | functions-groups folders and then the Admin.php file but joined everything.

Obs: if in the hosting I generate the backup and restore it works perfectly, if I take a zip file any generated "local" and rise also works in the hosting.

I don’t know if this is the case but the accommodation is hostgator and php version on site/hosting is 7.3

index php.

/*
Estrutura da pasta

raiz_do_site
-meusite.com
--systemRoot
---index.php
*/

define('INFO', array(
    //backup nome que vai ser gerado
    'backupName' => 'BACKUP', 

    //backup nome que vai após ser restaurado
    'restoreName' => 'RESTORED', 

    //nome da pasta para onde o arquivo será enviado após gerado o backup
    'destDir' => 'BackupFiles', 
));
define('DS', DIRECTORY_SEPARATOR);

function initBackup($source_path, $dir_skip = array()){

    $source_path = realpath($source_path);
    $backupName = INFO['backupName'].'_'.date('d_m_Y_-_H_i_s');

    if (!is_dir(__DIR__.DS.INFO['destDir'])) {
        mkdir(__DIR__.DS.INFO['destDir'], 0755);
    }

    $zip_file = __DIR__.DS.INFO['destDir'].DS.$backupName.'.zip';

    $zip = new ZipArchive();
    $zip->open($zip_file, ZipArchive::CREATE | ZipArchive::OVERWRITE);

    $files = new RecursiveIteratorIterator(
        new RecursiveDirectoryIterator($source_path),
        RecursiveIteratorIterator::LEAVES_ONLY
    );

    foreach ($files as $name => $file) {

       if (!$file->isDir()) {

        $file_path = $file->getRealPath();
        $file_view = explode('\\', $file_path);

        if(!array_intersect($dir_skip, $file_view)){
         $relative_path = substr($file_path, strlen($source_path) + 1);
         $zip->addFile($file_path, $relative_path);
     }
 }
}

$zip->close();
return $zip_file;
}

function initRestore($backupFile){

    $dir = __DIR__.DS.INFO['destDir'].DS;

    if (file_exists($dir.$backupFile)) {
        $path = pathinfo(realpath($dir.$backupFile), PATHINFO_DIRNAME);
        $file_restore_name = INFO['restoreName'].'_'.date('d_m_Y__H_i_s').'.zip';

        $zip = new ZipArchive;
        $res = $zip->open($dir.$backupFile);
        if ($res === TRUE) {
            $zip->extractTo('../');
            $zip->close();
        }
        rename($dir.$backupFile, $dir.$file_restore_name);

        $msg = 'Arquivo restaurado com sucesso';
    }else{
        $msg = 'Arquivo não encontrado';
    }

    echo $msg;
}
/*
|--------------------------------------------------------------------------
| GERA O BACKUP - initBackup($source_path, $dir_skip)
|--------------------------------------------------------------------------
|   $source_path = informe o diretório no qual o backup será realizado
|   $dir_skip = informe um array contendo as pasta que o backup não irá zipar
|
*/
 //initBackup('../', array('pasta1', 'pasta2','arquivo1.php','arquivo2.php'));

/*
|--------------------------------------------------------------------------
| RESTAURA O BACKUP - initRestore($backupFile, $location)
|--------------------------------------------------------------------------
|   $backupFile = informe o backup que será restaurado
|   $location = informe o diretório que será restaurado
|
*/
//initRestore('BACKUP_29_10_2019_-_22_31_28.zip');
  • It seems to me that it is a problem of how Windows and Linux treat the paths(path). On Windows with "" and on Linux with "/".

  • hard is that I don’t know where I can change this, with DIRECTORY_SEPARATOR would solve but where?

  • You would have to check if you are in windows or linux environment and change the tab in the restore. Another option is to back up to windows and linux.

No answers

Browser other questions tagged

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