Manish Basdeo
Manish Basdeo

Reputation: 6269

How to fix "failed to open stream : No such file or directory in zend 2 "?

I am trying small tests on zend framework to se how it works.. I created a module survey. Now, within this controller, I want to include a file that is found one directory above and when i execute this, I get

( ! ) Warning: require(../database.php) [<a href='function.require'>function.require</a>]: failed to open stream: No such file or directory in C:\wamp\www\zend\module\Survey\src\Survey\Controller\SurveyController.php on line 4
Call Stack
#   Time    Memory  Function    Location
1   0.0010  367280  {main}( )   ..\index.php:0
2   0.2738  5658600 Zend\Mvc\Application->run( )    ..\index.php:15
3   0.2780  5685104 Zend\EventManager\EventManager->trigger( )  ..\Application.php:297
4   0.2781  5685112 Zend\EventManager\EventManager->triggerListeners( ) ..\EventManager.php:208
5   0.2784  5686672 call_user_func ( )  ..\EventManager.php:464
6   0.2784  5686688 Zend\Mvc\DispatchListener->onDispatch( )    ..\EventManager.php:464
7   0.2785  5686688 Zend\Mvc\Controller\ControllerManager->get( )   ..\DispatchListener.php:90
8   0.2785  5686840 Zend\ServiceManager\AbstractPluginManager->get( )   ..\ControllerManager.php:114
9   0.2786  5686840 Zend\ServiceManager\ServiceManager->get( )  ..\AbstractPluginManager.php:110
10  0.2787  5687256 Zend\ServiceManager\ServiceManager->create( )   ..\ServiceManager.php:437
11  0.2787  5687288 Zend\ServiceManager\AbstractPluginManager->createFromInvokable( )   ..\ServiceManager.php:491
12  0.2788  5687704 Zend\Loader\StandardAutoloader->autoload( ) ..\ServiceManager.php:0
13  0.2788  5687760 Zend\Loader\StandardAutoloader->loadClass( )    ..\StandardAutoloader.php:217
14  0.2850  5694072 include( 'C:\wamp\www\zend\module\Survey\src\Survey\Controller\SurveyController.php' )  ..\StandardAutoloader.php:306

( ! ) Fatal error: require() [<a href='function.require'>function.require</a>]: Failed opening required '../database.php' (include_path='.;C:\php\pear') in C:\wamp\www\zend\module\Survey\src\Survey\Controller\SurveyController.php on line 4
Call Stack
#   Time    Memory  Function    Location
1   0.0010  367280  {main}( )   ..\index.php:0
2   0.2738  5658600 Zend\Mvc\Application->run( )    ..\index.php:15
3   0.2780  5685104 Zend\EventManager\EventManager->trigger( )  ..\Application.php:297
4   0.2781  5685112 Zend\EventManager\EventManager->triggerListeners( ) ..\EventManager.php:208
5   0.2784  5686672 call_user_func ( )  ..\EventManager.php:464
6   0.2784  5686688 Zend\Mvc\DispatchListener->onDispatch( )    ..\EventManager.php:464
7   0.2785  5686688 Zend\Mvc\Controller\ControllerManager->get( )   ..\DispatchListener.php:90
8   0.2785  5686840 Zend\ServiceManager\AbstractPluginManager->get( )   ..\ControllerManager.php:114
9   0.2786  5686840 Zend\ServiceManager\ServiceManager->get( )  ..\AbstractPluginManager.php:110
10  0.2787  5687256 Zend\ServiceManager\ServiceManager->create( )   ..\ServiceManager.php:437
11  0.2787  5687288 Zend\ServiceManager\AbstractPluginManager->createFromInvokable( )   ..\ServiceManager.php:491
12  0.2788  5687704 Zend\Loader\StandardAutoloader->autoload( ) ..\ServiceManager.php:0
13  0.2788  5687760 Zend\Loader\StandardAutoloader->loadClass( )    ..\StandardAutoloader.php:217
14  0.2850  5694072 include( 'C:\wamp\www\zend\module\Survey\src\Survey\Controller\SurveyController.php' )  ..\StandardAutoloader.php:306

here is the controller file

<?php

namespace Survey\Controller;
(require 'test.php');

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Zend\Config\Config;

class SurveyController extends AbstractActionController
{
    public function indexAction()
    {
         $plugin = $this->plugin('url');

    }

    public function addAction()
    {
    }

    public function editAction()
    {
    }

    public function deleteAction()
    {
    }
}

The same code in the controller, when I put the database.php in the same directory as the controler, works fine. Why it is not allowing me to include one which is one directory above and how to fix this ?

Upvotes: 0

Views: 3046

Answers (1)

Sam
Sam

Reputation: 16455

Please take a look at the index.php of the ZendSkeletonApplication. Take notice of the change directory command which is chdir().

This command sets the working directory of PHP-Scripts to the ROOT of your application. So whenever you want to include a file into your scripts, you gotta link them relatively. In your case this would solve the problem:

include __DIR__ . '/../database.php';

However: going just by the filename i'd say you're doing something that is not really intended. There should be no need for yet another filesystem interaction just to get some database parameters. These kind of information should be put into ./config/autoload/database.local.php

You'll be able to access the params using the array syntax from controller in this fashion:

$config   = $this->getServiceLocator()->get('config');
$dbParams = $config['dbParams'];  // or whatever you name it, default PHP Arrays

Upvotes: 1

Related Questions