?
Solved

Function call to the file(s) in other directory

Posted on 2004-09-12
2
Medium Priority
?
238 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 7

Assisted Solution

by:aib_42
aib_42 earned 200 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 300 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
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 …
Suggested Courses

762 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