user2650277
user2650277

Reputation: 6739

mkdir giving false warning when creating a directory

I am trying to create a temporary directory but I am getting following error

Warning: mkdir(): File exists

However when i checked the directory actually doesn't exist.A typical value for $tmp is /tmp/testKanfEt

$tmp = tempnam(sys_get_temp_dir() , 'test');
echo  $tmp;
mkdir($tmp);

What am i missing here ?

Upvotes: 2

Views: 412

Answers (1)

Xorifelse
Xorifelse

Reputation: 7911

The function tempnam() creates a file in given path and returns the full path if created successfully. You are attempting to make a directory with the same path as the file.


But in this case, tempnam() is being used wrongly in my opinion. It should be used when trying to make tmp files in any other directory than the o.s. tmp folder.

Why? Because making files in the tmp directory you should not care about the file name because once the lock has been released on a file (with fclose() or end of script execution for example) you cannot guarantee the file still being there.


Instead, use tmpfile() as it returns a file handle directly creating a file in the tmp directory.

And if you really need the file name, you still could use tempnam() or retreive it from the handle like so:

echo stream_get_meta_data(($fh=tempfile()))["uri"];

Upvotes: 2

Related Questions