0
<!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>

All arrays store huge number of paths on the machine. This script is gonna run on raspberry pi, no MySQL. I tried writing to a file the var_dump

you can think of an array thousands of strings similar to

/home/Desktop/centrum/1.mp3

I tried to save it in a session, it doesn't get saved, is there a way to save it to a session without messing with the data? the data can't be serialized just the simplest way to put in in a session? before playing with a database.

if i write $sm = serialize($MusicFound);

before

if (empty($ImageFound))

I get

Fatal error: Uncaught Exception: Serialization of 'SplFileInfo' is not allowed in /opt/lampp/htdocs/web/centrum/index.php:60 Stack trace: #0 /opt/lampp/htdocs/web/centrum/index.php(60): serialize(Array) #1 {main} thrown in /opt/lampp/htdocs/web/centrum/index.php on line 60

and this question states that I'm getting my error because I cant serialize

Community
  • 1
  • 1
Lynob
  • 5,059
  • 15
  • 64
  • 114
  • 1
    With over 1k reputation you should know by now that formatting your code for readability is going to increase the chance of your question being answered tenfold – scrowler May 26 '16 at 22:23
  • 1
    don't send it. Instead store that list somehow (say, a file), and make you server "serve up" the file content on request when someone asks for `yourserver/filecontent/fileid`, and send that tiny, tiny file id instead of the file's content. – Mike 'Pomax' Kamermans May 26 '16 at 22:24
  • @RobbieAverill isn't readable? and if i paste it to pastebin some users will complain to, for me it's easily readable and there's plenty of empty spaces, is there a rule of thumb i should follow? – Lynob May 26 '16 at 22:45
  • @Mike'Pomax'Kamermans the problem is that I the array cant be printed to a file and cant be serialized, should i try something like`var_export`? haven't tried it yet – Lynob May 26 '16 at 22:49
  • @Lynob Indentation, indentation, indentation. Your indentation should match your nesting. IMO thats way more important than 'plenty of spaces'. – jfadich May 26 '16 at 22:55
  • Why can't the data be serialized? If it can't be serialized then it can't be stored in a session. – jfadich May 26 '16 at 22:57
  • @jfadich roger that about indentation. if i write `$sm = serialize($MusicFound);` before `if (empty($ImageFound))` I get `Fatal error: Uncaught Exception: Serialization of 'SplFileInfo' is not allowed in /opt/lampp/htdocs/web/centrum/index.php:60 Stack trace: #0 /opt/lampp/htdocs/web/centrum/index.php(60): serialize(Array) #1 {main} thrown in /opt/lampp/htdocs/web/centrum/index.php on line 60` - and this http://stackoverflow.com/questions/6242821/why-serialization-of-splfileinfo-is-not-allowed states that I'm getting my error because I cant serialize – Lynob May 26 '16 at 23:15
  • So thats because you have an array of File objects (which can't be serialized), not filepath strings. Try replacing `array_push($ImageFound, $filename);` with `array_push($ImageFound, $filename-> getRealPath());` – jfadich May 26 '16 at 23:20
  • 1
    @jfadich issue solved! if you post an answer, I will accept it, thank you! – Lynob May 26 '16 at 23:24

1 Answers1

1

The arrays ($ImageFound, $MusicFound, $VideoFound) are actually arrays of SplFileInfo objects, not strings. When you add a new file to the array you'll want to convert the object to a string. You can do that by getting the filepath from the object.

Update the array_push calls to this:

array_push($ImageFound, $filename->getRealPath());

Depending on what you're trying to do you may want to use any method on SplFileInfo to get the filepath, filename, etc.

jfadich
  • 6,110
  • 2
  • 20
  • 31