Check whether a Directory Exists in PHP

I know, I know, this sounds soo easy. But I can't seem to find the correct answer on the Internet.

One of the solution I found was to use is_dir.

if(is_dir($dir)) echo 'directory exists'; else echo 'drectory not exist';

But this is wrong-- All this function does is to check whether the $dir is a directory, it doesn't check whether the directory exists, or not. In other words if I put:

$rootDir = "C:\\Documents and Settings\\test\\My Documents\\Image Directory\\Me Dog\\";

then the function will return a true, even though you can find no such directory on your web server.

Any ideas?


Should work correctly. From is_dir() documentation:

Returns TRUE if the filename exists and is a directory, FALSE otherwise.

Well, anyway if it doesn't try this:

if(file_exists($dir) && is_dir($dir))

BTW. results of these functions are cached in stat cache. Use clearstatcache() to clean that cache.

You'll probably want to use opendir() after is_dir() agrees that the path (could) be a directory.

If the resource returned by opendir() is valid, you know you have a directory, and already have a handle to read it.

Just be sure to call closedir(), either way, if a valid handle is returned.


This answer assumes that you'll be opening the directory either way. If you just need to ensure a path is sane / valid, file_exists() is much cheaper.

You could try this:

if(is_dir($dir) && is_writeable($dir))
// ...

bool file_exists ( string $filename )

Checks whether a file or directory exists.

Category:php Time:2009-04-14 Views:0
Tags: php file io

Related post

  • In Python, check if a directory exists and create it if necessary 2008-11-07

    What is the most elegant way to check if the directory a file is going to be written to exists, and if not, create the directory using Python? Here is what I tried: filename = "/my/directory/filename.txt" dir = os.path.dirname(filename) try: os.stat(

  • Using IsolatedStorage, Is it necessary to check if a directory exists before creating it? 2010-09-20

    I'm working on a Windows Phone 7 app, and I was wondering whether anyone had a definitive answer on whether or not I have to check if a directory exists before creating one, and what the advantages/disadvantages of doing/not doing so are. As far as I

  • How do you check if a directory exists on Windows in C? 2011-06-02

    Question In a Windows C application I want to validate a parameter passed into a function to ensure that the specified path exists.* How do you check if a directory exists on Windows in C? *I understand that you can get into race conditions where bet

  • C faster way to check if a directory exists 2012-02-16

    I'm using opendir function to check if a directory exists. The problem is that I'm using it on a massive loop and it's inflating the ram used by my app. What's the best (faster) way to check if a directory exists in C? What's the best (faster) way to

  • How to make Python check if ftp directory exists? 2009-07-08

    I'm using this script to connect to sample ftp server and list available directories: from ftplib import FTP ftp = FTP('') # connect to host, default port (some example server, i'll use other one) ftp.login() # user anonymous, passwd anonym

  • Checking if a directory exists on another server in ksh 2009-07-27

    I am trying to verify if a directory exists prior to moving a file in Korn, using the classic: if [[ -d ${dir} ]]; then scp file else exit 12 fi My Problem: That the directory is on another server, so whenever I check, the script can't find it and th

  • How to check whether a directory exists before inserting a file 2009-11-07

    I have a path like this c:application\photo\gallery\sketches Now I need to check whether this entire path exits or not before inserting a file into this location thank you --------------Solutions------------- Directory.Exists Method is what you're lo

  • how to check if ANY directory exists without knowing a specific directory name, but rather just the folder that the folder might be in 2010-04-17

    In how do you check if a ANY directory exists inside a directory I would need to know if there is a folder inside the c:\windows directory (WITHOUT knowing if there is ANY directory is in there). --------------Solutions------------- So you wan

  • Is there a way to check if a directory exists in Apache configuration files? 2010-09-12

    Is there a way to include configuration settings in Apache based on if a directory exists? Basically I have a portable hard drive that I transport between work and home that has some stuff I'm developing on it. I only want the Apache config to load a

  • Checking if a directory exists in Unix (system call) 2010-09-30

    I am not able to find a solution to my problem online. I would like to call a function in Unix, pass in the path of a directory, and know if it exists. opendir() returns an error if a directory does not exist, but my goal is not to actually open, che

  • Apache Commons FTPClient, check if remote directory exist and get permissions (linux - unix) 2010-12-21

    Is it possible with FTPClient (Apache commons-net) to check if a remote directory exists? I want to do something like this: ftp.isDirectory(String path) //returns true, false And then get permissions (chmod) of directory: ftp.getPermisions(String pat

  • Check whether a directory exists or not using JavaScript 2011-09-22

    How to check whether a directory or folder exists or not using javascript or jquery? --------------Solutions------------- You can't. If Javascript had access to the file system (either client or server), it would be a security hole. Do you mean Javas

  • check if a directory exists using FTPClient 2012-03-12

    I'm using FTPClient (org.​apache.​commons.​net.​ftp.​FTPClient) to upload some files to a FTP server. I need to check if parent directories exists or not; and create them if needed. How can I check file/directory existence using FTPClient? Thanks ---

  • checking if a directory exists on a remote machine and copying the file on it 2011-08-18

    I have a shared folder F on a remote machine M. Now I want to run a program on my local machine which does following. Check if subfoder S exists with \\remoteMachine\F if S exists then copy my file tstfile.txt within S else if S does not exist then c

  • Check that any directory exists inside given one 2012-02-29

    My application is Windows, C# 3.0. I want to make sure that a directory given does not contains subdirectories. Naive code like if (Directory.GetDirectories(path).Length != 0) will work very slow on directories which contain e.g. 10000 subdirectories

  • Check if directory exists in lua? 2009-08-27

    How do I check if a directory exists in lua, preferably without using the LuaFileSystem module if possible? Trying to do something like this python line: os.path.isdir(path) --------------Solutions------------- The problem is that the stock Lua distr

  • Check if directory exist on android's sdcard 2010-04-12

    How do I check if a directory exist on the sdcard in android? --------------Solutions------------- Regular Java file IO: File f = new File(Environment.getExternalStorageDirectory() + "/somedir"); if(f.isDirectory()) { .... Might also want to check f.

  • check if directory exists and delete in one command unix 2011-01-30

    Is it possible to check if a directory exists and delete if it does,in Unix using a single command? I have situation where I use ANT 'sshexec' task where I can run only a single command in the remote machine. And I need to check if directory exists a

  • Make fails checking if directory exists 2011-04-08

    I googled for this, but I can't figure out why Bash complains with the following code to check if a directory exists: #!/bin/bash MYDIR="dl" all: if [ ! -d $MYDIR ]; then #if [ ! -d "${MYDIR}" ]; then #if [ ! -d ${MYDIR} ]; then #Here fi make

Copyright (C), All Rights Reserved.

processed in 0.567 (s). 13 q(s)