0

我正在尝试输入复选框列表当前文件夹.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.';
}
?>

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

提前致谢

4

2 回答 2

1

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

也许您可以将表单声明移到foreach.

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

注意:未定义变量: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.';
        }
    }
}
?>
于 2015-12-17T16:53:22.803 回答
0
$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());
于 2020-06-15T23:47:43.800 回答