0

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.

2
  • 2
    You can't even manually create such directory name. See this
    – Thamilhan
    Jun 27, 2016 at 5:46
  • 1
    And even if: that would be a really bad idea: how to you want to use it afterwards?
    – arkascha
    Jun 27, 2016 at 5:50

2 Answers 2

2

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.

10
  • 2
    This is not really an answer. It should have been a comment.
    – arkascha
    Jun 27, 2016 at 5:50
  • 1
    He asked how to use mkdir() in such a case. In this case, the only way he can use it is to replace all _ with / to prevent it from being read as the root directory. His error obviously shows that it is reading from the root directory.
    – Ctc
    Jun 27, 2016 at 5:54
  • 1
    All I said is: this did not answer the question. And maybe there is a reason why you need a minimum reputation to comment...
    – arkascha
    Jun 27, 2016 at 5:56
  • 1
    His question was how can he use mkdir() when his dates separates with a slash. My answer to that is to replace all / with _ , how is that not an answer to his question?
    – Ctc
    Jun 27, 2016 at 5:58
  • It does not achieve what the OP asks for.
    – arkascha
    Jun 27, 2016 at 6:02
0

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 :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.