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.

Ken Y-N
  • 14,644
  • 21
  • 71
  • 114
Alex Susanu
  • 163
  • 1
  • 9

2 Answers2

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.

Ctc
  • 783
  • 5
  • 21
  • 2
    This is not really an answer. It should have been a comment. – arkascha Jun 27 '16 at 05: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 '16 at 05: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 '16 at 05: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 '16 at 05:58
  • It does not achieve what the OP asks for. – arkascha Jun 27 '16 at 06:02
  • Thank you very much for your answers. Worked with the "_" instead of "/", will try to modify all folder as such so wont have any issues in the future. Though dont understand why the down vote, seriously. I am a beginner in programming and I can't even understand why my question was down voted when for me seems ok to ask because I couldnt understand why mkdir() didnt worked... Anyway thank you very much for the answers. – Alex Susanu Jun 27 '16 at 06:25
  • If I have answered your question, a upvote and a check would be nice :) – Ctc Jun 27 '16 at 06:26
  • @Ctc I tried to give you an upvote but it gave me this in reply " Once you earn a total of 15 reputation, your votes will change the publicly displayed post score " :((( ... – Alex Susanu Jun 27 '16 at 06:28
  • @user6387363 its okay :) glad I helped! – Ctc Jun 27 '16 at 06:32
  • @Ctc I earned 15 reputations, I will gladly give the upvote now :) ! – Alex Susanu Jun 30 '16 at 15:18
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 :)