Jump to content

Alternative for the define("FILE_ROOT", "");


neven

Recommended Posts

Hi I am using this code for directory tree ...and it is working fine except the "define("FILE_ROOT", "$VARIABLE");"  because I can't use variable in the define function ...but I need the variable as the file root in next code ...

Not working: I need this

define("FILE_ROOT", $diro);

Working cod:

define("FILE_ROOT", "store");

The index script that get the directorys and files in to the directory tree..

		<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
		<html>
		<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
		<meta name="keywords"  content="" />
		<meta name="description" content="" />
		<title>Editable jquery tree with php codes</title>
		<link rel="stylesheet" type="text/css" href="js/jquery/plugins/simpleTree/style.css" />
		<link rel="stylesheet" type="text/css" href="styleis.css" />
		<script type="text/javascript" src="js/jquery/jquery.min.js"></script>
		<script type="text/javascript" src="js/jquery/plugins/jquery.cookie.js"></script>
		<script type="text/javascript" src="js/jquery/plugins/simpleTree/jquery.simple.tree.js"></script>
		<script type="text/javascript" src="js/langManager.js" ></script>
		<script type="text/javascript" src="js/treeOperations.js"></script>
		<script type="text/javascript" src="js/init.js"></script>
		</head>
		<body>
		<table border='2' width='633'>
		<tr>
		<td>
		<div class="contextMenu" id="myMenu1">	
				<li class="addFolder">
					<img src="js/jquery/plugins/simpleTree/images/folder_add.png" /> </li>
				<li class="addDoc"><img src="js/jquery/plugins/simpleTree/images/page_add.png" /> </li>	
				<li class="edit"><img src="js/jquery/plugins/simpleTree/images/folder_edit.png" /> </li>
				<li class="delete"><img src="js/jquery/plugins/simpleTree/images/folder_delete.png" /> </li>
				<li class="expandAll"><img src="js/jquery/plugins/simpleTree/images/expand.png"/> </li>
				<li class="collapseAll"><img src="js/jquery/plugins/simpleTree/images/collapse.png"/> </li>	
		</div>
		<div class="contextMenu" id="myMenu2">
				<li class="edit"><img src="js/jquery/plugins/simpleTree/images/page_edit.png" /> </li>
				<li class="delete"><img src="js/jquery/plugins/simpleTree/images/page_delete.png" /> </li>
		</div>


		<div id="wrap">
			<div id="annualWizard">	
					<ul class="simpleTree" id='pdfTree'>		
							<li class="root" id='<?php echo $treeManager->getRootId();  ?>'><span><?php echo $rootName; ?></span>
								<ul><?php echo $treeElements; ?></ul>	


							</li>
					</ul>						
			</div>
			<div>

			</div>
		</div> 
		<div id='processing'></div>
		</td>
		</tr>
		</table>
		</body>
		</html>

This is the script that makes the directory config.php :

<?php
require('connecto.php');
		session_start();
		error_reporting(0);

		     $id = $_GET['id'];
			 $q = "SELECT * FROM albums WHERE $id = ideo";
			 $results = mysql_query($q);
			 $data = mysql_fetch_array($results);
			 $diro = $data['dosjeid'];
                 $id = $data['ideo'];
                 
/**************************************
*   File Name: config.php
*   Begin: Thursday, June, 06, 2006
*   Author: Ahmet Oguz Mermerkaya 	
*   Email: ahmetmermerkaya@hotmail.com
***************************************/ 

if (!defined("IN_PHP"))
{
die();
}

/** 
* if you want to run in demo mode, enable the macro below
* if it runs in demo mode, it doesn't care what TARGET_PLATFORM is
*/
//define("DEMO_MODE", true);


/** target platform types
*  this script runs for both database(mysql) and file system. 
*  database is supposed to be mysql
*/
define ("DATABASE_PLATFORM", 0); // Don't edit
define ("FILE_SYSTEM_PLATFORM", 1); // Don't edit


/** 
* choose target platform according to your needs 
* it may be DATABASE_PLATFORM or FILE_SYSTEM_PLATFORM
*/
define ("TARGET_PLATFORM", FILE_SYSTEM_PLATFORM);


if (TARGET_PLATFORM == FILE_SYSTEM_PLATFORM)
{
/**
	* if FILE_SYSTEM_PLATFORM is selected as TARGET_PLATFORM,
	* then assign root folder adress to FILE_ROOT macro
	*/


	$dirko = ("store/".$diro);

define("FILE_ROOT", $dirko);

}
else if (TARGET_PLATFORM == DATABASE_PLATFORM)
{
/**
 * if DATABASE_PLATFORM is selected as TARGET_PLATFORM,
 * set the variables to connect database and 
 * set the TREE_TABLE_PREFIX if you want to
 * change the prefix of table name
 */	
// set database host 
$dbHost = "localhost";
//set database user name    
$dbUsername = "root"; 
// set password to connect to database	 
$dbPassword = "21236161";
// set database name
$dbName = "php";
// set database table prefix 
define ("TREE_TABLE_PREFIX", "tree");
}
else {
die("No known target platform specified, in includes/config.php");
}


define ("SUCCESS", 1);  // Don't edit
define ("FAILED", -1);  // Don't edit
define ("FAILED_FILE_WITH_SAME_NAME_EXIST", -2); // Don't edit, it is only used in FILE_SYSTEM_PLATFORM

error_reporting(0); 
?>

And this is File tree menager.php script....

<?php
require('connecto.php');
		session_start();
		error_reporting(0);

		     $id = $_GET['id'];
			 $q = "SELECT * FROM albums WHERE $id = ideo";
			 $results = mysql_query($q);
			 $data = mysql_fetch_array($results);
			 $diro = $data['dosjeid'];
                 $id = $data['ideo'];
/**************************************
     File Name: DemoTreeManager.php
     Begin:  Wednesday, April, 15, 2009, 21:49 
     Author: Ozan Koroglu
			 Ahmet Oguz Mermerkaya 	
     Email:  koroglu.ozan@gmail.com
     		 ahmetmermerkaya@hotmail.com
***************************************/ 

require_once("ITreeManager.php");

class FileTreeManager implements ITreeManager 
{
private $fileRoot;

const FOLDER_SEPARATOR = "FOLDER_SEPARATOR";

const DEFAULT_FOLDER_SEPARATOR = "/";

public function __construct($fileRoot) {
	if (is_dir($fileRoot)) 
	{
		$this->fileRoot = $fileRoot;
		//if ($this->fileRoot[strlen($this->fileRoot) - 1] != '/' ) {
		//	$this->fileRoot .= '/';
		//}

	}
	else {
		die($fileRoot .' is not a directory ');
	}

}

public function insertElement($name, $ownerEl, $slave) 
{		
	$realOwnerEl = str_replace(self::FOLDER_SEPARATOR, self::DEFAULT_FOLDER_SEPARATOR, $ownerEl);
	$fullPath = $this->getFullPath($realOwnerEl. self::DEFAULT_FOLDER_SEPARATOR . $name);

	$out = FAILED;
	if (file_exists($fullPath) === true) {
		$out = FAILED_FILE_WITH_SAME_NAME_EXIST;
	}
	else {
		if ($slave == 1) {
			if (touch($fullPath) === true) {							
				$out = '({ "elementId":"'. $ownerEl . self::FOLDER_SEPARATOR . $name . '", "elementName":"'.$name.'", "slave":"'.$slave.'"})';
			}			
		}
		else{
			if (mkdir($fullPath, 0755)) {
				$out = '({ "elementId":"'. $ownerEl . self::FOLDER_SEPARATOR . $name .'", "elementName":"'.$name.'", "slave":"'.$slave.'"})';
			}
		}
	}

	return $out;			
}


private function getFullPath($relativePath) 
{
	return $this->fileRoot . self::DEFAULT_FOLDER_SEPARATOR . $relativePath;
}


public function getElementList($ownerEl, $pageName) 
{
	$realOwnerEl = str_replace(self::FOLDER_SEPARATOR, self::DEFAULT_FOLDER_SEPARATOR, $ownerEl);
	$fullPath = $this->getFullPath($realOwnerEl);
	$str = null;



	if (is_dir($fullPath) && $handle = opendir($fullPath))
	{
		$fileStr = null;
    	/* This is the correct way to loop over the directory. */
    	
    	
	   	while (false !== ($file = readdir($handle))) 
		{
			if ($file == '.' || $file == '..' || $file == 'Thumbs.db'){
				continue;
			}
			$supp = NULL;	

			if (is_dir($fullPath . "/" . $file)) {

				$supp = "<ul class='ajax'>"
							."<li id='". $ownerEl. self::FOLDER_SEPARATOR . $file ."'>{url:".$pageName."?action=getElementList&ownerEl=". $ownerEl. self::FOLDER_SEPARATOR . $file ."}</li>"
						."</ul>";
			}


			$fileStr[] .= "<li class='text' id='". $ownerEl. self::FOLDER_SEPARATOR . $file."'>"
							."<span>$file</span>   <a href='".$fullPath."/".$file."'>View the file</a>"
								. $supp
							."</li>";	



								       			
		}


    		closedir($handle);
		if ($fileStr != null) {
			sort($fileStr);
			$str = implode($fileStr);
		}
	}



		return $str; 		
	}


    

	public function updateElementName($name, $elementId,$ownerEl) 
{
	$ownerEl = str_replace(self::FOLDER_SEPARATOR, self::DEFAULT_FOLDER_SEPARATOR, $ownerEl);

	$newElementId = $ownerEl . self::DEFAULT_FOLDER_SEPARATOR . $name;

	$elementId = str_replace(self::FOLDER_SEPARATOR, self::DEFAULT_FOLDER_SEPARATOR, $elementId);
	$realElementId = $ownerEl 
					. self::DEFAULT_FOLDER_SEPARATOR 
					. substr($elementId, strrpos($elementId, "/"), strlen($elementId));

	$fullPath = $this->getFullPath($realElementId);
	//$newElementId = substr($realElementId, 0,  strrpos($realElementId, "/")). "/" . $name;

	$newFullPath = $this->getFullPath($newElementId);

	$out = FAILED;
	if (file_exists($newFullPath) === true && dirname($fullPath) != dirname($newFullPath) ) {
		$out = FAILED_FILE_WITH_SAME_NAME_EXIST;
	}
	else if (rename($fullPath, $newFullPath) == true) {
		$newRealElementId = str_replace(self::DEFAULT_FOLDER_SEPARATOR, self::FOLDER_SEPARATOR, $newElementId);
		$out = '({"elementName":"'.$name.'", "elementId":"'. $newRealElementId .'"})';
	}

	return $out;
	}





	public function deleteElement($elementId, &$index = 0, $ownerEl)
{

	$elementId = str_replace(self::FOLDER_SEPARATOR, self::DEFAULT_FOLDER_SEPARATOR, $elementId);
	$elementId = str_replace(self::FOLDER_SEPARATOR, self::DEFAULT_FOLDER_SEPARATOR, $ownerEl)
				. self::DEFAULT_FOLDER_SEPARATOR 
				. substr($elementId, strrpos($elementId, "/"), strlen($elementId));
		$fullPath = $this->getFullPath($elementId);

	$out = FAILED;
	if (is_dir($fullPath) && $this->delete_recursive_dirs($fullPath)) {
		$out = SUCCESS;
	}
	else if (unlink($fullPath)) {
		$out = SUCCESS;
	}		

	return $out;
	} 	



	public function changeOrder($elementId, $oldOwnerEl, $destOwnerEl, $destPosition)
{ 		
	$oldOwnerEl = str_replace(self::FOLDER_SEPARATOR, self::DEFAULT_FOLDER_SEPARATOR, $oldOwnerEl);
	$elementId = str_replace(self::FOLDER_SEPARATOR, self::DEFAULT_FOLDER_SEPARATOR, $elementId);

	$realElementId = $oldOwnerEl . self::DEFAULT_FOLDER_SEPARATOR 
					 . substr($elementId, strrpos($elementId, "/"), strlen($elementId));

	$fullPath = $this->getFullPath($realElementId);		
	$elementName = substr($realElementId, strrpos($realElementId, "/")+1); // plus 1 not to get / character
	$realDestOwnerEl = str_replace(self::FOLDER_SEPARATOR, self::DEFAULT_FOLDER_SEPARATOR, $destOwnerEl);

	$newFullPath = $this->getFullPath($realDestOwnerEl . "/". $elementName);

	$newElementId = $destOwnerEl . self::FOLDER_SEPARATOR . $elementName;
	$out = FAILED;

	if (file_exists($newFullPath) === true && dirname($fullPath) != dirname($newFullPath)) {
		$out = FAILED_FILE_WITH_SAME_NAME_EXIST;
	}
	else if (rename($fullPath, $newFullPath) == true) {
		$out = '({"oldElementId":"'. str_replace(self::DEFAULT_FOLDER_SEPARATOR, self::FOLDER_SEPARATOR,  $elementId)
				.'", "elementId":"'. $newElementId .'"})';;
	}		

	return $out;  		
	}

public function getRootId(){
	return self::FOLDER_SEPARATOR;
}

private function delete_recursive_dirs($dirname) 
{ 
   // recursive function to delete 
  // all subdirectories and contents: 
	if(is_dir($dirname)) {
		$dir_handle=opendir($dirname);
	}
  	while($file=readdir($dir_handle)) 
  	{ 
  	  if($file!="." && $file!="..") 
  	  { 
  	    if(!is_dir($dirname."/".$file)){ 
			unlink ($dirname."/".$file); 
		}
    	else {
    		$this->delete_recursive_dirs($dirname."/".$file); 
		}
      } 
  	} 
  	closedir($dir_handle); 
  	rmdir($dirname); 
  	return true; 
}

}

?>

 

Do someone have an concrete idea for this problem?

Please LET ME KNOW I need this fast... Thanks .

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.