从文本文件php读取文件结构
我是一名网络程序员,但由于我正在攻读硕士学位,我在PHP中有一项任务,即创建一个具有上述文件结构的文本文件,并读取和插入数据库 ** 我的解决方案是创建一个包含此内容的文本文件从文本文件php读取文件结构,php,Php,我是一名网络程序员,但由于我正在攻读硕士学位,我在PHP中有一项任务,即创建一个具有上述文件结构的文本文件,并读取和插入数据库 ** 我的解决方案是创建一个包含此内容的文本文件 01dDocuments 11dImages 111fImages.jpg 112fImage1.jpg 113fImage2.jpg 12dWorks 121dAccountant 1211fLetter.doc 02dProgramFiles 21dSkype 211fSkype.exe 212fReadme.tx
01dDocuments
11dImages
111fImages.jpg
112fImage1.jpg
113fImage2.jpg
12dWorks
121dAccountant
1211fLetter.doc
02dProgramFiles
21dSkype
211fSkype.exe
212fReadme.txt
22dMysql
221fMysql.exe
222fMysql.com
首先,我尝试将目录及其对应目录设置正确-问题是嵌套文件夹,如“Accounting”,我想不出更好的解决方案。谢谢你的帮助
WalkDirectoryTree( 0, 1);
function WalkDirectoryTree(int $parrentcategory,int $root)
{
$file = fopen("testt.txt","r");
while(! feof($file))
{
$row = fgets($file);
if( mb_substr($row, 0, 3) == $parrentcategory.$root.'d')
{
$dir = new DirectoryFile();
$dir -> setName($row);
$dir-> setParrentId($parrentcategory);
print_r ($dir);
echo "<br>";
WalkDirectoryTree($parrentcategory, $root+1);
$parrentcategory++;
}
}
fclose($file);
}
include('../master.php');
?>
WalkDirectoryTree(0,1);
函数WalkDirectoryTree(int$parrentcategory,int$root)
{
$file=fopen(“testt.txt”、“r”);
而(!feof($file))
{
$row=fgets($file);
if(mb_substr($row,0,3)==$parrentcegory.$root.d'))
{
$dir=新目录文件();
$dir->setName($row);
$dir->setParrentId($parrentcategory);
打印(dir);
回声“”; WalkDirectoryTree($parrentcategory,$root+1); $parrentcategory++; } } fclose($文件); } 包括('../master.php'); ?>
YAML文件会更像原始文件并简化文件读取部分吗?我认为这只需要使用连字符来声明文件夹/文件的深度就可以了,这样您就可以根据当前深度(连字符的数量)计算出。您的任务可能不允许这样做,但我也建议只使用JSON文件,然后您只需执行
JSON\u decode($file,true)
即可将结构作为数组返回。
WalkDirectoryTree( 0, 1);
function WalkDirectoryTree(int $parrentcategory,int $root)
{
$file = fopen("testt.txt","r");
while(! feof($file))
{
$row = fgets($file);
if( mb_substr($row, 0, 3) == $parrentcategory.$root.'d')
{
$dir = new DirectoryFile();
$dir -> setName($row);
$dir-> setParrentId($parrentcategory);
print_r ($dir);
echo "<br>";
WalkDirectoryTree($parrentcategory, $root+1);
$parrentcategory++;
}
}
fclose($file);
}
include('../master.php');
?>