0

I am new to symfony 2.3 I have a problem in running an independent php file from my controller. I want to run that file from shell_exec() and to use it's output in my code. But i dont know the right way that where to locate that file I want to run. my controller code as follows

namespace my\apiBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;

use Symfony\Component\HttpFoundation\Request;

Class newController extends Controller
{
 public function getlibAction($text)
 {
    $out = shell_exec("php script/data.php");
    /*
    my rest code to take data from output

    */

    $response = new Response();
    $response->headers->set('Content-Type', 'text/html');
    $response->setContent($out);
    return $response;
 }
}

I have copied the "script/" folder containing "data.php" to the "my/apiBundle/Resources/public/" but i could not access it . And I try to copy it independently in "my/apiBundle/" but it was still not successful.

I dont know eher should i locate it. Please help me out of this issue. Thanks in advance..

priyank
  • 148
  • 3
  • 16
  • did you try require_once __DIR__."filename" – cptnk Jun 27 '13 at 07:50
  • yes i tried but still it is still not working.. I used it like "require_once __DIR__.'/../script/data.php'"; and changed the syntax like "$out = shell_exec("php data.php");". But still it is not working. – priyank Jun 27 '13 at 08:02
  • http://rndm-snippets.blogspot.de/2010/09/include-php-file-in-symfony.html This looks like symfony 1.x but maybe that one works for you. – cptnk Jun 27 '13 at 08:09
  • i have added "include __DIR__.'/../script/data.php';" but still it is not working.. – priyank Jun 27 '13 at 10:23
  • did you copy your file into the lib folder of your project? And did u try using use sfConfig::get(); ? It could look like this: include_once sfConfig::get('sf_lib_dir')."/script/data.php"; – cptnk Jun 27 '13 at 11:06

1 Answers1

2

You could obtain the full path of the current file (your controller) with:

dirname(__DIR__)

Based on that you can obtaining the parent directory like:

dirname(dirname(__DIR__))

And so on until you reach the desired path.

As an example, I located the recaptcha lib under a folder named libs inside Resources directory (src/Company/Project/Bundle/Resources/libs/recaptchalib.php) and I require it on my Controller like follows:

require_once dirname(__DIR__).'/Resources/libs/recaptchalib.php';

You could do something similar with the shell_exec command.

Hope it helps.

JAM
  • 323
  • 2
  • 4
  • 14