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 6832611
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:52:46+00:00 2026-05-26T22:52:46+00:00

So I am trying to include some files in PHP and it’s not working.

  • 0

So I am trying to include some files in PHP and it’s not working.

The file I am writing is in the home directory. There is a subdirectory “D1” in the home directory, and in D1, there is another directory, “D2”.

Inside D2 there are a bunch of files. I wrote a class file and stored it in D1. It looks like this:

<?php
include_once('D2/file1.php');
include_once('D2/file2.php');
//ect

class myClass {
   //class stuff
}
?>

So then I write a file inside the home directory like this:

<?php
include_once('D1/myclass.php');
?>

But when I load this file, I get this:

Warning: include_once(file1.php) [function.include-once]: failed to open stream: No such file or directory in /home/D1/D2/file1.php on line 2

“file1.php” is definitely in that directory, so what am I doing wrong?

  • 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-26T22:52:47+00:00Added an answer on May 26, 2026 at 10:52 pm

    when including a file, the relative location is relative to the script being called which is the “current working directory”. so if script A.php includes ./test/B.php and B.php wanted to include a file under ./test/, it would be relative to A.php. You can create an absolute path using __DIR__ which has the script path and name regardless of where it was included or by using $_SERVER['DOCUMENT_ROOT'] which will point to the document root.

    B.php example:

    <?php
    include(__DIR__.'/C.php');
    //will include C.php under the same directory as B.php
    ?>
    

    the other option is to add D1 to the includes path in php.ini. this tells php to look in that directory for the file when doing an include.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need some help trying to match a C include file with full path
I am trying to include a .js file into a php file. My folder
I am trying to include the Zend_Service_Amazon_S3 file by using require_once 'Zend/Service/Amazon/S3.php'; I have
I'm trying to post some very simple data to a php file using jquery
I'm a bit of a PHP newbie, but I'm trying to put some include
I'm trying to organize my php-project by puting some files into folders. When I
Where would set_include_path(../../); point to? I am trying to run some PHP code on
I'm trying to include a file with all my db connection info (hostname password
I'm trying to include a javascript file in a phmtl view script file using
I'm trying to parse a moderately large XML file (6mb) in php using simpleXML.

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.