How to automatically assign paths when uploading files in PHP, _PHP tutorial

WBOY
Release: 2016-07-13 10:09:57
Original
724 people have browsed it

How to automatically assign paths when uploading files in PHP,

The example in this article describes the method of automatically assigning paths when uploading files in PHP. Share it with everyone for your reference. The specific analysis is as follows:

When a website uploads files, if it is a small corporate website, it is no problem to put it in one directory. When the website is large and there are many uploaded files, we cannot put them in the same directory. Let’s talk about it here. How to automatically assign paths to uploaded files using PHP.

PHP assigns path example of uploaded file
The main program fragments are as follows:

Copy code The code is as follows:
/*Numerical allocation path*/
function allotPath($id, $extend='jpg') {
$folders = str_split(sprintf("%012s", $id),3);
$folders[3] = $id;
 
Return '/'.join('/', $folders).'.'.$extend;

 
/*Hash allocation path*/
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(allotHashPath(122333));
// string(23) "/9c/7c/c2/cd/122333.jpg"

Here we are mainly talking about numerical distribution paths and hash distribution paths. We can also distribute them based on dates. Friends who are interested in this issue can implement it themselves.

I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/940492.htmlTechArticleHow to automatically assign paths when PHP uploads files. This article describes the method of automatically assigning paths when PHP uploads files. Share it with everyone for your reference. The specific analysis is as follows: On the website...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!