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