Implementation: Use php to save the names of all files in a folder to the MySQL database.
Note: enter it as A folder A. If this folder contains another folder B, only files in A are not displayed.
The source code is as follows:
<? Php
// Function: Save the file names of all files in the folder to the mysql database.
// Configure the database
$ Server = 'localhost ';
$ User = 'root ';
$ Passwd = 'Password ';
$ Port = '000000 ';
$ Dbname = 'dbname ';
$ Link = mysql_connect ($ server, $ user, $ passwd );
If (! $ Link ){
Die ('could not connect: '. mysql_error ());
}
Else echo 'connectedsuccessfully ';
$ Start = time ();
Mysql_select_db ("catx", $ link); // select the database name.
// Php function body
Function listDir ($ dir ){
$ Cout = 0;
If (is_dir ($ dir )){
If ($ dh = opendir ($ dir )){
While ($ file = readdir ($ dh ))! = False ){
If ($ file! = "." & $ File! = ".."){
$ File = mb_convert_encoding ($ file, 'utf-8', 'gbk'); // conversion encoding, because the Chinese windwows file name is encoded in GBK format
// Var_dump ($ file );
$ Re = explode (".", $ file); // break down the file name
$ Res = $ re [0]; // do not include the file suffix and retrieve the file name
$ SQL = "insert into table_name (name) values ('". $ res ."');";
Mysql_query ($ SQL );
$ Cout ++;
Echo $ res. "<br> ";
}
}
Closedir ($ dh );
}
}
Return $ cout;
}
// Enter the path of the folder to be processed.
$ Cout = listDir ("E:/data ");
$ End = time ();
$ Time = $ end-$ start;
Echo "cout:". $ cout. "<br> ";
Echo "starttime:". $ start. "<br> ";
Echo "endtime:". $ end. "<br> ";
Echo $ time. "<br> ";
Mysql_close ($ link );
?>