Alex Susanu
Alex Susanu

Reputation: 161

Can't create dir with slash in dir name

Using php 7 on mac os x, I can't create a folder with php mkdir() if the folder name has a slash in it, e.g. Test 24/04/2015.

Here's my PHP code:

$FolderPath = readline("Insert Folder Path "); // I enter /Users/me/Test 24/04/2015
echo "You have entered: " . $FolderPath;
echo "\n";
echo "\n";

$FolderPathResized = $FolderPath . "/Resized";

if (file_exists($FolderPathResized)) {
    echo "The folder $FolderPathResized exists";
    echo "\n";
}else {
    mkdir($FolderPathResized);
}

The error I get is:

mkdir(): No such file or directory in

How can I use mkdir() in such case? My folders will always have dates separates with slashes in the folder name.

Upvotes: 0

Views: 2442

Answers (2)

You probably have found the answer since this question is 4 years old, but if you want to create a folder with a slash in the name, you can do like this:

mkdir My\:Folder

It will create a folder with name = "My/Folder"

Hope it helps someone :)

Upvotes: 0

Ctc
Ctc

Reputation: 801

You cannot create directory names with a / in it. Use a _ would be better to separate the date.

This can be done with using the function str_replace() to replace all / with _ in your date.

The error is being shown because it captures the first / and attempts to write it in such a directory and it doesn't exist. You will have to encase it in quotes for it to be read in the first place.

Upvotes: 2

Related Questions