How to unzip a folder in PHP?

Asked

Viewed 1,534 times

0

I have a form where an administrator will fill in the user data, but I need this administrator to enter a folder. zip where will take user photos.

I am using a button to select the file . zip , but I need to unzip this file to later use what is inside it.

<form action='pageinicial.php' method='GET' enctype='multipart/form-data'>
    <div class='diview'>
    Certifique-se de preencher os campos login, senha, nome e email!
    <table>
        <thead>
        <tr>
         <th >login</th>
          <th >senha</th>
           <th >nome</th>
           <th >departamento</th>
            <th >nivel</th>      
             <th >email</th> 
             </tr>
        </thead>
        <tbody>   
        <tr> 
           <td id='class_td'><input type='text' name='loginusu' value=''></td>
          <td id='class_td'><input type='text' name='senhausu' value=''></td>
           <td id='class_td'><input type='text' name='nomeusu' value=''></td>
           <td id='class_td'><input type='text'  name ='departamentousu' value=''></td>
           <td id='class_td'><input type='text' name='nivelusu' value=''></td>
            <td id='class_td'><input type='email' name='emailusu' value=''></td>
        </tr>        
        </tbody>
    </table>
    </div>
    <input type='submit' value='Adicionar'/>
<input type='file' name='foto' /><br /><br />
  • http://php.net/manual/en/ziparchive.extractto.php

  • Has some code?

  • Your form is method=GET, uploads cannot be sent via GET.

  • Guilherme Nascimento I use $_GET because the administrator is already logged in to the system, for application itself does not cause problems, because this screen is only accessible after logging into the system

1 answer

1


<?php
 $arquivo = getcwd().'/arquivo-teste.zip';
 $destino = getcwd().'/';

 $zip = new ZipArchive;
  $zip->open($arquivo);
   if($zip->extractTo($destino) == TRUE)
   {
    echo 'Arquivo descompactado com sucesso.';
   }
   else
   {
    echo 'O Arquivo não pode ser descompactado.';
   }
  $zip->close();
?>

Or direct:

<?php
$zip = new ZipArchive;
if ($zip->open('test.zip') === TRUE) {
    $zip->extractTo('/my/destination/dir/');
    $zip->close();
    echo 'ok';
} else {
    echo 'failed';
}
?>

Documentación Oficial

Sources:

Compact

Unpack

Browser other questions tagged

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