数组到 xml 或 xspf 为嵌入的 vlc lugin 播放列表制作 xspf 文件

array to xml or xspf make xspf file for embed vlc lugin playlist

我正在尝试输入复选框列表当前文件夹 .mp3 使用 gob 函数获取 glob_grace 然后使用复选框列出音频文件

获取数组复选框值使用php生成xspf文件

我把数组的代码都写到xml

但是小错误请任何人告诉我什么错误以及发生错误的地方!

<?php   
foreach (glob("*.{mp3,mp4}", GLOB_BRACE) as $filename) {
    $values = $filename ;
    echo "<form action='p.php' method='POST'>";
    echo "<input type='checkbox' name='foo[]' value='$values'>$values <hr>";    

}
    echo "<input type='submit' name='submit' value='Submit'>";
    echo "</form>";

?>

它将转到下面的下一页

<?php
        $g = $_POST['foo'];
       $cnt = count($g);
        //function definition to convert array to xml
function array_to_xml($array, &$xml_user_info) {
    for ($i=0 ; $i < $cnt ;$i++)
    {
        $track = $xml_user_info->addChild('track');
        $track->addChild("location",$array[$i]);
     }                                       

}

//creating object of SimpleXMLElement
$xml_user_info = new SimpleXMLElement("<?xml version=\"1.0\"?><trackList></trackList>");

//function call to convert array to xml
array_to_xml($g,$xml_user_info);

//saving generated xml file
$xml_file = $xml_user_info->asXML('users.xspf');

//success and error message based on xml creation
if($xml_file){
    echo 'XML file have been generated successfully.';
}else{
    echo 'XML file generation error.';
}
?>

请给我答案解决方案工作代码

提前致谢

如果 glob("*.{mp3,mp4}", GLOB_BRACE) returns 一个包含超过 1 个项目的数组,您将多次生成 <form action='p.php' method='POST'> 标签。

也许您可以将表单声明移到 foreach 之外。

如果我提交表单并加载 p.php,我会收到此通知:

Notice: Undefined variable: cnt

您正在使用 $cnt = count($g); 来计算项目,但您也可以传递此 $g 并在 function array_to_xml.

中计算其项目

我认为如果你想从$_POST['foo']中获取值,你应该首先检查它是否是一个POST然后检查是否设置了$_POST['foo']

也许这个设置可以帮助你:

<?php

//function definition to convert array to xml
function array_to_xml($array, &$xml_user_info)
{
    for ($i = 0; $i < count($array); $i++) {
        $track = $xml_user_info->addChild('track');
        $track->addChild("location", $array[$i]);
    }
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (isset($_POST['foo'])) {
        $g = $_POST['foo'];

        //creating object of SimpleXMLElement
        $xml_user_info = new SimpleXMLElement("<?xml version=\"1.0\"?><trackList></trackList>");

        //function call to convert array to xml
        array_to_xml($g, $xml_user_info);

        //saving generated xml file
        $xml_file = $xml_user_info->asXML('users.xspf');

        //success and error message based on xml creation
        if ($xml_file) {
            echo 'XML file have been generated successfully.';
        } else {
            echo 'XML file generation error.';
        }
    }
}
?>
$xml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><playlist></playlist>');
$trackList = $xml->addChild('trackList');
foreach ($_POST['files'] as $video) {
    $track = $trackList->addChild('track');
    $track->addChild('location', 'file://'.$tmp_dir.'/'.$video['path']);
    $track->addChild('title', $video['name']);
}
file_put_contents('playlist.xspf',$xml->asXML());