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 create new ASP.NET web application that use SMTP to send message. The problem
I am using wordpress and use custom permalink structure: /%category%/%postname%/ My problem is that
I am using VIM in Windows. The problem is that I want to use
The Problem I use a tool at work that lets me do queries and
it seems that I just solved my problem why I couldn't use the ASP.NET
So the crux of the problem, is I get the following error when running
One problem that I come across regularly and yet don't have a solution to
A problem that we need to solve regularly at my workplace is how to
The problem that I am having has to do with the need to keep
I have a problem that confuses my users, being that although an item is

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.