Solved

Function call to the file(s) in other directory

Posted on 2004-09-12
2
222 Views
Last Modified: 2010-08-05
Hi,
While implementing the solution to my previous question (http://www.experts-exchange.com/Web/Web_Languages/PHP/Q_21127951.html),
I found out that I need to make a function call to the file(s) in other directory.
So, 2 questions:
1. If I have a function (see below), how to get the file stored in a directory different than one initiated by the function call?
Say, I have a file 'content_page1' stored in 'www/1/htdocs/a/b/c/d/e', and I make a function call from a file stored in 'www/1/htdocs/a'.
2. Is this path in 'include' is in a correct format?
Thanks!
Le

<HTML>
<BODY>
<!-- Function getContent -->
<?php
include '/www/1/htdocs/myfuncs.inc.php'; <----Correct?
?>                        
                     
            <!-- CONTENT -->
            <?php getContent(); ?>
</BODY>
</HTML>

myfuncs.php:
<?php

    function getContent() {
        if (empty($_GET['page'])) {
            $page = 'content_page1';
        } else {
            $page = $_GET['page'];
        }
        $page .= '.php';
         
        if (false == is_file($page)) {
            $page = 'file_not_found.php';
        }
        include($page);
    }
0
Comment
Question by:athanasius296
2 Comments
 
LVL 7

Assisted Solution

by:aib_42
aib_42 earned 50 total points
ID: 12041075
> include '/www/1/htdocs/myfuncs.inc.php'; <----Correct?

This means that you have a "www" directory in your directory root (ie /www for *nix or c:\www for windows)
And that has a subdirectory called "1", that has one called "htdocs", etc... It looks correct to me.

The thing to be careful about is giving the file's true path on the server filesystem, not the web server. (ie not "www.mysite.com/somedir/something.php" or "/somedir/something.php".

I do not understand your first question very well, but this might answer it:
include()'ing a file is, effectively, copying all the lines in the file being included, and pasting them right on the line with the include() call.
0
 
LVL 48

Accepted Solution

by:
hernst42 earned 75 total points
ID: 12041584
Yes you can inculde pages in other directories. For that use a switch as pointed out in the other question.:

      switch ($_GET['page']) {
          case("content_page2"):
               $page = 'b/c/d/content_page2.php';
          case("content_page3"):
               $page = 'b/c/content_page3.php';
          default:
               $page = 'content_page1.php';
      }
        if (false == is_file($page)) {
            $page = 'file_not_found.php';
        }
        include($page);

So you can easyly configure where the included files should come from.
0

Featured Post

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This article discusses four methods for overlaying images in a container on a web page
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question