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
https://www.neowin.net/forum/topic/1075267-rooter-php-root-folder-locator/
Share on other sites

24 answers to this question

Recommended Posts

  • 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.

  • 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

  • 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.

  • 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.

  • 0
  On 15/05/2012 at 21:10, -Alex- said:

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.

  • 0
  On 16/05/2012 at 02:40, viogin said:

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.

  • 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.

  • 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.

  • 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
  • 0
  On 16/05/2012 at 11:03, Lingwo said:

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.

  • 0
  Quote
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.

  Quote
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
  • 0
  On 16/05/2012 at 21:46, -Alex- said:

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.

  • 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-now.comoj.com/ppvdemon/ppv/

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

Thanks in advanced for any helpful reply.

  • 0
  On 18/05/2012 at 01:31, netsurfer802 said:

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 ;)

This topic is now closed to further replies.
  • Posts

    • That's wonderful. A shame that a few fringe nutjobs will somehow concoct a story to make this something nefarious. I do wish he'd committed to using more of the foundation's funds for scientific/medical research here at home, though. It looks like the U.S. is suddenly poised to become a backwoods nation in research.
    • Scientists explain how water could have actually made Mars the red planet it is today by Sayan Sen For centuries, scientists have wondered why Mars is red. The long-standing theory was that the planet’s rusty color came from hematite, an iron-rich mineral formed in dry conditions. But new research suggests something else might be responsible: ferrihydrite, an iron oxide-hydroxide mineral that forms in wet environments. A study published in Nature Communications by researchers from Brown University and the University of Bern suggests that ferrihydrite (Fe5O8H · nH2O) is the dominant iron-containing mineral in Martian dust. Their findings—based on orbital observations, rover data, and lab experiments—challenge previous ideas about Mars' surface composition. “The fundamental question of why Mars is red has been thought of for hundreds if not thousands of years,” said Adomas Valantinas, a postdoctoral fellow at Brown University. “From our analysis, we believe ferrihydrite is everywhere in the dust and also probably in the rock formations, as well.” Ferrihydrite is formed when iron reacts with oxygen and water. On Earth, it’s commonly found in volcanic rock and ash. Its presence on Mars suggests that the planet was once much wetter, with conditions that could have supported liquid water. This contrasts with hematite, which forms in drier environments. To test their theory, the researchers recreated Martian conditions in a lab. They ground minerals into tiny particles—about 1/100th the width of a human hair—matching the size of real Martian dust. They then studied how light reflected off these particles. The results showed that ferrihydrite remains stable in Mars’ current dry, cold climate, but its structure still holds signs that it formed when the planet had water. “What we know from this study is the evidence points to ferrihydrite forming, and for that to happen there must have been conditions where oxygen and water could react with iron,” Valantinas explained. “Those conditions were very different from today’s dry, cold environment.” To confirm ferrihydrite’s presence, the team studied data from NASA’s Mars Reconnaissance Orbiter and ESA’s Mars Express and Trace Gas Orbiter. They also used spectral readings from rovers like Curiosity, Pathfinder, and Opportunity. Combining all of these sources, they found that the mineral appears widespread on the Martian surface. This discovery challenges previous theories that Mars gradually oxidized in dry conditions. Instead, it suggests that ancient Mars went through a wetter phase before drying out. That shift from a water-rich past to the dry, dusty planet we see today is key to understanding Mars’ climate history—and possibly its ability to support life. “The study is a door-opening opportunity,” said Jack Mustard, senior author of the study. “It gives us a better chance to apply principles of mineral formation and conditions to tap back in time.” While the findings provide strong evidence for ferrihydrite’s role in Mars’ red dust, definitive proof will have to wait until Mars samples—currently being collected by NASA’s Perseverance rover—are brought back to Earth. Scientists hope these samples will confirm the theory and shed more light on the planet’s environmental history. Source: Brown University, University of Bren, Nature | Image via Depositphotos This article was generated with some help from AI and reviewed by an editor. Under Section 107 of the Copyright Act 1976, this material is used for the purpose of news reporting. Fair use is a use permitted by copyright statute that might otherwise be infringing.
    • I would say 98% of people you can't figure out how to install Linux would never attempt to install Windows. It's not news non-tech savvy people can't install an PC OS.
    • Do I really have to tell you that people generally don't make random posts about how Windows is running perfectly for them? Of course you're seeing more posts about people hating Windows, it's just a very loud minority as usual.
    • ...and not really appropriate for most people for desktop usage.
  • Recent Achievements

    • Week One Done
      jrromero17 earned a badge
      Week One Done
    • One Month Later
      jrromero17 earned a badge
      One Month Later
    • Conversation Starter
      johnwin1 earned a badge
      Conversation Starter
    • One Month Later
      Marwin earned a badge
      One Month Later
    • One Year In
      fred8615 earned a badge
      One Year In
  • Popular Contributors

    1. 1
      +primortal
      237
    2. 2
      snowy owl
      156
    3. 3
      ATLien_0
      142
    4. 4
      +FloatingFatMan
      132
    5. 5
      Xenon
      131
  • Tell a friend

    Love Neowin? Tell a friend!