How to unzip a file with php

0 votes

I want to unzip a file and this works fine

system('unzip File.zip');

But I need to pass in the file name through the URL and can not get it to work, this is what I have.

$master = $_GET["master"];
system('unzip $master.zip'); 

What am I missing? I know it has to be something small and stupid I am overlooking.

Thank you,

Sep 29, 2020 in Laravel by kartik
• 37,510 points
183 views

1 answer to this question.

0 votes

Hello @kartik.

PHP has built-in extensions for dealing with compressed files. There should be no need to use system calls for this. ZipArchive is one option.

$zip = new ZipArchive;
$res = $zip->open('file.zip');
if ($res === TRUE) {
  $zip->extractTo('/myzips/extract_path/');
  $zip->close();
  echo 'woot!';
} else {
  echo 'doh!';
}

Also, as others have commented, $HTTP_GET_VARS has been deprecated since version 4.1 which was a really long time ago. Don't use it. Use the $_GET superglobal instead.

Finally, be very careful about accepting whatever input is passed to a script via a $_GET variable.

Hope it helps!!
Thank you!!

answered Sep 29, 2020 by Niroj
• 82,720 points
Have you tried this and got successful?

Hello,

Yes, it is a working solution. You can also give a try to make things clear to you

Thank you for the advise. I am new to this and just figuring everything out. With your code, how can I get it to unzip in the same folder the zipped file is in?

There is a difference between the current working directory of your script and the directory where the zip file resides. If the zip file is in the same directory as the script you could do $zip->extractTo('./'); However, this is likely not the case. A better option is to determine the zip file's location in the filesystem and extract it there

I used yourDestinationDir as session_save_path() . DIRECTORY_SEPARATOR . "$name" . DIRECTORY_SEPARATOR where $name is target folder to unzip in session folder. can i used it?

You should be careful when dealing with system or exec functions which contains variables ! It can litteraly give a free command line from your server to a hacker

Related Questions In Laravel

+1 vote
1 answer

How to load blade or php content into a view via ajax/jquery in laravel?

Hello @kartik, Assuming you're using jQuery... create a route ...READ MORE

answered Apr 14, 2020 in Laravel by Niroj
• 82,720 points
10,930 views
0 votes
1 answer

How to make Django serve that file for download as opposed to trying to find a URL and View to display it?

Hello @kartik, You can just use the built ...READ MORE

answered Jul 30, 2020 in Laravel by Niroj
• 82,720 points
3,217 views
0 votes
1 answer

How to populating a database in a Laravel migration file?

Hello @kartik, Don't put the DB::insert() inside of ...READ MORE

answered Aug 4, 2020 in Laravel by Niroj
• 82,720 points
693 views
0 votes
1 answer

How to get current path of a Request with its query parameters?

Hello @kartik, Try to use the following: \Request::getRequestUri() Hope this ...READ MORE

answered Aug 11, 2020 in Laravel by Niroj
• 82,720 points
183 views
0 votes
1 answer

jQuery AJAX fires error callback on window unload - how do I filter out unload and only catch real errors?

Hello, In the error callback or $.ajax you have three ...READ MORE

answered Apr 27, 2020 in Java-Script by Niroj
• 82,720 points
892 views
0 votes
1 answer

How do I pass command line arguments to a Node.js program?

Hello @kartik, If your script is called myScript.js ...READ MORE

answered May 5, 2020 in Java-Script by Niroj
• 82,720 points
1,337 views
0 votes
1 answer

Error:Issue when trying to use IN() in wordpress database

Hello @kartik, Try this code : // Create an ...READ MORE

answered May 8, 2020 in PHP by Niroj
• 82,720 points
167 views
+2 votes
1 answer

How do I debug Node.js applications?

Hello @kartik, Use node-inspector  from any browser supporting WebSocket. Breakpoints, ...READ MORE

answered Jul 8, 2020 in Node-js by Niroj
• 82,720 points
171 views
0 votes
1 answer

How to make a new page with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,720 points
1,730 views
0 votes
1 answer

How Can I Set the Default Value of a Timestamp Column to the Current Timestamp with Laravel Migrations?

Hello, To create both of the created_at and updated_at columns: $t->timestamp('created_at')->default(DB::raw('CURRENT_TIMESTAMP')); $t->timestamp('updated_at')->default(DB::raw('CURRENT_TIMESTAMP on update ...READ MORE

answered Apr 2, 2020 in Laravel by Niroj
• 82,720 points
6,333 views