Snippet in PHP for Unzipping a File
Using this below PHP snippet, you can be able to unzip a
file on it whenever you rip with this code running through PHP. The initial
unzip function checks the file to unzip and sees a location with an unlink action
confirming the unzipping environment you execute. Making use of this coding
process has better results you comply with as unzipping software’s are not
reliable and might cause bad sectors on your hard disk.
function
unzip($location,$newLocation)
{
if(exec("unzip
$location",$arr)){
mkdir($newLocation);
for($i = 1;$i<
count($arr);$i++){
$file =
trim(preg_replace("~inflating: ~","",$arr[$i]));
copy($location.'/'.$file,$newLocation.'/'.$file);
unlink($location.'/'.$file);
}
return TRUE;
}else{
return FALSE;
}
}
function
unzip($location,$newLocation)
{
if(exec("unzip
$location",$arr)){
mkdir($newLocation);
for($i = 1;$i<
count($arr);$i++){
$file =
trim(preg_replace("~inflating: ~","",$arr[$i]));
copy($location.'/'.$file,$newLocation.'/'.$file);
unlink($location.'/'.$file);
}
return TRUE;
}else{
return FALSE;
}
}
<?php
unzip('test.zip','unziped/test');
//File would be unzipped in unziped/test folder
?>
<?php
unzip('test.zip','unziped/test');
//File would be unzipped in unziped/test folder
?>
Zipping a File in PHP
Using the below PHP snippet,
you would be able to zip files instantly:
This code snippet is to zip files instantly and immediately,
which has prominent advantages to cut down disk space where you store files, intended
for recurrent use on your PC computer. Making initial declaration of how you
want to zip file parameters defined through the first function and have a
reliable stream of signals and characters compressed to zip a file you want with
reduced space.
function
create_zip($files = array(),$destination = '',$overwrite = false) {
//if the zip file already exists and
overwrite is false, return false
if(file_exists($destination) &&
!$overwrite) { return false; }
//vars
$valid_files = array();
//if files were passed in...
if(is_array($files)) {
//cycle through each file
foreach($files as $file) {
//make sure the file exists
if(file_exists($file)) {
$valid_files[] = $file;
}
}
}
//if we have good files...
if(count($valid_files)) {
//create the archive
$zip = new ZipArchive();
if($zip->open($destination,$overwrite
? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {
return false;
}
//add the files
foreach($valid_files as $file) {
$zip->addFile($file,$file);
}
//debug
//echo 'The zip archive contains
',$zip->numFiles,' files with a status of ',$zip->status;
//close the zip -- done!
$zip->close();
//check to make sure the file
exists
return file_exists($destination);
}
else
{
return false;
}
}
function
create_zip($files = array(),$destination = '',$overwrite = false) {
//if the zip file already exists and
overwrite is false, return false
if(file_exists($destination) &&
!$overwrite) { return false; }
//vars
$valid_files = array();
//if files were passed in...
if(is_array($files)) {
//cycle through each file
foreach($files as $file) {
//make sure the file exists
if(file_exists($file)) {
$valid_files[] = $file;
}
}
}
//if we have good files...
if(count($valid_files)) {
//create the archive
$zip = new ZipArchive();
if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE :
ZIPARCHIVE::CREATE) !== true) {
return false;
}
//add the files
foreach($valid_files as $file) {
$zip->addFile($file,$file);
}
//debug
//echo 'The zip archive contains
',$zip->numFiles,' files with a status of ',$zip->status;
//close the zip -- done!
$zip->close();
//check to make sure the file
exists
return file_exists($destination);
}
else
{
return false;
}
}
<?php
$files=array('file1.jpg',
'file2.jpg', 'file3.gif');
create_zip($files,
'myzipfile.zip', true);
?>
<?php
$files=array('file1.jpg',
'file2.jpg', 'file3.gif');
create_zip($files,
'myzipfile.zip', true);
?>
PHP Snippets for Different Operations
PHP Snippet to Resize
Image:
Sometimes clients can upload a large image, as you might
want to resize it. Below PHP Development snippet would enable you to do that. Changing the
size of an image is important when you are looking for fitting a visual or
banner in a location having limited space, still promoting you further after
you use this code snippet for the purpose.
function
resize_image($filename, $tmpname, $xmax, $ymax)
{
$ext = explode(".",
$filename);
$ext = $ext[count($ext)-1];
if($ext == "jpg" || $ext ==
"jpeg")
$im =
imagecreatefromjpeg($tmpname);
elseif($ext == "png")
$im =
imagecreatefrompng($tmpname);
elseif($ext == "gif")
$im = imagecreatefromgif($tmpname);
$x = imagesx($im);
$y = imagesy($im);
if($x <= $xmax && $y <=
$ymax)
return $im;
if($x >= $y) {
$newx = $xmax;
$newy = $newx * $y / $x;
}
else {
$newy = $ymax;
$newx = $x / $y * $newy;
}
$im2 = imagecreatetruecolor($newx,
$newy);
imagecopyresized($im2, $im, 0, 0, 0, 0,
floor($newx), floor($newy), $x, $y);
return $im2;
}
function
resize_image($filename, $tmpname, $xmax, $ymax)
{
$ext = explode(".",
$filename);
$ext = $ext[count($ext)-1];
if($ext == "jpg" || $ext ==
"jpeg")
$im =
imagecreatefromjpeg($tmpname);
elseif($ext == "png")
$im =
imagecreatefrompng($tmpname);
elseif($ext == "gif")
$im = imagecreatefromgif($tmpname);
$x = imagesx($im);
$y = imagesy($im);
if($x <= $xmax && $y <=
$ymax)
return $im;
if($x >= $y) {
$newx = $xmax;
$newy = $newx * $y / $x;
}
else {
$newy = $ymax;
$newx = $x / $y * $newy;
}
$im2 = imagecreatetruecolor($newx,
$newy);
imagecopyresized($im2, $im, 0, 0, 0, 0,
floor($newx), floor($newy), $x, $y);
return $im2;
}
Comments
Post a Comment