Prepare the SQL once, execute it for each validated file record, and use a transaction when storing many rows. Keep database credentials outside the tutorial page in your normal application configuration.
<?php
$insert = $pdo->prepare(
'INSERT INTO file_dtl (url, title, dt) VALUES (:url, :title, :dt)'
);
$pdo->beginTransaction();
try {
foreach ($files as $file) {
$insert->execute([
':url' => $file['url'],
':title' => $file['title'],
':dt' => $file['date'],
]);
}
$pdo->commit();
} catch (Throwable $e) {
if ($pdo->inTransaction()) {
$pdo->rollBack();
}
throw $e;
}
?>
<?Php
$dbhost_name = "localhost";
$database = "sql_tutorial";
$username = "root";
$password = "test";
//////// Do not Edit below /////////
try {
$dbo = new PDO('mysql:host=localhost;dbname='.$database, $username, $password);
} catch (PDOException $e) {
//print "Error!: " . $e->getMessage() . "<br/>";
die();
}
?>
After connecting to MySQL we will insert the data. The part which does the storing is here.
/////Inserting to MySQL database //////
$sql=$dbo->prepare("insert into file_dtl(url,title,dt) values(:url,:title,:dt)");
$sql->bindParam(':url',$url,PDO::PARAM_STR, 200);
$sql->bindParam(':title',$title,PDO::PARAM_STR, 150);
$sql->bindParam(':dt',$dt,PDO::PARAM_STR,10);
if($sql->execute()){
}
else{
print_r($sql->errorInfo());
echo "<br>";
//echo " Not able to add data please contact Admin ";
}
////// Enf of Inserting to MySQL datbase //////
We have used PDO to connect and insert data to MySQL table<!doctype html public "-//w3c//dtd html 3.2//en">
<html>
<head>
<title>Storing the file data in a MySQL table</title>
</head>
<body>
<?Php
include "z_db1.php";
///////////////function my_strip///////////
function my_strip($start,$end,$total){
$total = stristr($total,$start);
$f2 = stristr($total,$end);
return substr($total,strlen($start),-strlen($f2));
}
/////////////////////End of function my_strip ///
///////////// Reading of file content////
$i=0; // Number of file counter
echo "<table><tr><th>URL ( Path + file name)</th><th>Title</th><th>Date</th></tr>";
$path="dir_name/path/";// Change this to your directory or path
$handle=opendir($path);
while (($file_name = readdir($handle))!==false) {
$str="";
if(stristr($file_name,".php")){ // We are checking for a perticular file extension, change this to match yours
$url="$path".$file_name;
$contents="";
$fd = fopen ($url, "r"); // opening the file in read mode
while($buffer = fread ($fd,1024)){
$contents .=$buffer;
}
/////// End of reading file content ////////
//////// We will start with collecting title part ///////
$title = my_strip("<title>","</title>",$contents);
$dt=date("Y-m-d",filemtime($url)); // Last modification date of the file
/////Inserting to MySQL database //////
$sql=$dbo->prepare("insert into file_dtl(url,title,dt) values(:url,:title,:dt)");
$sql->bindParam(':url',$url,PDO::PARAM_STR, 200);
$sql->bindParam(':title',$title,PDO::PARAM_STR, 150);
$sql->bindParam(':dt',$dt,PDO::PARAM_STR,10);
if($sql->execute()){
}
else{
print_r($sql->errorInfo());
echo "<br>";
//echo " Not able to add data please contact Admin ";
}
////// End of Inserting to MySQL datbase //////
//echo "<tr><td>$url</td><td>$title</td><td>$dt</td></tr>";
$i=$i+1;
}// end of if checking file extension
}// end of while reading directory
closedir($handle);
echo "</table>";
echo "Total Number of files checked = $i ";
?>
</body>
</html>
The SQL Dump of the table is here
CREATE TABLE IF NOT EXISTS `file_dtl` (
`id` int(3) NOT NULL AUTO_INCREMENT,
`url` varchar(200) NOT NULL,
`title` varchar(150) NOT NULL,
`dt` date NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=136 ;
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.