How to archive/zip folder on your server

This might not be the best code for this, yet its the most concise code I found that doesnt require a bunch of additional classes.  I found this online dont remember where and I added a few lines cause of errors.  If you get memory errors you can increase the memory (ini_set(‘memory_limit’, ‘300M’).

<?php
ini_set(‘memory_limit’, ‘300M’);
$archive = ‘zipbackup.zip’; // File Name ;
$source = ‘wp-content/gallery/’; // Path To the folder;
$destination = ‘zipbackup.zip’; // Path to the file and file name ;
$include_dir = false;

if(file_exists($destination)) {
unlink($destination);
}
ob_start();

if (!extension_loaded(‘zip’) || !file_exists($source)) {
return false;
}

if (file_exists($destination)) {
unlink ($destination);
}

$zip = new ZipArchive;

if (!$zip->open($archive, ZipArchive::CREATE)) {
return false;
}
$source = str_replace(‘\\’, ‘/’, realpath($source));
if (is_dir($source) === true)
{

$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST);

if ($include_dir) {

$arr = explode(“/”,$source);
$maindir = $arr[count($arr)- 1];

$source = “”;
for ($i=0; $i < count($arr) – 1; $i++) {
$source .= ‘/’ . $arr[$i];
}

$source = substr($source, 1);

$zip->addEmptyDir($maindir);

}

foreach ($files as $file)
{
$file = str_replace(‘\\’, ‘/’, $file);

// Ignore “.” and “..” folders
if( in_array(substr($file, strrpos($file, ‘/’)+1), array(‘.’, ‘..’)) )
continue;

$file = realpath($file);

if (is_dir($file) === true)
{
$zip->addEmptyDir(str_replace($source . ‘/’, ”, $file . ‘/’));
//echo “added directory: ” . $file . “<br>”;
}
else if (is_file($file) === true)
{
$zip->addFromString(str_replace($source . ‘/’, ”, $file), file_get_contents($file));
//echo “added image: ” . $file . “<br>”;
}
}
}
else if (is_file($source) === true)
{
$zip->addFromString(basename($source), file_get_contents($source));
}
$zip->close();

echo ‘ location.replace(“backup.zip”); ‘; //name of zip here
?>

How to rename nggallery folders

Ok so if you were in a hurry to set up your wordpress site and didnt pay much attention to the folder structure of the nggalleries, then you might want to rename them and possibly keep a backup copy.

To do this you will ftp into your wp-content/gallery folder, (make a backup copy first) and then rename the folder.  Keep track of what you are fixing.  Then change the path in the nggallery > manage gallery tab, selecting the same gallery.

rename

Then one step often missed is to clear the nggallery cache, in nggallery > other options tab.

clear

Add NGGallery gallery previewpic, title, desc and info to any page via php

Was having a difficult time finding the code for this as many people have different code.  I figure it should be real simple as the fields are provided in each gallery to add a title, desc, previewpic, and other info.  The issue is that many are using $nggdb->get_gallery.   The answer is to use $nggdb->find_gallery.  You will need to know the gallery id.  You can then get info for the previewpic using $nggdb->find_image.  This is the code for both:

<?php

global $nggdb;

$galleryID = 22; // add image id here

$gallery = $nggdb->find_gallery($galleryID);

echo “gallery id: ” . $gallery->gid . “<br>”;
echo “gallery name: ” . $gallery->name . “<br>”;
echo “gallery slug: ” . $gallery->slug . “<br>”;
echo “gallery path: ” . $gallery->path . “<br>”;
echo “gallery title: ” . $gallery->title . “<br>”;
echo “gallery desc: ” . $gallery->galdesc . “<br>”;
echo “gallery page linked id: ” . $gallery->pageid . “<br>”;
echo “gallery preview image id: ” . $gallery->previewpic . “<br>”;
echo “gallery author: ” . $gallery->author . “<br>”;

$previewimg = $gallery->previewpic;
echo “preview image is ” . $previewimg . “<br><br>”;

$detail = $nggdb->find_image($previewimg);

echo “<br><br>”;
echo “url: “;
echo $detail->imageURL;
echo “<br>”;
echo “title: “;
echo $detail->description;
echo “<br>”;
echo “alttext: “;
echo $detail->alttext;
echo “<br>”;
echo “thumbURL: “;
echo $detail->thumbURL;

?>

if you need more info about the image you see all available fields with this code:

<?php
echo “<pre>”;
var_dump($detail);
echo “</pre>”;
?>

 

 

 

How to fix a mac exfat formatted disk

If you have a mac, you might format an external disk in the exfat format to make it compatible with mac and windows.  This formatting for a hard drive disk is available in the disk utility app in the Applications > Utilities.

Now, if for some reason your disk is acting up, because you might have unplugged it while it was transferring a file or instead of ejecting the hard drive disk (by selecting it and right clicking eject), there is a way to fix it with these steps:

  1. find the terminal app in Applications > Utilities > Terminal.app and launch it (by double clicking it).
  2. at the flashing character prompt, type “diskutil list” and return key.
  3. then look for your disk in the list, you will probably recognize it from the gigabytes size and it will probably say something like “Windows_NTFS”.  find the disk “IDENTIFIER” name and copy it down.
  4. type the following line replacing the disk identifier with your disk name identifier:sudo fsck_exfat -d disk1s1
  5. it might say that your disk is busy.  keep trying until you see it start to name off all the files on the disk.  this could take a while, but take a breath cause you saved your disk drive.
  6. after its finished repairing, your disk will work as before.  you can then close the terminal app. bravo!

How to log visits with php

There is a lot of insight available when logging traffic to your website.  There are sometimes people trying to log into your wp admin and you can see that traffic and block it with htaccess.  Also you can see what keywords are being used to arrive at your site.  These keywords you can use to then create dedicated pages to boost seo.  Be sure to set your timezone.  Be sure to fix the apostrophes once you copy to php.  This code will create a new .csv doc for each day.

<?php

date_default_timezone_set(‘America/Los_Angeles’);
$today = date(“m-d-y g:ia”);
$logData = $_SERVER[‘REMOTE_ADDR’] . “,” .
$today . “,” .
$_SERVER[‘REQUEST_METHOD’] . “,” .
$_SERVER[‘REQUEST_URI’] . “,” .
$_SERVER[‘HTTP_REFERER’] . “,” .
$_SERVER[‘HTTP_USER_AGENT’];
$file = “folder/“ . date(“m-d-y”) . “.csv”;
$fp = fopen($file,”a”);
fwrite($fp,”$logData\n”);
fclose($fp);
?>

Fixed Error Warning: Division by zero in nggallery scroll

replace 287-302 with this…the issue is that $picture[“width”] and $picture[“height”] are not returning values :

if($adjustImagesize==true){
$imgsize = @getimagesize($picture[“img”]);//0=width 1=height
$imgwidth = $imgsize[0];
$imgheight = $imgsize[1];
//$imgwidth = $picture[“width”];
//$imgheight = $picture[“height”];
if($width>$height){//landscape
//get new size
$newimageheight=$imgheight;
$newimagewidth=($imgwidth/$imgheight)*$newimageheight;
//check ob passt, sonst weiter verkleinern
if($newimagewidth>$width){
$newimagewidth2=$width;
$newimageheight=($newimageheight/$newimagewidth)*$newimagewidth2;
$newimagewidth=$newimagewidth2;
}