How to extract or uncompress .tar.gz file using PHP?

Can anyone help me extract a .tar.gz file with PHP? I am using free web hosting, they don’t have an extract option in their control panel. They also don’t allow exec() commands.

I have a script in .tar.gz file around 80MB file size. I uploaded the file to the hosting server with FTP. After that, I need to extract it to run the script but found no way to do it.

Is any option available in PHP to extract .tar.gz?

You can use PharData class to extract the tar archive. In 2 steps you can extract the file.

  1. You need to decompress the gz
  2. Then extract the tar
try {

    $file_path = "/path/to/myfile";

    // decompress
    $p = new PharData("{$file_path}.tar.gz");
    $p->decompress(); // creates /path/to/my.tar

    // extract
    $phar = new PharData("{$file_path}.tar");

} catch (Exception $e) {
    // handle errors