Solved

Function call to the file(s) in other directory

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Magneto product importing through API script 10 32
How do I edit this Drupal page? 9 30
php documentation 4 22
Paging Using PHP 7 34
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

746 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

14 Experts available now in Live!

Get 1:1 Help Now