• 0

Rooter [PHP Root Folder Locator]


Question

Rooter by Yanike Mann (YeoWorks)

http://www.youtube.com/watch?v=heANjFpablU

Rooter is a better way of locating files from root using PHP. You can use Rooter for locating anything from a root folder of your choice. You can now place multiple includes in one file and rooter will know exactly how to find the files from root using the Rooter script. No more "./", "../", "../../", and etc done differently on each page to find files. Rooter takes care of that for you and also can use URLs as root as well.

Background of Rooter: I started my new job with a web development/video production company who used Microsoft Servers for development. Coming from PHP, I really didn't like ASP. Long story short.. Boss told me if I could find a way to make PHP use root like ASP does with "/", then I could code their sites in PHP. So I went home and created Rooter, which uses any folder of choice or domain as root.

You can use my Rooter script in personal and/or commercial products for FREE. Just leave the Copyright intact.

Rooter_by_Yanike_Mann.zip

Link to comment
Share on other sites

24 answers to this question

Recommended Posts

  • 0

Why would you need a whole class for this? You should know your root, and paths can be stored in a config file if such things are needed.

Also, that video is terrible - sliding all over the place!

Link to comment
Share on other sites

  • 0

I understand that you have offered the code as is, but as you have made an effort to make a video and a Google code page, then there are a few little things that I would have done (I have limited experience compared to others here btw).

The video is far too shaky. Use a scripted mouse or a fixed screen area to make it easier to watch.

Offer the code on a HTML page (not through a ZIP file). Also, don't compact the code into one line, it is a nightmare for inspecting/debugging/extending. Compacting code is normally used with Javascript files to minimise bandwidth between client and server.

Namespaces and Autoloaders can be used to deal with including extra PHP files from PHP 5.3+ with ease. I normally define a global constant from the root index using define('ROOT_PATH', dirname(__FILE__)) to use with the Autoloaers. Though I think your code deals with URLs and parsing HTML link tags instead. In this case, I either use a function to dynamically extract the base URL from the URI or use a pre-set value from a config file.

Also one last thing, it may have been better to enclose your routine in a function for re-use in other files.

Though do carry on providing code for all to use.

Link to comment
Share on other sites

  • 0

---- Rooter v2----

In Rooter v2, Rooter is a function instead of a variable. You can now just type rdot('FILENAME'); to locate a file from your selected ROOT. For PHP INCLUDES, you will use rdotinc('FILE');.

// ROOTER INCLUDE - Copyright 2012 Yanike Mann (YeoWorks) www.ywcorp.com yeoworks@gmail.com
// USE rdot() for locating anything using ./, ../, etc. e.g. rdot('img/ball.png');[/color]
function rdot($rstr){
$rooterchecker = 0; $rootdot = './'; $rootdot2 = '../'; while($rooterchecker != 2){ if($rooterchecker == 0){ include($rootdot."rooter.php"); if($rootercheck == 1){ echo $rootdot.$rstr; break; } else { $rooterchecker++; } } elseif($rooterchecker == 1){ include($rootdot2."rooter.php"); if($rootercheck == 1){ echo $rootdot2.$rstr; break; } else { $rootdot2 .= '../'; } } }
}
// USE rdotinc() FOR PHP INCLUDES e.g. rdotinc('inc/hi.php');[/color]
function rdotinc($rstr){
$rooterchecker = 0; $rootdot = './'; $rootdot2 = '../'; while($rooterchecker != 2){ if($rooterchecker == 0){ include($rootdot."rooter.php"); if($rootercheck == 1){ include($rootdot.$rstr); break; } else { $rooterchecker++; } } elseif($rooterchecker == 1){ include($rootdot2."rooter.php"); if($rootercheck == 1){ include($rootdot2.$rstr); break; } else { $rootdot2 .= '../'; } } }
}

<body>
	<p><?php rdotinc('inc/hi.php'); ?><br /><img src="<?php rdot('img/yeoworks.png'); ?>" /></p>
</body> 

Download Rooter

Link to comment
Share on other sites

  • 0

The main reason of Rooter is to easily have one or more root directories. No need to deal with config files. It's just a tool to use if you need it. You can use config files if you want. This is just another way to get to a ROOT folder or ROOT folders of your choice.


// rdot() is nba folder and rdot2() wnba folder.
<img src="<?php rdot('img/ball.png'); ?>" />
<img src="<?php rdot2('img/ball.png'); ?>" />

// using rdot() is easier to see and better than writing:

<img src="../../../../../../../nba/img/ball.png" />
<img src="../../../../../../../wnba/img/ball.png" />

// or writing this:

<img src="../../../../nba/img/ball.png" />
<img src="../../../../../../../wnba/img/ball.png" />

[/CODE]

Especially if you have to use the right number of dots by hand. rdot() takes care of everything for you.

Link to comment
Share on other sites

  • 0

Your code is written really sloppy, and although I haven't tried it, I know it won't work.

  • include($rootdot."rooter.php"); - why are we including rooter.php? I don't have a file called rooter.php... I thought copying the function into my PHP file would suffice?
  • if($rootercheck == 1) will never invoke, because it is inside if($rooterchecker == 0)
  • The two references to if($rootercheck == 1) will never invoke, because $rootercheck is never declared (or you typo'd $rooterchecker)
  • You're going from while($rooterchecker != 2) to if($rooterchecker == 0) $rooterchecker++; back round to the while loop - why?!
  • The whole if($rooterchecker == 0) and elseif($rooterchecker == 1) blocks are basically the same code repeated - why?

That's just in your rdot() function, I haven't even looked at your rdotinc() function.

I'm sure I could go on, but it seems like a very poor implementation of a simple $root = '/dir/'; approach. I was going to simply clean up your code, but after trying, I can't even figure out what your code's meant to do because it's so poorly written.

Link to comment
Share on other sites

  • 0

Your code is written really sloppy, and although I haven't tried it, I know it won't work....

Amazing how you know so much when you know nothing. Download the script and learn it before judging blind. The code is written fine. It's been tested with no errors so far. I use my code on commercial sites and others professionally use it. Are you really that.... to think I would share a code that doesn't work???

Anyways, it's there if you need it. I have things to do.

Link to comment
Share on other sites

  • 0

Amazing how you know so much when you know nothing. Download the script and learn it before judging blind. The code is written fine. It's been tested with no errors so far. I use my code on commercial sites and others professionally use it. Are you really that.... to think I would share a code that doesn't work???

Anyways, it's there if you need it. I have things to do.

I'm sorry mate, but Alex is right. Your script doesn't look like it would work at all.

Looking at v2

you have two declared functions, each of which attempts to include a file named rooter.php. These are both in a file called rooter.php. In each function the first iteration of the while loop attempts to include a file named rooter.php in the path ./ - ie, the functions attempt to include the file in which they were declared. This should cause a fatal error, since functions cannot be redeclared.

In the same functions a check is made against a variable called $rootercheck. This variable does not exist in the scope of either function, this should raise a php uninitialized variable warning.

In the case of both function it's hard to see how the following which you posted possibly translates

// rdot() is nba folder and rdot2() wnba folder.
&lt;img src="&lt;?php rdot('img/ball.png'); ?&gt;" /&gt;
&lt;img src="&lt;?php rdot2('img/ball.png'); ?&gt;" /&gt;

// using rdot() is easier to see and better than writing:

&lt;img src="../../../../../../../nba/img/ball.png" /&gt;
&lt;img src="../../../../../../../wnba/img/ball.png" /&gt;

how is rdot nba folder? where is that defined?

how is rdot2 wnba folder? where is that defined?

Are you suggestion we write a function for each root folder and include that globally in order to access files in those folders?

What the hell is wrong with

// include.php - include in main script, bootstrap, w/e
define('NBA_FOLDER', 'path/to/nba/');
define('WNBA_FOLDER', 'path/to/wnba/');

and then in html

&lt;img src="&lt;?php echo NBA_FOLDER . 'img/ball.png'; ?&gt;" /&gt;
&lt;img src="&lt;?php echo WNBA_FOLDER . 'img/ball.png'; ?&gt;" /&gt;

It's unclear from your code and from the files contained in your download how your functions are meant to function and where they should be placed, and how they should be used. Perhaps if you could address the shortcomings it might help. As it stands, I concur with Alex.

Link to comment
Share on other sites

  • 0

ok, so looking closer, and trying to understand what this is meant to do, we're meant to place a file named rooter some place in the file system, and when called it will prepend ../ to whatever path we supply to rdot() and rdotinc() until it eventually finds itself (rooter.php) and then use that as the base?

So, we need to then duplicate the functions with different names and place at least one in each path under which we need a root? Again, why would you not just define the roots as constants and use those? It seems a lot easier than first having to remember the paths to the rooters to include them, and then having to remember what you called the functions in order to use the correct ones. Maybe I'm missing something, just seems like an awful lot of extra code/files and definitely doesn't fit with the DRY principle.

Link to comment
Share on other sites

  • 0

It is a good script for a bit of fun to learn PHP and to play around with functions, while you're still learning PHP.

Really though i think most developers it would be common practice to define this type of data in a config file, use a bootstrapper or something similar.

Even for the smallest of projects.

Also why is your functions code all one line? It offers no performance gain and really makes it difficult for others to read and understand it. Especially with the amount of ifs and whiles you use.

Link to comment
Share on other sites

  • 0

I believe the script is basically equivalent to this (if HI_IM_THE_ROOT.wtf was in your 'root' directory):

function rdot($rstr){
$path = './';
while (!file_exists($path . 'HI_IM_THE_ROOT.wtf')) { $path .= '../'; }
echo $path . $rstr;
}[/CODE]

But does it in a buggy convoluted way... and it's a terrible idea to begin with, as stated in the comments above just define the paths yourself.

Anyway, if you want to continue with this idea (however bad it may be) here's some constructive criticism based on your code:

- Don't access uninitialized variables (E_NOTICE)

- Don't attempt to include non-existent files (E_WARNING)

- If rooter.php doesn't exist you enter an infinite loop (Fatal)

- Once you find the root path store it so you don't have to repeat the loop every time you call rdot

- rdot and rdotinc repeat the same logic, don't.

  • Like 3
Link to comment
Share on other sites

  • 0

It is a good script for a bit of fun to learn PHP and to play around with functions, while you're still learning PHP.

Really though i think most developers it would be common practice to define this type of data in a config file, use a bootstrapper or something similar.

Even for the smallest of projects.

Also why is your functions code all one line? It offers no performance gain and really makes it difficult for others to read and understand it. Especially with the amount of ifs and whiles you use.

I'm used to compacting my script now and coding it that way. For my professional sites, I usually expand my code.

Some developers don't know how to use includes well. You can seriously use one header include for an entire site that has the rooter include, doc type and other scripts that's needed on every page.

Everyone codes differently and sometimes developers have to remember there's always usually more than one way to do a task. No way is really right or wrong.

Good luck out there fellow developers.

@ZakO,

There's nothing wrong with the script and it's not badly coded. Also, if you take time to read and actually test codes against PHP, you will know that rdot() and rdotinc() are not the same.

Also v1 and v2 are different methods to do the same thing. Kind of like working a math problem two different ways but coming up with the same answer. Neither is better than the other.

Link to comment
Share on other sites

  • 0
There's nothing wrong with the script and it's not badly coded...

Kind of like working a math problem two different ways but coming up with the same answer. Neither is better than the other.

I would disagree, put error_reporting(E_ALL) at the top of your script. It will return several messages (E_NOTICE and E_WARNING which most PHP developers will consider bad) due to accessing uninitialized variables and including non-existent files. Don't get me wrong, I know there's always many ways to achieve the same thing but in some cases one way is better than the other.

The rdotinc(); on the other hand does the same thing, but puts it all in an INCLUDE(). WHY? Because during testing I found that trying to include a function in an INCLUDE() doesn't work. It just echos out text on the page. So, I wrapped the INCLUDE() with the function and everything works as expected.

This seems to show a lack of understanding of the basics of PHP. You absolutely can include the return value of a function inside an include call, the reason it's not working for you is because your rdot function is echoing a value, not returning one.

  • Like 2
Link to comment
Share on other sites

  • 0

I'll get my popcorn.

:laugh:... IRL!

Seriously though, I'd love you to share your comments. You're one of the few people on here whose PHP advice I respect... so I say go for it!

Link to comment
Share on other sites

  • 0

Seriously though, I'd love you to share your comments. You're one of the few people on here whose PHP advice I respect... so I say go for it!

Well... then I shall say this Rooter thing is an overkill not unlike an attempt to shoot yourself in the foot by amputating it with a rusted hacksaw mounted backwards, then supergluing it in front of a Gatling gun, then designing a remote-control enabled Arduino board synced to NTP pool to make a controlled precision shot, and then trying to sew the remains of that foot back using chopsticks, Ethernet cable and a bunch of bread bag gizmos.

ZakO's approach is way more simple, seen in practice, too. Though hammering filesystem repeatedly for every little crud one might want to refer to that way is still very inefficient.

* Generate all necessary paths based on $_SERVER['DOCUMENT_ROOT']

* (Recommended) Cache them (APC) or even store them (hidef)

* Take care not to expose them to the world

* Profit!

Oh, and I work at a convenience store (not unlike Dante Hicks), and just code crap because I hate everything else, so perhaps I really shouldn't be respected.

Link to comment
Share on other sites

  • 0

I've been trying to install something called PPVDemon and it's supposed to work with some license code but when I enter it in it doesn't do anything on my web host is it possible and I got the error located at:

http://fast-profits-...m/ppvdemon/ppv/

What does this error mean and how can I fix it?

Thanks in advanced for any helpful reply.

Create your own thread rather than hijacking others ;)

Link to comment
Share on other sites

  • 0

yeah, this was a bad idea. After working on ASP for awhile, I found a better way of accessing root directories for every site setting up Virtual Hosts in apache :)

Link to comment
Share on other sites

This topic is now closed to further replies.