0% found this document useful (0 votes)
8 views

UNZIP FILE SA PHPom

UNZIP FILE SA PHPom

Uploaded by

Ante Maric
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

UNZIP FILE SA PHPom

UNZIP FILE SA PHPom

Uploaded by

Ante Maric
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Unzip a file with php

I want to unzip a file and this works fine

<?php
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.

<?php
$master = $HTTP_GET_VARS["master"];
system('unzip $master.zip');
?>
up vote 24 down vote
favorite What am I missing? I know it has to be something small and stupid I am overlooking.
11
Thank you,

php unzip
asked Jan 17 '12 at 2:38

share|improve this question

BostonBB
168118
1
Double quotes evaluate variables; single quotes do not. BUT -- be careful, as simply passing some input to a system call could be
11
quite dangerous. – Wiseguy Jan 17 '12 at 2:40
You realize there are PHP libraries that handle unzipping, right? – coreyward Jan 17 '12 at 2:43
We have 2012. Why do you use $HTTP_*_VARS instead of the corresponding $_* superglobal arrays? – ThiefMaster♦ Jan 17 '12
2
at 2:55
add comment

3 Answers
active oldest votes
I can only assume your code came from a tutorial somewhere online? In that case, good job trying to figure it out by yourself. On the other
hand, the fact that this code could actually be published online somewhere as the correct way to unzip a file is a bit frightening.

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

<?php
$zip = new ZipArchive;
$res = $zip->open('file.zip');
up vote 73 if ($res === TRUE) {
$zip->extractTo('/myzips/extract_path/');
down vote $zip->close();
accepted echo 'woot!';
} else {
echo 'doh!';
}
?>

Also, as others have commented, $HTTP_GET_VARS has been deprecated since version 4.1 ... which was a reeeeeally 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.
2
ALWAYS SANITIZE USER INPUT.

UPDATE

As per your comment, the best way to extract the zip file into the same directory in which it resides is to determine the hard path to the file and
extract it specifically to that location. So, you could do:

// assuming file.zip is in the same directory as the executing script.


$file = 'file.zip';

// get the absolute path to $file


$path = pathinfo(realpath($file), PATHINFO_DIRNAME);

$zip = new ZipArchive;


$res = $zip->open($file);
if ($res === TRUE) {
// extract it to the path we determined above
$zip->extractTo($path);
$zip->close();
echo "WOOT! $file extracted to $path";
} else {
echo "Doh! I couldn't open $file";
}
answered Jan 17 '12 at 2:54

edited Aug 10 '12 at 20:10


share|improve this answer

rdlowrey
12.7k42447
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
3
the zipped file is in? – BostonBB Jan 17 '12 at 3:06
Well, there is a difference between the current working directory of your script and the directory where the zip file resides. If the zip file
1 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'll update my answer to demonstrate. – rdlowrey Jan 17 '12 at 3:40
What if you don't have the ZipArchive class available? I'm working on a site with crap hosting and I get the Fatal error: Class
'ZipArchive' not found error which I try this script :-( Is there any option at that point? – CWSpear Aug 10 '12 at 17:47
@CWSpear you are going to need the underlying zlib library to perform pretty much any compress/decompress operations with PHP.
Even to make system calls you'd need to have the underlying library. This is a very ubiquitous thing though, and not having it is the
exception. If you're on shared hosting, they should install it for you. Otherwise, just google something like "How to Install Zip Functions
Support For PHP" – rdlowrey Aug 10 '12 at 20:14
Yeah, it's pretty cheap hosting as I've been told. Anyway, thanks for the reply, it's good stuff to know. – CWSpear Aug 10 '12 at 23:34
add comment

Please, don't do it like that (passing GET var to be a part of a system call). Use ZipArchive instead.

So, your code should look like:

$zipArchive = new ZipArchive();


up vote 10 down $result = $zipArchive->open($_GET["master"]);
vote if ($result === TRUE) {
$zipArchive ->extractTo("my_dir");
$zipArchive ->close();
// Do something else on success
} else {
// Do something on error
}

4
And to answer your question, your error is 'something $var something else' should be "something $var something else" (in double
quotes).

answered Jan 17 '12 at 2:45

share|improve this answer

Aleksandar Vučetić
6,01532231
+1 Oops, sorry for tacking on basically the same answer five minutes later. Didn't see you there :) – rdlowrey Jan 17 '12 at 2:57
add comment
Try this blog it have some working codes http://scriptime.blogspot.in/2013/01/unzip-zip-file-with-php.html

answered Jan 17 at 8:51

up vote -2 down vote


share|improve this answer

midhun pottmmal
202
add comment

Your Answer




5










Sign up or login

Sign up using Google

Sign up using Facebook


6
Sign up using Stack Exchange

Post as a guest

Name
Email

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged php unzip or ask your own
question.
tagged

php × 481673
unzip × 626
asked 1 year ago
viewed 40078 times
active 10 months ago

 Senior Software Engineer - Help Shape our Product!


ACE Portal New York, NY / relocation
 Quality Assurance Analyst
Morningstar Chicago, IL / relocation
 Senior Software Developer for Derivatives Team
Bloomberg New York, NY

Linked
7
3
PHP Unzip very large file

0
Unzip .zip file uploaded from Android via PHP to IIS 7

0
Is there a (PHP) shortcut to zip all files in a folder (perform the reverse of the ZipArchive extractTo operation)?

0
How to get original data from base64Binary encoded data in PHP?

0
linux: Convert spaces to underscores in filenames when unzipping the archive

0
Download repo from GitHub in PHP?

0
How to upload and unzip files via php and a url

Related

4
Unzipping larger files with PHP

1
How to unzip a file in the previous directory using PHP

1
8
php - download zip file with curl and unzip it?

3
php unzipping huge files

0
Unzip .zip file uploaded from Android via PHP to IIS 7

-2
Is there any on server unzip lib for PHP?

0
Python - Error message while unzipping a file

0
Improve my Unzip & Move function - PHP

0
PHP Unzip function creating hidden system files

0
php unzip remote file
question feed
about help badges blog chat data legal privacy policy jobs advertising info mobile contact us feedback
Technology Life / Arts Culture / Recreation Science Other
1. Stack 1. Programmers 1. Database 1. Photography 1. English 1. Mathematics 1. Stack
Overflow 2. Unix & Linux Administrators 2. Science Fiction Language & 2. Cross Validated Apps
2. Server Fault 3. Ask Different 2. Drupal & Fantasy Usage (stats) 2. Meta
3. Super User (Apple) Answers 3. Seasoned 2. Skeptics 3. Theoretical Stack
4. Web 4. WordPress 3. SharePoint Advice 3. Mi Yodeya Computer Overflo
Applications Answers 4. User (cooking) (Judaism) Science w

9
5. Ask Ubuntu 5. Geographic Experience 4. Home 4. Travel 4. Physics 3. Area 51
6. Webmasters Information 5. Mathematica Improvement 5. Christianity 5. MathOverflow 4. Stack
7. Game Systems 6. more (14) 5. more (13) 6. Arqade 6. more (7) Overflo
Development 6. Electrical (gaming) w
8. TeX - LaTeX Engineering 7. Bicycles Careers
7. Android 8. Role-playing
Enthusiasts Games
8. Information 9. more (21)
Security

site design / logo © 2013 stack exchange inc; user contributions licensed under cc-wiki with attribution required
rev 2013.11.30.1184

10

You might also like