Solved

Function call to the file(s) in other directory

Posted on 2004-09-12
2
219 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

912 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now