I'm trying to check if a directory exists, and if not, create it, but the page doesn't work for me when I use the file_exists
or functions is_dir
.
I have tried like this:
if(!file_exists( $path )
mkdir($path);
And so:
if(!is_dir ( $path )
mkdir( $path );
And I have also tried to put the path
with different formats:
According to the documentation, if I'm on Windows I should do it like this:
$path= "C://Apache24/htdocs/uploads/carpeta"
Although I have also tried to do it like this:
$path ="C:/Apache24/htdocs/uploads/carpeta"
And so
$path= "./uploads/carpeta"
Even so:
$path= $_SERVER['DOCUMENT_ROOT']."/uploads/carpeta"
In all cases it fails. Locally I am working on Windows but I guess when I upload it to the server it will have to run on Linux .
Why do these functions fail? How is it solved?
The code
!file_exists
is correct. You should only be aware that a parenthesis is missing.I added the curly braces to make it clearer, although they are not necessary.
I also think you have a difference with
$path
. It's important to understand how that works,$path
it refers to where you run the function fromfile_exists
.For example if you are in...
...and you want to check if the directory exists
...
$path
should beimages/pics
(note changed slashes and path relative to where the function is executed).If instead you execute the function from...
... then
$path
it should be../images/pics
, ie download a directory (../) and then the path to validate (images/pics
).