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
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.
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.
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.
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.
Try file_exists() instead.
http://php.net/manual/en/function.file-exists.php
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"))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With