CakePHP constants are not recognized when accessing file from webroot

Question:

I was testing a simple little upload system in my cakephp, for that, I referenced the imgs folder by these paths that the root folder's index.php gives me. But it was always giving an error, saying the folder path was wrong, and when I echoed the variable, instead of returning /var/www/html/app/webroot/ , it was returning me WWW_ROOT . Nor the . DS . it's working. That is, my server is not recognizing the constants in the index.php file in the webroot folder.

$uploaddir = WWW_ROOT;
$uploadfile = $_FILES['userfile']['name'];
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploaddir . DS . $uploadfile)) {
    echo "Arquivo válido e enviado com sucesso.\n";
} else {
    echo "Possível ataque de upload de arquivo!\n";
}

echo 'Aqui está mais informações de debug:';
print_r($_FILES);
echo $uploaddir;

NOTE: I made 2 files in the webroot folder, to have direct access, just to test. I just wanted to know, because my server doesn't interpret this constant as it has to be, do you have any access to this?

Answer:

As you said in a comment, your file is loose in the project's webroot , so it is accessed directly, without going through Cake. That way, nothing from Cake gets initialized, including the constants you cited.

See apache's URL rewrite rules that are in Cake's webroot (note the comments):

RewriteEngine On
# se o caminho corresponde a um diretório, acessa diretamente
RewriteCond %{REQUEST_FILENAME} !-d
# se o caminho corresponde a um arquivo, acessa diretamente
RewriteCond %{REQUEST_FILENAME} !-f
# caso contrário, chama o index.php passando o resto da URL como parâmetro
RewriteRule ^(.*)$ index.php [QSA,L]

In other words, the webroot's index.php file, which starts Cake, is only called if the URL doesn't correspond to a physical directory or file below the webroot.

Scroll to Top