09 Defining Your Directories By pdq

Started by Mindless, July 21, 2012, 08:08:24 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Mindless

#1
Credits to pdq
Most PHP scripts perform File IO operations, whether it be inclusion of scripts or opening or writing to files. One common mistake made by developers often makes these operations much slower then necessary.

How NOT to open files
Code (php) Select
<?php
include "file.php";
?>



Always use full path file path when opening files, to avoid expensive normalization of the file's path.

Code (php) Select
<?php
include "/path/to/file.php";
// or
include "./file.php";
?>


This is very important even if you use opcode cache, since the paths of includes and normal files will still need to be resolved.

OK...

in include/config.php find: *(and /** comment out **/ once you have edited ALL files like below examples)

Code (php) Select
/*
if ( strtoupper( substr(PHP_OS, 0, 3) ) == 'WIN' )
  {
    $file_path = str_replace( "\\", "/", dirname(__FILE__) );
    $file_path = str_replace( "/include", "", $file_path );
  }
  else
  {
    $file_path = dirname(__FILE__);
    $file_path = str_replace( "/include", "", $file_path );
  }
define('ROOT_PATH', $file_path)
*/


Code (php) Select
then add this below the above ^^:
/** define dirs **/
define('INCL_DIR', dirname(__FILE__).DIRECTORY_SEPARATOR);
define('ROOT_DIR', realpath(INCL_DIR.'..'.DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR);
define('ADMIN_DIR', ROOT_DIR.'admin'.DIRECTORY_SEPARATOR);
define('FORUM_DIR', ROOT_DIR.'forums'.DIRECTORY_SEPARATOR);
define('CACHE_DIR', ROOT_DIR.'cache'.DIRECTORY_SEPARATOR);


now find:
Code (php) Select
$TBDEV['torrent_dir'] = ROOT_PATH . '/torrents'; # must be writable for httpd user

change to:
Code (php) Select
$TBDEV['torrent_dir'] = ROOT_DIR.'torrents'; # must be writable for httpd user

find:
Code (php) Select
$TBDEV['sql_error_log'] = ROOT_PATH.'/logs/sql_err_'.date("M_D_Y").'.log';

replace with:
Code (php) Select
$TBDEV['sql_error_log'] = ROOT_DIR.'logs'.DIRECTORY_SEPARATOR.'sql_err_'.date('M_D_Y').'.log';

then save config.php.

That was the easy part, now edit all your files replacing all require, require_once and include lines to like these following examples

top of bittorrent.php: (there are many requires in bittorrent.php, this is just one example)

Code (php) Select
require_once("include/config.php");
require_once("cleanup.php");


would become:
Code (php) Select
require_once(dirname(__FILE__).DIRECTORY_SEPARATOR.'config.php');
require_once(INCL_DIR.'cleanup.php');


top of index.php:
Code (php) Select
require_once "include/bittorrent.php";
require_once "include/user_functions.php";


would become:
Code (php) Select
require_once(dirname(__FILE__).DIRECTORY_SEPARATOR.'include'.DIRECTORY_SEPARATOR.'bittorrent.php');
require_once(INCL_DIR.'user_functions.php');


down a little lower in index.php is another:
Code (php) Select
require_once "include/bbcode_functions.php";

      $button = "";


would become:
Code (php) Select
require_once INCL_DIR.'bbcode_functions.php';

      $button = "";


hope you get the idea, so based on examples do all your files lol.   

If using php 5.3 or higher can change from above

Code (php) Select
dirname(__FILE__)

to the all new magic constant:
Code (php) Select
__DIR__

Good luck!

=].