0
This is not exactly a direct question, but it might be interesting since there is another discussion here about file_exists vs is_file.
I, like many programmers, define variables to find myself in the environment - file structure, like this:
$dirRoot = "/Applications/MAMP/htdocs/";
When a file enters, it completes itself like this:
$dirArquivo = $dirRoot.$arquivo;
But a common error is the $file being empty (the user’s dummy did not fill the field), in this case, if we test $dirFile file with file_exists will occur a false TRUE. See a test code:
$dirRoot = "/Applications/MAMP/htdocs/";
$arquivo = "index.php";
$dirArquivo = $dirRoot.$arquivo;
$txtResult = "Teste file_exists para ".$arquivo."\n";
$txtResult .= "Diretório ".$dirRoot."\n";
$txtResult .= "Caminho para o arquivo ".$dirArquivo."\n";
$txtResult .= "\nNome errado: ";
$txtResult .= (file_exists("nindex.php"))? "TRUE" : "FALSE" ;
$txtResult .= "\nSem diretório: ";
$txtResult .= (file_exists($arquivo))? "TRUE" : "FALSE" ;
$txtResult .= "\nCom diretório (usual): ";
$txtResult .= (file_exists($dirArquivo))? "TRUE" : "FALSE" ;
$txtResult .= "\nSem arquivo (erro comum): ";
$txtResult .= (file_exists($dirRoot))? "TRUE" : "FALSE" ;
echo($txtResult);
Upshot:
Teste file_exists para index.php
Diretório /Applications/MAMP/htdocs/
Caminho para o arquivo /Applications/MAMP/htdocs/index.php
Nome errado: FALSE
Sem diretório: TRUE
Com diretório (usual): TRUE
Sem arquivo (erro comum): TRUE
See that file_exists returns TRUE because it is a directory and it exists, and it tests both. An alternative is to use is_file or combine like this:
if(file_exists($dirRoot.$arquivo) && $arquivo)
In both cases, it seems to me a gambiarra. Is there any right way to deal with it? It is acceptable to program like this?
The user has not filled the field or was the programmer’s mané not validated? : P
– bfavaretto
Did your question actually isn’t: When using file_exists() or is_file() ? Also, why do you call the user dummy for not having filled in any field if your system allows this?
– felipsmartins
"It’s acceptable to program like this?" No validation? Of course not acceptable. Enough one programmer to avoid failures that infinite users can commit. Understood this, which side is the responsibility of validating the same data? Or do you believe the "lack of training" excuse that many IT companies claim when it doesn’t work?
– Bacco
Furthermore, it has already been answered here: http://answall.com/questions/99992/arquivo-existe-php
– Bacco
But what I’m doing is just validate the file, see if it exists. The field can be validated via JS but there may be another error that leads to $file being empty... plus I even put the differences between file_exists and is_file... sorry if I offended someone, my intention was only to debate the practice of programming... in my experience, to assume that the variable is correct because another stage has already validated it is risky.
– Gustavo
@user6492 You have not offended any of us, only the "poor" users - ok, not always so poor :) About validate, you need to validate on the server, JS is not enough. You need to see if the file name came empty. If it came, it returns the person to the form, and it doesn’t even process. If the person didn’t even indicate the file name, does it make sense to check if the [nameless] file already exists? It doesn’t! As the linked question shows, the
file_exists
, despite the name, it behaves like a "path_exists" because it checks the complete path. If it points to a dir that exists, it givestrue
. There’s no changing that.– bfavaretto
There is some difference between the function is_file and file_exists?
– rray
@bfavaretto You answered the question! I kept thinking and I think I oversimplified the question and did not yield the discussion. The real problem occurred in a system that reads some metadata and composes the $file and is composed of several libraries. There were "cascading" errors and it was difficult to find the source. The test then is done many times and I was in doubt whether I was doing a gambiarra many times. I will pull myself further into the question next time!
– Gustavo