好的,这是我的情况。
我使用一个脚本扫描视频目录,检索视频的 imdb 信息,然后将数据存储在文本文件中。每部电影 1 个。我想从txt文件中获取数据并插入数据库。
脚本需要检查哪些是新的、哪些不是,并且只插入新信息。
这就是所有 txt 文件的布局方式。
09/04/2015 02:11:54
Cast Away
movie
tt0162222
Adventure,Drama
7.7
videos/Cast.Away.2000.mp4
pg_13
A FedEx executive must transform himself physically and emotionally to survive a crash landing on a deserted island.
每行的解释
Line 1 - Date txt/time file created
Line 2 - Title of Movie
Line 3 - Type of video (Movie or TV show)
Line 4 - image file associated with movie
Line 5 - Genre
Line 6 - Viewer Rating (0.0 to 10)
Line 7 - video directory
Line 8 - Movie Rating
Line 9 - Description
所以基本上我需要弄清楚如何将 txt 数据放入 mysql 数据库中,以便我可以在我的家庭媒体服务器上使用它。基本上我正在为我的大型 DVD 收藏制作一个类似 Netflix 的网站。
顺便说一句,目录布局是
/data - (txt files)
/pics - (cover photos)
/videos - (video files)
script.php
这是更新的代码 (9/5/2015 4:22) 仍然是相同的结果。空白行。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test script.</title>
</head>
<body>
<?php
$host = "localhost";
$username = "testing";
$password = "*******";
$database = "zadmin_testing";
$textFilesArray = scandir("data");
foreach( $textFilesArray AS $textFile )
{
$handle = fopen($textFile, "r");
# Get the lines of the file
$dateCreated = fgets($handle);
$videoTitle = fgets($handle);
$videoType = fgets($handle);
$videoImage = fgets($handle);
$videoGenre = fgets($handle);
$videoViewerRating = fgets($handle);
$videoPath = fgets($handle);
$videoMovieRating = fgets($handle);
$videoDesc = fgets($handle);
}
// Creating my connection
{
$conn = mysql_connect("$host","$username","$password");
//Checking connection
if (!$conn)
{
die('Could not connect: ' . mysql_error());
}
}
//My query
$query = "INSERT INTO `zadmin_testing`.`videos` (`date`, `title`, `type`, `cover`, `genre`, `vrating`, `path`, `mrating`, `description`) VALUES ('$dateCreated', '$videoTitle', '$videoType', '$videoImage', '$videoGenre', '$videoViewerRating', '$videoPath', '$videoMovieRating', '$videoDesc')";
{
//Run my query
mysql_query($query);
}
echo "<h2>testing 12</h2>";
mysql_close($conn);
?>
</body>
</html>
最佳答案
假设您要导入数据库的所有文本文件都在一个文件夹中,这就是我要做的:
首先,使用 scandir() 获取目录中所有文本文件的列表。
$textFilesArray = scandir("directoryContainingTheFiles");
然后,您可以循环浏览文件
foreach( $textFilesArray AS $textFile ){
从文件中读取所有数据......
# Open the file for reading (r)....
$handle = fopen($textFile, "r");
# Get the first line of the file
$dateCreated = fgets($handle);
# get the next line from the file
$movieTitle = fgets($handle);
现在,您有多种选择来避免重复记录。
一个选项是在文件的开头/结尾保存一个标志,当该标志存在时,跳过插入影片的尝试。
另一种选择是在您的 MySQL 字段之一上创建唯一键。 (也许在电影标题上)。然后,当您进行插入时,不会创建重复项。 (重复输入尝试将导致错误,这意味着什么都不会发生。如果您想抑制这些 mySQL 错误,您可以始终使用“INSERT IGNORE ....”而不是“INSERT ....”)
总结一下:
$textFilesArray = scandir("directoryContainingTheFiles");
foreach( $textFilesArray AS $textFile ){
# Open the file for reading (r)....
$handle = fopen($textFile, "r");
# Get the first line of the file
$dateCreated = fgets($handle);
# get the next line from the file
$movieTitle = fgets($handle);
# get the rest of the file into variables here
# preform checks on variables before attempting the insert
# (make sure strings are escaped, and number values contain all numbers, etc.)
# PREFORM mySQL INSERT QUERY HERE!
# something like: "INSERT INTO movies (date, title, etc) VALUES ($dateCreated, $movieTitle, etc)"
}
关于php - 多个txt文件到mysql条目脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32415329/