How do I get a file's path in PHP?
I have to check using the file_exists functio开发者_如何学编程n... But, if I use something like that
if (file_exists('http://horabola.com/imagens/dt_2845.jpg')) {
//code
}
it doesn't work...
I know and I'm sure that the file "dt_2845.jpg" exists in the folder "imagens" .... now, how do I check that? How do I get the server's file path?
Try:
if (file_exists($_SERVER['DOCUMENT_ROOT'].'/imagens/dt_2845.jpg')) {
//code
}
Good luck
The server's filesystem path for url / is stored in the $_SERVER['DOCUMENT_ROOT']
predefined variable.
The current script's path is always stored in the __FILE__
constant. You may want to use
dirname(__FILE__);
to know the current script's filesystem path.
What you tried by calling:
file_exists('http://horabola.com/imagens/dt_2845.jpg');
which is open files via HTTP, needs the use of fopen() instead of file_exists() (thanks @Gordon for pointing this out), and you need, on your PHP server, the so-called url wrapper for fopen(). Anyway, using the HTTP protocol to open files on the same server as the runnning script is a bit of a performance waste (using HTTP, that is network, instead of hard disk, that is 10x to 1000x faster).
精彩评论