Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - why is_dir returns TRUE when a dir doesn't exist?

Tags:

php

My current directory structure is as follows:

C:\xampp\htdocs\PHP_Upload_Image_MKDIR

In other words, the following directories do NOT exist at all.

C:\xampp\htdocs\PHP_Upload_Image_MKDIR\uploaded
C:\xampp\htdocs\PHP_Upload_Image_MKDIR\uploaded\s002

The problem is that when I run the following script, the function is_dir always return TRUE.

Based on the manual, http://us2.php.net/manual/en/function.is-dir.php is_dir: Returns TRUE if the filename exists and is a directory, FALSE otherwise.

Do I miss something here?

Thank you

$userID = 's002';
$uploadFolder = '/PHP_Upload_Image_MKDIR/uploaded/';
$userDir = $uploadFolder . $userID;
echo '<br/>$userDir: ' . $userDir . '<br/>';

if ( is_dir ($userDir))
{
  echo "dir exists"; // always hit here!!!
}
else 
{
  echo "dir doesn't exist";
}

mkdir($userDir, 0700);
C:\xampp\htdocs\PHP_Upload_Image_MKDIR>dir /ah
 Volume in drive C is System
 Volume Serial Number is 30B8-2BB2

 Directory of C:\xampp\htdocs\PHP_Upload_Image_MKDIR

File Not Found

C:\xampp\htdocs\PHP_Upload_Image_MKDIR>


//////////////////////////////////////////////////////////

Based on Artefacto's comments:

Here is the output of C:\PHP_Upload_Image_MKDIR\uploaded\s005
 echo '<br/>' . realpath($userDir) . '<br/>';

Thank you for the solutions.

Best wishes

like image 248
q0987 Avatar asked Aug 28 '10 16:08

q0987


People also ask

How do you check if a dir exists in PHP?

The file_exists() function in PHP is an inbuilt function which is used to check whether a file or directory exists or not. The path of the file or directory you want to check is passed as a parameter to the file_exists() function which returns True on success and False on failure.

How do I get a list of files in a directory in PHP?

The scandir() function in PHP is an inbuilt function that is used to return an array of files and directories of the specified directory. The scandir() function lists the files and directories which are present inside a specified path.

How will you Create a folder in PHP?

The mkdir() function is used to create directory in PHP. It is an inbuilt function in PHP. The mkdir() function creates a new directory with the specified pathname. The path and mode are sent as parameters to the mkdir() function and it returns TRUE on success or FALSE on failure.


3 Answers

Also, it seems as if the dir you are checking is PHP_Uploaded_Image_MKDIR/uploaded/s002, which is an absolute path starting from the root filesystem.

Try prepending C:\xampp\htdocs\ to this and see if it works then. Also, check to see if the folder exists at the root of the volume.

like image 155
efritz Avatar answered Oct 04 '22 01:10

efritz


Try file_exists() instead.

http://php.net/manual/en/function.file-exists.php

like image 26
Moe Sweet Avatar answered Oct 04 '22 01:10

Moe Sweet


If you've run that script more than once, then is_dir($userDir) will return true because of this line (the last one) in your script:

mkdir($userDir, 0700);

You can use rmdir() or some other method to delete it.

To test is_dir(), try a directory name that has never been used / created. Something like the following should return false, when it does, you know that is_dir() works:

if ( is_dir ("/PHP_Upload_Image_MKDIR/uploaded/lkjlkjlkjkl"))
like image 29
Peter Ajtai Avatar answered Oct 04 '22 02:10

Peter Ajtai