如何发送一个无法序列化到另一个页面的数组?

时间:2016-05-26 22:22:22

标签: php arrays

<!DOCTYPE html>
<html>
<head>
    <title>Centrum Homepage</title>
</head>
<body>



<div id="logo">
<h1>WELCOME TO CENTRUM</h1>
</div>
<div id="instruction">
Enter a path on the filesystem to search for media    
</div>
<div id="search-div">
<form action="index.php" method="post">
<input type="text" name="search-bar">
<input type="submit" name="search-submit" value="Search Path">    
</form>

<?php

// You can add any file you want to search for, keep in mind, the default ones are the only ones supported by HTML5 players

$ImageFormats = array('jpeg', 'jpg', 'png');
$MusicFormats = array('mp3', 'wav');
$VideoFormats = array('mp4', 'webm');
$ImageFound = array();
$MusicFound = array();
$VideoFound = array();



if(isset($_POST['search-submit'])){
    $path = $_POST['search-bar'];

if (!file_exists($path)) {
    echo "Path not found. Please try another path.";
} else {

// If Path is valid, search for media in folders and sub folders and add them to arrays

foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path)) as $filename)
{


if(in_array(pathinfo($filename, PATHINFO_EXTENSION), $ImageFormats)) 
    array_push($ImageFound, $filename);


elseif(in_array(pathinfo($filename, PATHINFO_EXTENSION), $MusicFormats)) 
    array_push($MusicFound, $filename);


elseif(in_array(pathinfo($filename, PATHINFO_EXTENSION), $VideoFormats)) 
    array_push($VideoFound, $filename);
}

if (empty($ImageFound))
    echo "No Image found";
else {
    echo "We found " . count($ImageFound) . "image files" . "<br>";
        echo '<form action="photos.php" method="post">';
     echo '<input type="image" src="images/photo.png" \>';
    echo "</form>";

}



if (empty($MusicFound))
    echo "No Music found";
else {




    echo "We found " . count($MusicFound) . "Music files" . "<br>";
        echo '<form action="music.php" method="post">';
     echo '<input type="image" src="images/music.png" \>';
    echo '<input type="submit" name="submit-music" value="View Music" \>';

    echo "</form>";

}


if (empty($VideoFound))
    echo "No Video found";
else {
    echo "We found " . count($VideoFound) . "video files" . "<br>";
        echo '<form action="videos.php" method="post">';
     echo '<input type="image" src="images/video.png" \>';
    echo '<input type="submit" name="submit-videos" value="View videos" \>';
    echo "</form>";

}
}
}
?>

</body>
</html>

所有阵列都在机器上存储大量路径。这个脚本将在raspberry pi上运行,没有MySQL。我尝试写一个文件var_dump

你可以想到一个类似于

的数千个字符串

/home/Desktop/centrum/1.mp3

我试图将它保存在会话中,它没有被保存,有没有办法将它保存到会话而不会弄乱数据?数据不能serialized只是最简单的会话方式吗?在玩数据库之前。

如果我写$sm = serialize($MusicFound);

之前

if (empty($ImageFound))

我得到了

  

致命错误:未捕获的例外:序列化&#39; SplFileInfo&#39;不是   允许在/opt/lampp/htdocs/web/centrum/index.php:60堆栈跟踪:#0   /opt/lampp/htdocs/web/centrum/index.php(60):serialize(Array)#1   {main}在第60行的/opt/lampp/htdocs/web/centrum/index.php中抛出

and this question声明我收到了错误,因为我无法序列化

1 个答案:

答案 0 :(得分:1)

数组($ ImageFound,$ MusicFound,$ VideoFound)实际上是Throwable个对象的数组,而不是字符串。向阵列添加新文件时,您希望将对象转换为字符串。您可以通过从对象获取文件路径来实现此目的。

将array_push调用更新为:

SplFileInfo

根据您尝试做的事情,您可能希望使用SplFileInfo上的任何方法来获取文件路径,文件名等。