Unzip a file with php
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. ZipArchive
docs 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 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.
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";
}
Can't unzip file with php
I have found the problem.
The code is fine, but the hosting service is not, and they do not have the ZIP extension available right now
How to unzip a zip folder using php code
<?php
$zip = zip_open("zip.zip");
if ($zip) {
while ($zip_entry = zip_read($zip)) {
$fp = fopen("zip/".zip_entry_name($zip_entry), "w");
if (zip_entry_open($zip, $zip_entry, "r")) {
$buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
fwrite($fp,"$buf");
zip_entry_close($zip_entry);
fclose($fp);
}
}
zip_close($zip);
}
?>
execute php page that unzip a file (manipulate files)
By the way all the php line codes that were manipulating files wasen't taking affect. I solved this by allowing the Apache user (apache
in rhel) to create files in the directory. This can be done by making Apache the owner of the directory running this cmd:
sudo chown apache /var/www/html/
PHP Unzip File and Extract Files On By One
Finally, from this link I read, so I choose to extract the file with array method. Here's my end code for my problem:
for ($i=0; $i<$zip->numFiles;$i++)
{
$current = $zip->statIndex($i);
if($current["size"] > (1*1024*1024))
{
printf("%s (size: %d bytes) is too big, failed to upload this image!<br>", $current["name"], $current["size"]);
}
else
{
$location = $folder.'/'.$folder2.'/';
if($zip->extractTo($location , array($current['name'])))
printf("%s successfully uploaded<br>", $current["name"]);
else
printf("Failed <br />");
}
}
Extract .zip files using PHP
If you have zziplib installed, you can use this code:
<?php
$zip = new ZipArchive;
if ($zip->open('test.zip') === TRUE) {
$zip->extractTo('/my/destination/dir/');
$zip->close();
echo 'ok';
} else {
echo 'failed';
}
?>
Make sure the user executing php (usually nobody, apache or httpd) have writing privileges on destination dir.
PHP Unzip very large file
Thanks for the suggestions everyone. I ended up modifying the code in this question to unzip the files.
Unzip a file with php
Related Topics
How to Perform Static Code Analysis in PHP
How to Make an Asynchronous Get Request in PHP
Remove All HTML Tags from PHP String
Split String by New Line Characters
PHP Multidimensional Array Searching (Find Key by Specific Value)
How to Add Http:// If It Doesn't Exist in the Url
Check File Extension in Upload Form in PHP
How to Embed PHP Code in JavaScript
How to Get Directory Size in PHP
How to Retract a Salted Password from the Database and Auth User
Forcing a Simplexml Object to a String, Regardless of Context
Full Secure Image Upload Script
How to Declare a Global Variable in PHP
Upload Photo to Album With Facebook'S Graph API