Jump to content

file directory


phpgoal

Recommended Posts

Hi,

 

I am using file zilla to upload file.

 

I have files as below:

 

folder1

  folder1file1.php

  foder2

    folder2file.php

    folder3

       folder3file.php

 

Now i am working with folder3file.php. In php, i want to include folder1file.php. How can i do it?

 
<?php 
'include 'folder1/folder1file1.php'; ?>

not working. Could you please help?

 

-P

Link to comment
Share on other sites

Use absolute paths whenever possible, either using the DOCUMENT_ROOT or __DIR__.

// works regardless of where you are
include $_SERVER["DOCUMENT_ROOT"] . "/folder1/folder1file1.php";
// have to know the relative path between the current file and the one you want
include __DIR__ . "/../../folder1file1.php";
Link to comment
Share on other sites

I and many others would not do this.  I would use a file in the top most directory to control the application and include other files.  However, to do this you either need to give the relative path from where you are, or the absolute path:

//two ways for relative
include('../../folder1file1.php');
include('../../../folder1/folder1file1.php');

//example absolute
include('/var/www/vhosts/domain/httpdocs/folder1/folder1file1.php');
Link to comment
Share on other sites

Maybe what you'll want to do is just change the folder PHP includes files from?

 

If you're running a PHP file in folder1/folder2/folder3 then PHP is looking for files from folder3's location, which I doubt is what you really want. You probably want to change the running directory to the folder that your folder1 is in. The function for that is chdir().

 

http://php.net/manual/en/function.chdir.php

 

 

 

so if you're 3 folders up from where you want to be, chdir() down 3 folders:

 

chdir('../../../');

 

 

Now your includes should all work the way you wanted on your post:

 

include 'folder1/folder1file1.php';

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.