PHP: How to automatically allocate paths when uploading files-PHP source code

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.