Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 457607
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T22:35:17+00:00 2026-05-12T22:35:17+00:00

The problem is that I use the method file _ get _ contents(body.html) in

  • 0

The problem is that I use the method file _ get _ contents("body.html") in a class that is in the same folder as body.html. The problem is that I get an error saying that the file could not be found. This is because I from another class require the file that uses the method file _ get _ contents("body.html"), and suddenly I have to use “../body/body.html” as filepath..!

Isn’t that a bit strange? The class that calls the method file _ get _ contents("body.html") is in the same folder as body.html, but because the class is required from another class somewhere else, I need a new filepath?!

Here’s a short list over directories and files:

lib/main/main.php
lib/body/body.php
lib/body/body.html

This is body.php:

class Body {

 public function getOutput(){
  return file_get_contents("body.html");
 }
}

This is main.php:

require '../body/body.php';

class Main {

 private $title;
 private $body;

 function __construct() {

  $this->body = new Body();
 }

 public function setTitle($title) {
  $this->title = $title;
 }

 public function getOutput(){
  //prints html with the body and other stuff.. 
 }
}

$class = new Main();
$class->setTitle("Tittel");
echo $class->getOutput();

The thing I ask for is a fix on the error that body.php is in the same folder as body.html but I have to change the path when a another class requires body.php from somewhere else in the method file _ get _ contents("body.html")

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T22:35:17+00:00Added an answer on May 12, 2026 at 10:35 pm

    The scope of PHP’s file-based functions always starts at the point of the first file in the execution stack.

    If index.php is requested, and includes classes/Foo.php which in turn needs to include ‘body/body.php’, the file scope will be that of index.php.

    Essentially, the current working directory.

    You have some options, though. If you want to include/open a file in the same directory as the current file, you can do something like this

    file_get_contents( dirname( __FILE__ ) . '/body.html' );
    

    Or, you can define a base directory in a constant and use that for your inclusion

    define( 'APP_ROOT', '/path/to/app/root/' );
    file_get_contents( APP_ROOT . 'lib/body/body.html' );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a HTML form where I use several buttons. The problem is that
In a couple of scripts that I use I have problem that is intermittent.
Rather odd problem in that I cannot use the data variable (the information returned
I'm having a problem when running programs that use Java from the command line.
I find I've been confused by the problem that when I needn't to use
My problem is that I was looking for way to use both storyboard and
I guess the problem is that I do not know how to use factory
I need to use something like get_or_create() but the problem is that I have
Ive got this problem: Line 20 (LOOT_FromContainer(container) I need that to use as Invoke
I use Spring MVC [version: 2.5] and Security[version: 2.0.4]. My problem looks like that:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.