PHP include ladder to find include path.

Posted on Monday the 10th of September, 2007 at 4:24 pm in Asides, Dev

I needed a way to be able to include particular files from files that are themselves included. because the working directory was not necessarily the same, so I needed a way to find what the actual include path should be. The following code snippet finds a specific folder or file and returns the ladder (relative path) to get to it.

  1. function find_path($loc,$dir=0){
  2. for($i = 0; $i <10; $i++){
  3. if($dir){
  4. if(is_dir($ladder.$loc)){
  5. return $ladder;
  6. break;
  7. }
  8. }elseif(is_file($ladder.$loc)){
  9. return $ladder;
  10. break;
  11. }
  12. $ladder .='../';
  13. }
  14. }
  15. Use the following to copy and paste the code.

This code is NOT user-input safe: any checks you do should be hard coded otherwise you might as well just give out your passwords.

Related posts


Leave a reply

:) :D :( :o 8O :? 8) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: ;) :!: :?: :idea: :arrow: :| :mrgreen: