简体   繁体   中英

Failed to open stream using composer autoload

I just included in the probject folder the phpmailer through composer running this command: composer require phpmailer/phpmailer inside the folder: application/assets/

I included in the index.php file the following command:

include 'application/assets/vendor/autoload.php';

but unfortunately I get this error:

Warning: include(application/assets/vendor/autoload.php): failed to open stream: No such file or directory in /var/www/html/projects/MyApp/index.php on line 74

this is the tree view of my application:

>MyApp
    >application  
       >assets
           >vendor
               >autoload.php
    >system  
    >index.php

inside the vendor folder I've composer and phpmailer folder, and if I include manually phpmailer class I doesn't get any error, this happen only when I include the composer autoload.

This is the content of autoload.php :

<?php

// autoload.php @generated by Composer

require_once __DIR__ . '/composer/autoload_real.php';

return ComposerAutoloaderInit13c998efcdd189d437d19150f7ef3bc9::getLoader();

I'm not sure if the installation in the subfolder /application/assets/ is a must, but i'd suggest using the standard folder layout:

Step1 Create a composer.json next to your index.php

    \- MyApp
       \- application  
       \- system  
       \- index.php
       \- composer.json

Then add phpmailer/phpmailer into the require section of this composer.json file:

{
    "require": {
        "phpmailer/phpmailer": "^5.2"
    }
}

Then run composer update .

(Alternatively, you could also run composer require phpmailer/phpmailer in the MyApp folder. It will automatically generate the composer.json file for you)

The dependencies will be fetched into the folder: vendors , that means your folder structure looks like this now:

\ MyApp
  \ application  
  \ system 
  \ vendor          <-- new folder with composer autoloaders and dependencies
  \ index.php
  \ composer.json

Step2 Finally, add the Composer Autoloader to your index.php

    require __DIR__ . '/vendor/autoload.php';

That's it... start using your dependency: $mail = new PHPMailer;

Referencing:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM