Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP's mkdir function trouble on Windows

Tags:

php

mkdir

I am working on a command line program in PHP and I am having trouble, my first problem is when I call PHP's mkdir() it is giving me this error

Warning: mkdir(): No such file or directory in 
E:\Server\_ImageOptimize\OptimizeImage.php
on line 196

I then read in the PHP docs a user comment that said that the forward slash / does not work with this method under Windows but on Unix.

So I then changed my code to change them to backslashes but it did not change anything for me, I still got the same error on the same line.

Here is the code below can someone help me figure this out please

// I tried both of these below
$tmp_path = '\tmp\e0bf7d6';
//$tmp_path = '/tmp/e0bf7d6';

echo $tmp_path;

mkdir($tmp_path);
like image 241
CodeDevelopr Avatar asked Jan 09 '12 19:01

CodeDevelopr


People also ask

How create directory if not exists in PHP?

Methods: file_exists(): It is an inbuilt function that is used to check whether a file or directory exists or not. is_dir(): It is also used to check whether a file or directory exists or not. mkdir() : This function creates a directory.


2 Answers

The actual problem is that mkdir() only creates one subdirectory per call, but you passed it a path of two non-existant directories. You would normally have to do this step by step:

mkdir("/tmp");
mkdir("/tmp/e0b093u209");
mkdir("/tmp/e0b093u209/thirddir");

Or use the third parameter shortcut:

mkdir("/tmp/e0b093u209", 0777, TRUE);
like image 141
mario Avatar answered Oct 06 '22 01:10

mario


I normally use the following line as a constant and I put in a global file to be used through my sites.

defined('DS') ? null : define('DS', DIRECTORY_SEPARATOR);

That should fix the separator problem. I would also try the recursive property found in mkdir that will allow you to make the nested structure. Please see the foillowing, http://php.net/manual/en/function.mkdir.php

You will notice that you needs to call mkdir like below.

mkdir ($path, $mode, true)
like image 41
kwelch Avatar answered Oct 05 '22 23:10

kwelch