require_once problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Lars Plessmann

    require_once problem

    I hava several php files in different folders.

    I tried to use relative paths. But when I call a script from another
    folder that has another depth, it cannot find the files that are
    embedded via require once within the included file.

    sample:

    fileA.php (in the root dir!)
    -----
    require_once 'common_inc.php ';
    ....
    -----

    classes/fileB.php (in /classes)
    -----
    require_once '../fileA.php';
    ....
    -----


    No it trys to find the 'common_lib in the /classes folder insted of the
    root dir.


    Well, I tried now to specify absolute paths like
    "http://127.0.0.1:8080/projectname/common_inc.php"
    But it cannot be included then. Thats the browser string of the file.
    Why doesn't it work?


    How can I solve this problem?
    It doesn't matter for me if relative or absolute paths specifications
    are used. It should only work :)


    thanks in advance,
    Lars
  • Pedro Graca

    #2
    Re: require_once problem

    Lars Plessmann wrote:[color=blue]
    > Well, I tried now to specify absolute paths like
    > "http://127.0.0.1:8080/projectname/common_inc.php"
    > But it cannot be included then. Thats the browser string of the file.
    > Why doesn't it work?[/color]

    Use filesystem paths:

    require_once 'C:/htdocs/common_inc.php' ;
    require_once 'C:/htdocs/classes/fileB.php';


    --
    USENET would be a better place if everybody read: : mail address :
    http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
    http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
    http://www.expita.com/nomime.html : to 10K bytes :

    Comment

    • Lars Plessmann

      #3
      Re: require_once problem

      Pedro Graca wrote:
      [color=blue]
      > Lars Plessmann wrote:
      >[color=green]
      >>Well, I tried now to specify absolute paths like
      >>"http://127.0.0.1:8080/projectname/common_inc.php"
      >>But it cannot be included then. Thats the browser string of the file.
      >>Why doesn't it work?[/color]
      >
      >
      > Use filesystem paths:
      >
      > require_once 'C:/htdocs/common_inc.php' ;
      > require_once 'C:/htdocs/classes/fileB.php';
      >
      >[/color]

      problem: I develop on windows and my host is unix.
      how to handle this?

      Comment

      • Pedro Graca

        #4
        Re: require_once problem

        Lars Plessmann wrote:[color=blue]
        > Pedro Graca wrote:
        >[color=green]
        >> Lars Plessmann wrote:
        >>[color=darkred]
        >>>Well, I tried now to specify absolute paths like
        >>>"http://127.0.0.1:8080/projectname/common_inc.php"
        >>>But it cannot be included then. Thats the browser string of the file.
        >>>Why doesn't it work?[/color]
        >>
        >>
        >> Use filesystem paths:
        >>
        >> require_once 'C:/htdocs/common_inc.php' ;
        >> require_once 'C:/htdocs/classes/fileB.php';
        >>
        >>[/color]
        >
        > problem: I develop on windows and my host is unix.
        > how to handle this?[/color]

        Maybe like this (PHP 5)?


        <?php
        if (stripos(PHP_OS , "win") === false) {
        require_once '/htdocs/common_inc.php' ;
        } else {
        require_once 'C:/htdocs/common_inc.php' ;
        }
        ?>


        stripos() is available from PHP 5

        PHP_OS is a core predefined constant
        PHP is a popular general-purpose scripting language that powers everything from your blog to the most popular websites in the world.




        if you don't have PHP 5 try:

        <?php
        function my_stripos($hay stack, $needle, $offset=0) {
        return strpos(strtoupp er($haystack), strtoupper($nee dle), $offset);
        }
        ?>

        --
        USENET would be a better place if everybody read: : mail address :
        http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
        http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
        http://www.expita.com/nomime.html : to 10K bytes :

        Comment

        • Garp

          #5
          Re: require_once problem


          "Lars Plessmann" <Lars.Plessmann @gmx.de> wrote in message
          news:40c4d9ad$0 $26367$9b4e6d93 @newsread4.arco r-online.net...[color=blue]
          > I hava several php files in different folders.
          >
          > I tried to use relative paths. But when I call a script from another
          > folder that has another depth, it cannot find the files that are
          > embedded via require once within the included file.
          >
          > sample:
          >
          > fileA.php (in the root dir!)
          > -----
          > require_once 'common_inc.php ';
          > ...
          > -----
          >
          > classes/fileB.php (in /classes)
          > -----
          > require_once '../fileA.php';
          > ...
          > -----
          >
          >
          > No it trys to find the 'common_lib in the /classes folder insted of the
          > root dir.
          >
          >
          > Well, I tried now to specify absolute paths like
          > "http://127.0.0.1:8080/projectname/common_inc.php"
          > But it cannot be included then. Thats the browser string of the file.
          > Why doesn't it work?
          >
          >
          > How can I solve this problem?
          > It doesn't matter for me if relative or absolute paths specifications
          > are used. It should only work :)
          >
          >
          > thanks in advance,
          > Lars[/color]

          $_SERVER["DOCUMENT_R OOT"] should give you the top-level document root
          ("/var/www/html" or "c:\inetpub\www root", say) - if your include storage is
          consistent, you could trust that.

          Garp


          Comment

          • CHARA=>TOMeK

            #6
            Re: require_once problem

            > I hava several php files in different folders.[color=blue]
            >
            > I tried to use relative paths. But when I call a script from another
            > folder that has another depth, it cannot find the files that are
            > embedded via require once within the included file.
            >
            > sample:
            >
            > fileA.php (in the root dir!)
            > -----
            > require_once 'common_inc.php ';
            > ...
            > -----
            >
            > classes/fileB.php (in /classes)
            > -----
            > require_once '../fileA.php';
            > ...
            > -----
            >[/color]

            my idea:
            -----
            require_once dirname(__FILE_ _).'../fileA.php';
            -----

            dirname(__FILE_ _)." relative paths ". " file ";

            --
            CHARA=>TOMeK


            Comment

            • CHARA=>TOMeK

              #7
              Re: require_once problem

              > my idea:[color=blue]
              > -----
              > require_once dirname(__FILE_ _).'../fileA.php';
              > -----
              >
              > dirname(__FILE_ _)." relative paths ". " file ";[/color]

              This is good:
              -----
              require_once dirname(__FILE_ _).'/'.'../fileA.php';
              -----

              dirname(__FILE_ _).'/'." relative paths ". " file ";

              Pozdrawiam,
              CHARA=>TOMeK

              Comment

              • Virgil Green

                #8
                Re: require_once problem

                "Lars Plessmann" <Lars.Plessmann @gmx.de> wrote in message
                news:40c4d9ad$0 $26367$9b4e6d93 @newsread4.arco r-online.net...[color=blue]
                > I hava several php files in different folders.
                >
                > I tried to use relative paths. But when I call a script from another
                > folder that has another depth, it cannot find the files that are
                > embedded via require once within the included file.
                >
                > sample:
                >
                > fileA.php (in the root dir!)
                > -----
                > require_once 'common_inc.php ';
                > ...
                > -----
                >
                > classes/fileB.php (in /classes)
                > -----
                > require_once '../fileA.php';
                > ...
                > -----
                >
                >
                > No it trys to find the 'common_lib in the /classes folder insted of the
                > root dir.[/color]

                What was the current directory when you tried this? What was the
                include_path setting when you tried this?
                [color=blue]
                > Well, I tried now to specify absolute paths like
                > "http://127.0.0.1:8080/projectname/common_inc.php"
                > But it cannot be included then. Thats the browser string of the file.
                > Why doesn't it work?[/color]

                You use file system paths, not URLS
                [color=blue]
                > How can I solve this problem?
                > It doesn't matter for me if relative or absolute paths specifications
                > are used. It should only work :)[/color]

                We can't really say until you tell us about your include path and identify
                the directory in which the script initially started.

                - Virgil


                Comment

                Working...