-2

After installing a Composer package, I am trying to include the autoloader and run the script:

require __DIR__ . 'vendor/autoload.php';

At this point I get the following messages:

Warning: require(): open_basedir restriction in effect. File(/home/admin/domains/example.com/private_html/map/mt940/_example/vendor/autoload.php) is not within the allowed path(s): (/home/admin/:/tmp:/var/tmp:/opt/alt/php72/usr/share/pear/:/dev/urandom:/usr/local/lib/php/:/usr/local/php72/lib/php/) in /home/admin/domains/example.com/private_html/map/mt940/_example/example1.php on line 6

Warning: require(/home/admin/domains/example.com/private_html/map/mt940/vendor/autoload.php): failed to open stream: No such file or directory in /home/admin/domains/example.com/private_html/map/mt940/_example/example1.php on line 6

Fatal error: require(): Failed opening required '/home/admin/domains/example.com/private_html/map/mt940/vendor/autoload.php' (include_path='.:/usr/local/lib/php') in /home/admin/domains/example.com/private_html/map/mt940/_example/example1.php on line 6

Am I missing a step? Why do I get this error message?

Update 1:

After disabling open_basedir I still get the following error messages:

Warning: require(/home/admin/domains/example.com/private_html/map/mt940/vendor/autoload.php): failed to open stream: No such file or directory in /home/admin/domains/example.com/private_html/map/mt940/_example/example1.php on line 6

Fatal error: require(): Failed opening required '/home/admin/domains/example.com/private_html/map/mt940/vendor/autoload.php' (include_path='.:/usr/local/lib/php') in /home/admin/domains/example.com/private_html/map/mt940/_example/example1.php on line 6

yivi
  • 42,438
  • 18
  • 116
  • 138
John
  • 904
  • 8
  • 22
  • 56
  • 1
    Are you sure the PATH is correct? – BadPiggie Nov 17 '19 at 05:03
  • You mean: `include_path='.:/usr/local/lib/php'`? I did not define this path – John Nov 17 '19 at 05:05
  • If you do an `echo __DIR__ . '/vendor/autoload.php';` in your script and check the path, does the file exist? – Mischa Nov 19 '19 at 22:06
  • Did you check it with your hosting provider? check this [answer](https://stackoverflow.com/questions/1846882/open-basedir-restriction-in-effect-file-is-not-within-the-allowed-paths) if your hosting provider is using Plesk for more details. If they're not using Plesk, you can still check how to do the same from your hosting provider. – Tuhin Nov 20 '19 at 10:28
  • is there a way for you to try relative path, something like `require '../vendor/autoload.php'`, depending on your project file structure? – Ante Braovic Nov 20 '19 at 11:06
  • Your latest update says you've resolved the open_basedir issue, but your error message still indicates you're using `dirname()`, as I explained in my answer 3 days ago. – miken32 Nov 20 '19 at 20:15
  • @John Where is vendor directory? Could you provide the image of directories. – Dmitry Leiko Nov 21 '19 at 11:55
  • A bunch of us are asking questions. No feedback? – miken32 Nov 22 '19 at 14:42

4 Answers4

1

Your question is missing vital information, which is the directory structure of your project.

We can try to guess it from your error messages.

The directory /home/admin/domains/example.com/private_html/map/mt940/_example/ should contain:

composer.json
composer.lock
vendor/
example1.php
...

If this is correct, the current require __DIR__ . 'vendor/autoload.php'; should work.

Your previous example require dirname(__DIR__) . 'vendor/autoload.php'; would work if your file example1.php resided on directory deeper. E.g.

composer.json
composer.lock
vendor/
something/example1.php // <-- notice it's on deeper level
...
yivi
  • 42,438
  • 18
  • 116
  • 138
0

Are you sure the (Linux?) user used by PHP can read files into /home/admin?

  • You can test by appending <?= get_current_user() ?> into your main script (example1.php). Then call it in your browser (via Apache/Nginx/etc).
  • You can touch a "test.php" with the same rights like example1.php, then append require __DIR__ . 'test.php';, you should get the same error.

Configure your rights for /home/admin/domains or move your source to a directory like /var/www/ (and check the rights)

C Würtz
  • 856
  • 9
  • 20
0

From your error messages, it seems to be a combined error. You are searching with the wrong file path and you don't have read/write access for some of the files. You need to make sure that you are using the correct path and that you have access to the files as the Apache user.

It is a common problem with generated projects. Sometimes the files and folders don't have the appropriate system permissions. Walk through the files and check for rogue permissions. You want to have both read/write privileges to most files.

John Krit
  • 544
  • 1
  • 6
  • 20
0

Steps I would take to resolving issues with include and require: ###

  1. Check if the required files actually exist

I know this sounds stupid but you do not want to know how often I made an error in the required path causing me trouble

  1. Check if the file permissions are correct

Permissions in linux can be quite a handful. Make sure everyting is setup correctly.

  1. Check if the user running your webserver is the correct user and has the right permissions

Apache, nginx and other webservers all have their own users executing the webserver. This depends on your installation and configuration of course. Make sure that your webserver is executed by the right user, that user has the right permissions.

  1. Make sure the include_path is correct

If a package messes up your include path, make sure to correct it.

Peter
  • 8,776
  • 6
  • 62
  • 95