0

I just uploaded my project to my server in var/www made using slim framework the project has a folder structure like:

include -Functions.php controller -weeklysummary.php(Script I want to run) vendor -autoload.php

When I try to call a function in the Functions.php script On my local machine the project worked fine but on the server it keeps telling me `

 Warning: require(../vendor/autoload.php): failed to open stream: No such file or directory in /var/www/html/project/include/Functions.php on line 11

 Fatal error: require(): Failed opening required '../vendor/autoload.php' (include_path='.:/usr/share/pear:/usr/share/php') in /var/www/html/innov8alert/include/Functions.php on line 11`

This is how Functions.php Looks like

class Functions {

private $conn;


function __construct() {
    require_once 'Connect.php';
    require '../vendor/autoload.php';
    require '../mailer/class.phpmailer.php';
    $db = new Connect();
    $this->conn = $db->connect();
}

function __destruct() {

}

I looked up this and this but none seemed to help.

Rando roxford
  • 141
  • 2
  • 10
  • Did you run `composer install` on the server or did you upload the `vendor`-folder? If not, you need to do either or. – M. Eriksson Nov 14 '17 at 14:37
  • Either the file `/var/www/html/project/include/vendor/autoload.php` doesn't exist or the directory/file has the wrong permissions. – bassxzero Nov 14 '17 at 14:37
  • You could also use defines like: `define('BASE_PATH', dirname(__FILE__));` So you can change the entire url to something like this: `BASE_PATH _ . '/include/vendor/autoload.php'` – Ronnie Oosting Nov 14 '17 at 14:39
  • @MagnusEriksson I just uploaded, even the local project I have I never used composer, i just copied and pasted slim and it worked. – Rando roxford Nov 14 '17 at 14:51
  • @bassxzero it has to be in `html`? its in `www` this is my first time uploading a project to a real server – Rando roxford Nov 14 '17 at 14:51
  • Have you tried changing `/` to `\ `? Maybe that helps? – SourceOverflow Nov 14 '17 at 14:52
  • @RonnieOosting worth taking not, let me try this, is the ` _ ` needed or an error? – Rando roxford Nov 14 '17 at 14:53
  • @SourceOverflow won't that mean in the current directory? `..` tells php to look in the directory before the current one which is where my file is – Rando roxford Nov 14 '17 at 14:53
  • My comment had a markdown-formating error, sorry. I fixed it – SourceOverflow Nov 14 '17 at 14:56
  • To use the defined path I suggest to create `defines.php` and create `define('BASE_PATH', dirname(__FILE__));`. Include this file on top of your page. Then `echo BASE_PATH`; and see the result – Ronnie Oosting Nov 14 '17 at 15:11

1 Answers1

0

This is due to your current working directory (CWD), which matches the one where you've started the script from. If this is a website, it should be safe to assume that CWD is where the index.php file is located.

There's 2 ways around the issue:

1) As suggested in the comments, declare a BASEPATH constant from inside index.php and always use it to prefix include paths:

// index.php:
define('BASEPATH', __DIR__.'/');

// other files:
// require BASEPATH.'path/relative/to/index.php/directory

2) Use the __DIR__ magic constant itself to prefix includes on files, but relative to where you include them from (this will immediately work in your case without other changes):

require __DIR__.'/../vendor/autoload.php';
Narf
  • 14,600
  • 3
  • 37
  • 66