Use php to save the names of all files in a folder to the MySQL database.

Source: Internet
Author: User

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 );
?>

Related Article

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.