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, now let's talk about how to use PHP to automatically allocate paths to uploaded files. 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, now let's talk about how to use PHP to automatically allocate paths to uploaded files.
Script ec (2); script
PHP allocates a file upload path instance
The main program snippets are as follows:
| The Code is as follows: |
|
/* 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 mainly talk about the number-based path allocation and the miscellaneous-based path allocation. We can also allocate the path by date. This problem is left to the readers themselves.