Creating random folders using php

I developed API integration, it contains several drag and drop downloads. The problem is that if several users to whom he accesses it, it crashes, so I planned to create random folders for each user and destroy this folder after everything is done. Are there any methods / methods available in PHP for generating random folders?

+4
source share
3 answers

For such things, I found a useful php uniqid function. Basically, something like this:

$dirname = uniqid(); mkdir($dirname); 

And then just move the downloaded file to this directory.

Edit: forgot to mention, the directory name is not random, but guaranteed to be unique, which seems to be what you need.

+14
source

I guess it is better to have a function that tries to create random folders (and check if it is successful) until it succeeds.

It has no race conditions, and it does not depend on belief in uniqid (), providing a name that has not yet been used as a name in tempdir.

 function tempdir() { $name = uniqid(); $dir = sys_get_temp_dir() . '/' . $name; if(!file_exists($dir)) { if(mkdir($dir) === true) { return $dir; } } return tempdir(); } 
+2
source

Yes its possible use mkdir() Example

 <?php mkdir("/path/to/my/dir", 0700); ?> 

To check this more

http://php.net/manual/en/function.mkdir.php

0
source

All Articles