Category Archives: PhP

PhP hidden gadgets

Standard

Today I found out that there are some hidden stuff to the php package. It wont work on any server, but check those links 🙂

http://www.webdigity.com/?=PHPE9568F36-D428-11d2-A769-00AA001ACF42

http://www.textpattern.com/faq?=PHPE9568F36-D428-11d2-A769-00AA001ACF42

Funny huh?

I found this from this discussion which my friend Olaf started

Advertisements

Backup all your databases with one click

Standard

Bellow is a php script that I made for taking backups of my mySql databases. The script is designed to run under console(CLI) in root mode, so I don’t think that it will work for shared hosting accounts, but you can try it, or try to modify the exec commands with SUDO.

<?php
//================================================
//
// Auto backup your databases
// Author: Nick Papanotas
// Comments/feedback/troubleshooting:
// http://www.webdigity.com/
//
// This script propably wont run from web, except
// if you modify it to add SUDO.
//================================================

$backup_dir = dirname( __FILE__ ) . ‘/backups/’;

$u = ‘User’;
$p = ‘XXXX’;
$db = ‘Adatatbase’;//This is just the name of a database just to make the query work. The script will backup all the databases that your use has access to.

$db_link = mysql_connect($h,$u,$p);
$res = mysql_db_query($db, ‘SHOW DATABASES’, $db_link) or die(‘Could not connect: ‘ . mysql_error());
echo
‘Found ‘. mysql_num_rows($res) . ‘ databases’ . “\n”;

while ( $rec = mysql_fetch_array($res) )
{
//Parse time :
$time = microtime();
$time = explode(‘ ‘, $time);
$time = $time[1] + $time[0];
$start = $time;

echo $rec[0] . “\n”;
shell_exec( ‘mysqldump –result-file=’.$backup_dir.$rec[0].‘.’.date(‘Y-m-d’).‘.sql –password=’.$p.‘ ‘.$rec[0] );
//Parse time :
$time = microtime();
$time = explode(‘ ‘, $time);
$time = $time[1] + $time[0];
$finish = $time;
$total_time = round(($finish $start), 6);
echo
‘Parsed in ‘ . $total_time . ‘ secs’ . “\nStarting with compression\n”;
}
//Let’s tar those backups :
shell_exec( ‘tar cvf ‘.$backup_dir.date(‘Y-m-d’).‘.tar ‘.$backup_dir.‘*.sql’ );
?>

This script has been originally posted to webdigity’s code library.

A class that automatically generates a menu of links (sitemap)

Standard

Use this php class to create a sitemap for your site, or create a links menu.

The function is recursive, so it will read the content of the directories, and the subdirectories in them.

Here is the class file:

Code:

<?php
<?php
class file2menu {
////////////////////////////////////////////////////
// File to menu converter class
//
// Version 1.00
//
// This class is creating an HTML menu from the files that
// exist in a directory, and its subdirectories
//
// Author: Nick Papanotas
//
// License: GPL, see LICENSE
// Support: You can post your questions/bugs or anything else
// to http://www.webdigity.com/
//
////////////////////////////////////////////////////

/*
The directory that we want to create the listing for. Should be relative to the web site’s root path
@var string
*/
var $directory;
/*
The file types that will be displayed in the menu
@var string
*/
var $acceptedFileTypes = array (‘php’,‘html’,‘htm’);
/*
Should we display the file extension to the menu?
@var string
*/
var $displayExt = false;
/*
Created by the preparse() function
@var array
*/
var $listArray;

/*********************************************************
* PUBLIC FUNCTIONS *
*********************************************************/

/*
A constructor function
@access public
@return bool
*/
function file2menu($directory)
{
$this->directory = $directory;
if ( empty (
$this->directory ))
die ( ‘Directory not selected’ );
else
{
$this->directory = substr($this->directory,0,1) != ‘/’?‘/’.$this->directory:$this->directory;
$this->directory = substr($this->directory,-1) == ‘/’?substr($this->directory,0,-1):$this->directory;
$this->directory = $_SERVER[‘DOCUMENT_ROOT’]. $this->directory;
if ( !
is_dir($this->directory ))
die ( $this->directory . ‘ is not a directory’);
}
return
true;
}
/*
Creates the $this->listArray array. You don’t have to use this function
This function is a modified version of http://fundisom.com/phparadise/php/directories/recursive_directory_listing
@access private/puplic
@return array
*/
function preparse($dir = )
{
$dir = empty($dir)?$this->directory:$dir;
$dirlist = opendir($dir);
while (
$file = readdir ($dirlist))
{
if (
$file != ‘.’ && $file != ‘..’)
{
$newpath = $dir.‘/’.$file;
$level = explode(‘/’,$newpath);
if (
is_dir($newpath))
{
$mod_array[] = array(
‘level’=>count($level)-1,
‘path’=>$newpath,
‘relPath’ => substr($newpath,strlen($_SERVER[‘DOCUMENT_ROOT’])),
‘name’=>end($level),
‘type’=>‘dir’,
‘mod_time’=>filemtime($newpath),
‘content’=>$this->preparse($newpath));
}else{
$ext = array_pop(explode(‘.’,$file));
if ( in_array ($ext,$this->acceptedFileTypes))
{
$end = end($level);
$mod_array[] = array(
‘level’=>count($level)-1,
‘name’=>$this->displayExt?$end:substr($end,0,(strlen($end) – strlen($ext)-1)),
‘relPath’ => substr($newpath,strlen($_SERVER[‘DOCUMENT_ROOT’])),
‘type’=>‘file’,
‘mod_time’=>filemtime($newpath),
‘size’=>filesize($newpath));
}
}
}
}
closedir($dirlist);
$this->listArray = $mod_array;
return
$mod_array;
}

/*
A recursive function that displays a menu. If you want to modify the look of the autogenerated menu, this is the function you have to change
@access public
@return NULL
*/
function DisplayMenu($list=,$displayHeaderFooter = true)
{
if ( empty(
$this->listArray))
$this->preparse();
$list = empty($list)?$this->listArray:$list;
$levelsSub = count(explode(‘/’,$_SERVER[‘DOCUMENT_ROOT’]));
if (
$displayHeaderFooter )
echo ‘<pre>’;//You can put some html header here
foreach ( $list as $item )
{
for (
$i=$levelsSub; $i<$item[‘level’];$i++)
echo “\t”;
if ( $item[‘type’] == ‘file’ )
{
//Here we parse files :
echo ‘<a href=”‘.$item[‘relPath’].‘”>’.$item[‘name’].‘</a><br />’;
}else{
//Here we parse directories:
echo ‘<b>’.$item[‘name’].‘</b><br />’;
if ( !empty($item[‘content’]))
echo $this->DisplayMenu($item[‘content’],false);
}
}
if (
$displayHeaderFooter )
echo ‘</pre>’;//You can put some html footer here
}

/*
Saves the output of the DisplayMenu function to a file.
@access public
@return bool
*/
function SaveAs ( $fileName )
{
if (!
$fp = @fopen ($fileName, ‘w’))
die ( ‘Can\’t open file for writing.’);
ob_start();
$this->DisplayMenu();
$data = ob_get_clean();
if ( !@
fwrite($fp, $data))
die( ‘Can\’t write to file.’);
if ( !@
fclose($fp))
die ( ‘Cam\’t close file.’);
return
true;
}
}

?>

 

To use the function you just need something like this :

<?php
$dir
= new file2menu(‘/myDirectory’);
$dir->DisplayMenu();

You can also use the SaveAs method to save the menu in a file :

<?php
$dir
= new file2menu(‘/myDirectory’);
$dir->SaveAs(‘FileToSave.html’);

If you have any comments, questions regarding this class, please post a topic here.

This class originally posted to webdigity’s code library.

 

Automatically optimize all tables in a MySQL database

Standard

Use this script preferably with a cronjob to automatically optimize all the tables in your mysql databases. Under root privilleges the script will search for all the databases in your server and will optimize them all. Hope you will like it as it took me more than 3 hours to write it 🙂

 

Optimize all databases/tables in MySQL
For more tutorials you can refer here