CakePHP constants are not recognized when accessing file in webroot

3

I was testing a simple upload system in my cakephp, for that, I referenced the imgs folder by those paths that the index.php of the root folder gives me. But it was always giving error, saying that the folder path was wrong, and when I echoed the variable, instead of returning /var/www/html/app/webroot/ , I was returning WWW_ROOT . Neither . DS . is working. That is, my server is not recognizing the constants of 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 itself, to have direct access, just to test. I just wanted to know, because my server does not interpret this constant as it has to be, do you have any access to it?

    
asked by anonymous 31.03.2015 / 14:54

1 answer

1

As you said in a comment, your file is loose in the webroot of the project, so it is accessed directly, without going through Cake. This way, nothing that is Cake is initialized, including the constants you quoted.

See the apache URL rewrite rules in Cake's webroot (attention to 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 file index.php of webroot , which initializes Cake, is only called if the URL does not correspond to a directory or physical file below the webroot.     

31.03.2015 / 16:40