PHP: File System Basics


/ Published in: PHP
Save to your folder(s)

Here are some ways to figure out what file or directory your script is loading from.


Copy this code and paste it in your HTML
  1. <?php
  2.  
  3. //Provide the file name
  4. echo __FILE__ . "<br />";
  5.  
  6. //What line is this being executed
  7. echo __LINE__ . "<br />";
  8.  
  9. //Provide the directory name
  10. echo dirname( __FILE__ ) . "<br />";
  11.  
  12. //Does this file exist within the directory?
  13. echo file_exists( dirname( __FILE__ ) . "/basic.html" ) ? "yes" : "no";
  14.  
  15. //Find out if the directory exist?
  16. echo file_exists( dirname( __FILE__ ) ) ? "yes" : "no";
  17.  
  18. //Does this file exist?
  19. echo is_file( dirname( __FILE__ ) . "/basic.html" ) ? 'yes' : 'no';
  20.  
  21. //Does this directory exist?
  22. echo is_dir( dirname( __FILE__ ) ) ? 'yes' : 'no';
  23.  
  24. //Determine if this is a directory (version 2)
  25. echo is_dir( '..' ) ? 'yes' : 'no';
  26.  
  27. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.