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 [email protected]
// 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

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

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

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

  • 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

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

    • Got it. Seem like a good improvement. Thanks. But, before we "Add the source to Trusted Sites," how would know if it is trusted or not? Could get messy for not first Knowing" it is clean. See what I mean?
    • AB Download Manager 1.9.0 by Razvan Serea AB Download Manager is an open-source, feature-rich download manager designed to accelerate downloads, organize files efficiently, and provide seamless control over downloads. With support for multiple connections, resume capability, and an intuitive interface, it enhances the downloading experience for users seeking speed and reliability. The software integrates with various browsers, enabling quick link grabbing and batch downloading. It supports HTTP, HTTPS, and FTP protocols, ensuring broad compatibility with different file sources. Users can schedule downloads, set speed limits, and categorize files automatically for better organization. AB Download Manager is lightweight yet powerful, making it a great alternative to proprietary download managers. Its open-source nature allows developers to contribute, customize, and improve the software as needed. Whether you're downloading large files, managing multiple downloads at once, or seeking an ad-free experience, this tool offers a practical and efficient solution. Key features of AB Download Manager: Multi-Connection Support – Accelerates downloads by splitting files into multiple segments. Resume Capability – Allows paused or interrupted downloads to be resumed without starting over. Batch Downloading – Supports downloading multiple files at once for improved efficiency. Browser Integration – Captures download links directly from browsers for seamless operation. HTTP, HTTPS, and FTP Support – Ensures compatibility with a wide range of file sources. Download Scheduling – Enables users to automate downloads at specific times. Speed Limiting – Lets users control bandwidth usage for optimized performance. File Categorization – Automatically organizes downloaded files into designated folders. User-Friendly Interface – Simple and intuitive design for easy navigation. Cross-Platform Compatibility – Works on multiple operating systems. Ad-Free Experience – No intrusive ads or tracking for a clean user experience. AB Download Manager 1.9.0 changelog: Added Czech language support User-friendly error messages for download errors (#1252) An option to remember the last selected queue and quickly add downloads to it by long-clicking the Add button ( #1246) An option to export/import downloads using JSON format A Download button on the multi-download page for cases where users do not want to start downloads without queue processing (#1247) The app now includes a logger that can be enabled using a command-line flag (#1226) Startup errors are now logged automatically to help diagnose initialization issues Changed The default unqueued "Max Concurrent Downloads" value has been changed from "Unlimited" to 3 (This can be customized in the app settings) Improved Updated translations Added an indicator on the Android main page when resume is not supported (#1248) Extract the file name from the download link as a fallback when no response information is available (#1209) Minor UI/UX improvements Download: AB Download Manager 1.9.0 | Portable | ~80.0 MB (Open Source) Download: ARM64 | Portable ARM64 | Android Links: AB Download Manager Website | Github Page | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • I just started using Teams (and “New” Oiutlook) at work (instead of Slack) and both are truly abysmal pieces of software.
  • Recent Achievements

    • First Post
      DragonOfMercy earned a badge
      First Post
    • First Post
      bella52 earned a badge
      First Post
    • Reacting Well
      Techinmay earned a badge
      Reacting Well
    • Collaborator
      WndSks went up a rank
      Collaborator
    • One Month Later
      Sopa flores earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      489
    2. 2
      PsYcHoKiLLa
      206
    3. 3
      +Edouard
      152
    4. 4
      Steven P.
      85
    5. 5
      ATLien_0
      71
  • Tell a friend

    Love Neowin? Tell a friend!