realpath() function does not accept variable

Asked

Viewed 78 times

1

You guys, when I put this on:

$rootPath = realpath("/home/687_332_0/332");
echo $rootPath;

The above example will print: 687_332_0/332 (WORKS!)

However, when I put what’s below, echo does not work:

$rootPath = realpath("/home/".$folder1."/".$folder2);
ou $rootPath = realpath('/home/'.$folder1.'/'.$folder2);
ou $rootPath = realpath("/home/$folder1/$folder2");

ou o mesmo processo com essas variáveis convertidas com strval() ou (string)
$caminho1 = strval($folder1);
$caminho2 = (string)$folder2;

echo $rootPath;

The above example will print: ANYTHING!

Why it happens and how I fix it?

  • I could check if when accessing this folder php code has privileges for this?

  • Yes, you do. Okay.

  • 1

    In the example you are using, $folder1 and $folder2 have what values? These variables may possibly have strange characters like spaces, accents, special symbols, ...?

  • 687_332_0 and 332, as in the question.

1 answer

1


A little clarification on the function realpath: It will return FALSE should the path of pasta or arquivo does not exist.

Example:

var_dump(realpath('nao_existe/essa_pasta')); // bool(false)

I believe the most appropriate way to ride the path of path would be:

$rootPath = "/home/".$folder1."/".$folder2;

if (! is_dir($rootPath)) {
  // Faça alguma coisa se essa parta não existir
}

// ... Continua o código

Browser other questions tagged

You are not signed in. Login or sign up in order to post.