Inserts all filenames under a specified directory into a table according to a directory name

Source: Internet
Author: User

The most recent project encountered a requirement to insert the names of all the files in a directory into a table with a certain set of rules, first of all to look at the badge table structure:

  

The directory name is ' picture/badge/', and the names of the files are named by certain rules, for example: Balldate_1_1_sl.png,match_1_10_1_1_gr.png,apply_1_50_sl.png,,, If you insert the balldate_1_1 into

Badge table, then type=1,first_index=1,second_index=1,path= ' picture/badge/balldate_1_1_sl.png ';

1. Get the names of all the files in this directory,

$file _names = Scandir (Public_path ('/picture/badge '));

Unset ($file _names[0], $file _name2[1]);(because the first 2 directories in the $file_names directory are. and. File

$data _array = Array ();

foreach ($file _names as $key = = $file _name) {

$new _file = substr ($file _name,0,strrpos ($file _names, '. '); /Remove the suffix of balldate_1_1.png. png

$explode _data = Explode (' _ ', $new _file); Will

$data [' type '] = $explode _data[0];

$data [' first_index '] = $explode _data[1];

$data [' second_index '] = $explode _data[2];

$data [' is_highlight '] = $explode _data[3] = = ' SL '? 1:0;

$data [' path '] = sprintf ('/upload/picture/badge/%s ', $file _name);

$data _array[] = $data;

}

Db::table (' badge ')->insert ($data _array);

    

Inserts all filenames under a specified directory into a table according to a directory name

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.