Viewed   83 times

The question is as follows:

How can I get the server path to the web directory in Symfony2 from inside the controller (or from anywhere else for that reason)

What I've already found (also, by searching here):

This is advised in the cookbook article on Doctrine file handling

$path = __DIR__ . '/../../../../web';

Found by searching around, only usable from inside the controller (or service with kernel injected):

$path = $this->get('kernel')->getRootDir() . '/../web';

So, is there absolutely no way to get at least that 'web' part of the path? What if I, for example, decided to rename it or move or something?

Everything was easy in the first symfony, when I could get like everything I needed from anywhere in the code by calling the static sfConfig::get() method..



There's actually no direct way to get path to webdir in Symfony2 as the framework is completely independent of the webdir.

You can use getRootDir() on instance of kernel class, just as you write. If you consider renaming /web dir in future, you should make it configurable. For example AsseticBundle has such an option in its DI configuration (see here and here).

Tuesday, August 30, 2022

You need to give the array_shift() the parameter! Look this example:

$stack = array("orange", "banana", "apple", "raspberry");
$fruit = array_shift($stack); // Here you give the parameter

You give the null parameter on array_shift() and you need to change it!


array_shift() shifts the first value of the array off and returns it, shortening the array by one element and moving everything down. All numerical array keys will be modified to start counting from zero while literal keys won't be touched. Read here for more

Wednesday, October 19, 2022

file_put_contents creates the file if it doesn't exist, but it fails if you try to put the file in a directory that doesn't exist. So you should try the following:

  • check if the images directory exists
  • check the write permissions of the directory
  • try with an absolute path, so in your case probably $target_dir = '/home/ragiththomas/Sites/asco-forum/images/';
Friday, December 9, 2022

Simply opening and reading directories does not change the current working directory. However, changing directory in your program will.

for reference,

#include <unistd.h>
#include <stdio.h>

int main() {
    char cwd[1024];
    getcwd(cwd, sizeof(cwd));
    printf("Current working dir: %sn", cwd);
Sunday, October 9, 2022

no way except debug_backtrace

function whatever() {
    $t = debug_backtrace();
    echo "called from {$t[0]['file']}";
Saturday, September 24, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :