This article mainly introduces how to automatically allocate paths when uploading files in PHP, which can be used for classified storage of uploaded files. it has some reference value, for more information about how to automatically allocate paths when uploading files in PHP, see the following example. Share it with you for your reference. The specific analysis is as follows:
When a website uploads a file, if it is a small enterprise site, it is okay to put it in a directory. when the website is large and there are too many files uploaded, we cannot put it in the same directory, here we will talk about how to use PHP to automatically allocate paths to uploaded files.
PHP allocates a file Upload path instance
The main program snippets are as follows:
The code is as follows:
<? Php
/* Digitally allocate paths */
Function allotPath ($ id, $ extend = 'jpg '){
$ Folders = str_split (sprintf ("% 012 s", $ id), 3 );
$ Folders [3] = $ id;
Return '/'. join ('/', $ folders). '.'. $ extend;
}
/* Allocate paths by means of collection */
Function allotHashPath ($ id, $ extend = 'jpg '){
$ Folders = array_slice (str_split (md5 ($ id), 2), 0, 4 );
$ Folders [] = $ id;
Return '/'. join ('/', $ folders). '.'. $ extend;
}
Var_dump (allotPath (122333 ));
// String (23) "/000/000/122/122333 .jpg"
Var_dump (allothash path (122333 ));
// String (23) "/9c/7c/c2/cd/122333.jpg"
Here we will focus on the allocation of paths in the number mode and the allocation of paths in the miscellaneous mode. we can also allocate paths by date. if you are interested in this issue, you can do it yourself.
I hope this article will help you with php programming.