Save file at the same place as an included script

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • luttkens
    New Member
    • Jul 2007
    • 23

    Save file at the same place as an included script

    I've got a script to access my database called mysql.class.php. So from every other script that is talking to the database, this file i included, like this for instance:

    [PHP]<?php
    include("../classes/mysql.class.php ");
    //Code using the included file goes here
    ?>[/PHP]

    Now I want the file mysql.class.php to save a log-info for every query in a file that is placed in the same folder as the mysql.class.php-file. However, the file is saved where the script which included mysql.class.php is located.

    How can I always save the file at the same place as mysql.class.php, even though it's included in another folder?
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    Can you not just point to the folder the same way you do with the include?

    Comment

    • Atli
      Recognized Expert Expert
      • Nov 2006
      • 5062

      #3
      Personally, I would probably just set a constant that is pointed to the absolute location of the directory.

      For example, if this were my index.php file:
      [code=php]
      <?php
      // The absolute path where the index file is
      define("LOC_ROO T_DIR", "/path/to/my/project/");

      // The absolute path to the class directory
      define("LOC_CLA SS_DIR", LOC_ROOT_DIR . "classes/");

      // Include the file.
      include(LOC_CLA SS_DIR ."class.mysql.p hp");

      // Do whatever else you want with it here...
      ?>
      [/code]
      And in my "class.mysql.ph p" file, I might do:
      [code=php]
      <?php
      // After whatever you want logged, you could do:
      file_put_conten ts(LOC_CLASS_DI R . "log.txt", $logText);
      ?>
      [/code]
      Now, all you have to do is change the LOC_ROOT_DIR constant whenever you set this up on a new server (which usually doesn't happen that often) and you can use it in any script without having to worry about where the script is actually located.

      Comment

      • luttkens
        New Member
        • Jul 2007
        • 23

        #4
        Since I include the file from different locations, I can't do it the same way I do with the include...All paths are realative.

        I could use a constant, but that's trouble when I'm uploading the site - and I do that quite often to fix bugs...

        Comment

        Working...