PHP method for automatically allocating paths when uploading files _ PHP

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

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.