XF 2.0 Permission denied when trying to include a file

grantus

Active member
I'm trying to include a file I have from another folder but I keep getting "permission denied". I tried both 777 and 755 for my "includes" folder but still I get the same error.

This is what I have:

Code:
<?php
namespace Test;

class Listener
{

  public static function getHtml()
  {
    include('/path/to/includes/test.php');
    return $output;
  }

}

I also tried putting the test.php file inside my addon folder and put this:

Code:
include('/path/to/src/addons/Test/test.php')

And it didn't work.

When I put this (calling the test file from the addons/Test folder):

Code:
include('test.php')

It worked. What could be causing this?
 
The last one worked because your addons/Test folder is the current working directory.

Without seeing the actual paths you used in the other tries I would guess the ones you tried are not including the full server path to them.

Use the XF file system and canonicalize the path so the full path on your sever is used. ie: /var/www/clients/client0/http_docs/your/file/path

Code:
use XF\Util\File;

.....

$path = FILE::canonicalizePath('your/file/path');
 
What is the full error?

Code:
ErrorException: [E_WARNING] include(/home/testsite/public_html/includes/test.php): failed to open stream: Permission denied in src/addons/Test/Listener.php at line 11

    XF::handlePhpError() in src/addons/Test/Listener.php at line 11
    Test\Listener::getHtml() in src/addons/Test/Listener.php at line 11
    Test\Listener::getHtml()
    call_user_func_array() in src/XF/Pub/Controller/Page.php at line 58
    XF\Pub\Controller\Page->actionIndex() in src/XF/Mvc/Dispatcher.php at line 249
    XF\Mvc\Dispatcher->dispatchClass() in src/XF/Mvc/Dispatcher.php at line 89
    XF\Mvc\Dispatcher->dispatchLoop() in src/XF/Mvc/Dispatcher.php at line 41
    XF\Mvc\Dispatcher->run() in src/XF/App.php at line 1879
    XF\App->run() in src/XF.php at line 328
    XF::runApp() in index.php at line 13


The last one worked because your addons/Test folder is the current working directory.

Without seeing the actual paths you used in the other tries I would guess the ones you tried are not including the full server path to them.

Use the XF file system and canonicalize the path so the full path on your sever is used. ie: /var/www/clients/client0/http_docs/your/file/path

Yes, I'm using the full path, it's: /home/testsite/public_html/includes/

So that's why I don't see how it would be an issue.
 
Are you certain that the internal_data and/or data files are all writable by the PHP user?
 
The last one worked because your addons/Test folder is the current working directory.

Without seeing the actual paths you used in the other tries I would guess the ones you tried are not including the full server path to them.

Use the XF file system and canonicalize the path so the full path on your sever is used. ie: /var/www/clients/client0/http_docs/your/file/path

Code:
use XF\Util\File;

.....

$path = FILE::canonicalizePath('your/file/path');
Sorry, I didn't see your code with the previous reply.

Would I put that code inside my Listener? I'm new to 2.0 so I'm just getting started trying to figure it out.

Are you certain that the internal_data and/or data files are all writable by the PHP user?
Yes, both are 777.
 
Would I put that code inside my Listener? I'm new to 2.0 so I'm just getting started trying to figure it out.
Code:
<?php
namespace Test;

use XF\Util\File;

class Listener
{

  public static function getHtml()
  {
    $path = FILE::canonicalizePath('path/to/includes/test.php');
    include $path;
    return $output;
  }

}

While providing the example above I noticed something I didn't see before. The included file should not be wrapped in parenthesis () which could be your entire problem. It should be just include '/path/to/includes/test.php';

If you still get permission errors, then they are just that, permission errors that you need to track down on your server.

EDIT: If the includes directory is in the same folder as XF, you can just use a relative path "includes/test.php' for the include... include 'includes/test.php'; I know for a fact that works because I use it in at least one of my add-ons. ;)
 
Last edited:
@Snog About the brackets for include, I used that in version 1 without issues, I never imagined that would be an issue in version 2, weird.

So I tried it without the brackets and it was the same error.

I then just used 'includes/test.php' and it works! I have xenforo in my root directory so the includes folder is in there.

Thanks for the help.

EDIT:

This is how it looks now for those that might need this for reference:

Code:
<?php
namespace Test;

class Listener
{

  public static function getHtml()
  {
    include 'includes/test.php';
    return $output;
  }

}
 
Interesting because include was never meant to be used with parenthesis...

Glad to hear it's working now. :)
 
Back
Top Bottom