PHP says file doesn’t exist even if it exists

Asked

Viewed 591 times

1

Hello, when I log in I redirect to a file called 'config.php' which in turn includes a file called 'usuario_dao.php'. The problem is that I get the following error:

Warning: include(/opt/lampp/htdocs/projects/centraljogos/config/dao/usuario_dao.php): failed to open stream: File or directory not found in /opt/lampp/htdocs/projects/centraljogos/config/valida_login.php on line 9

Warning: include(): Failed opening '/opt/lampp/htdocs/projetos/centraljogos/config/dao/usuario_dao.php' for inclusion (include_path='.:/opt/lampp/lib/php') in /opt/lampp/htdocs/projetos/centraljogos/config/valida_login.php on line 9

Fatal error: Call to Undefined Function seleciona_usuario() in /opt/lampp/htdocs/projects/centraljogos/config/valida_login.php on line 11

But the file exists in the folder specified in the code. As you can see below:

<?php

session_start();

//Recebe os campos da página login
$input_usuario = $_POST['usuario'];
$input_senha = $_POST['senha'];

include (dirname(__FILE__) . '/dao/usuario_dao.php');

$usuario = seleciona_usuario($input_usuario, $input_senha);

//Se o resultado retornar vazio executa
if(empty($usuario))
{
    //Mensagem de erro
    $_SESSION['login_erro'] = "Usuário e/ou senha inválido(s)! Por favor, tente novamente.";
    
    //Redireciona para a tela de login
    header("Location: index.php");
}
else
{
    //Sessions que recebem os valores dos campos do banco de dados
    $_SESSION['nome_usuario'] = $aluno['nome'];
    $_SESSION['acesso_usuario'] = $aluno['permissao'];
    $_SESSION['login_usuario'] = $aluno['login'];

    $_SESSION['permissao'];
    
    if($_SESSION['acesso_usuario'] == 1)
    {
        $_SESSION['permissao'] = "Master";
        header('Location: menu.php');
    }
    else
    {
        $_SESSION['permissao'] = "Usuário";
        header("Location: menu_usuario.php");
    }
}

The file is exactly in: "/opt/lampp/htdocs/projects/centralgames/dao/usuario_dao.php".

Ahh and that "dirname(__FILE__)" i am using justly because I have been told that I would avoid errors when including directories. If anyone can help me I would appreciate it very much.

  • 1

    missing a directory in the middle, the folder "config": /opt/lampp/htdocs/projetos/centraljogos/config/dao/usuario_dao.php

  • 1

    Thanks for the help.

1 answer

3

Quick Fix! It’s copy and paste that funnel!

In this passage:

include (dirname(__FILE__) . '/dao/usuario_dao.php');

Trade for this:

include (__DIR__.'/../dao/usuario_dao.php');









Detailed solution. Care, gives sleep and causes mental confusion.

In the question itself you point to the correct path, which is different from the path entered in the error message.

The error message returns:

/opt/lampp/htdocs/projetos/centraljogos/config/dao/usuario_dao.php

However, you inform yourself that the path is /opt/lampp/htdocs/projetos/centraljogos/dao/usuario_dao.php

Note that there is a folder config/ in the error path.

One simple way to solve it is to go back one level:

include (__DIR__.'/../dao/usuario_dao.php');

With this, the generated path is /opt/lampp/htdocs/projetos/centraljogos/config/../dao/usuario_dao.php

Each ../ makes indentation of 1 directory.

This causes the path to be pointed to

/opt/lampp/htdocs/projetos/centraljogos/dao/usuario_dao.php

Tip: for greater compatibility, I suggest you use the DIRECTORY_SEPARATOR instead of manually setting the bars.

include (__DIR__.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'dao'.DIRECTORY_SEPARATOR.'usuario_dao.php');
  • Thank you so much for the answer, it helped me a lot. Hug!

Browser other questions tagged

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