sponji Posted July 30, 2011 Share Posted July 30, 2011 I'm trying to make a playlist output from a directory of movie clips. Here is what I have came up with so far. <?php $dir = './motion'; $url = 'http://192.168.0.5/motion/'; $playlist = array( 0 => array( 0 => array( 'src' => $url$file, 'type' => 'video/flv', ), 'config' => array( 'title' => "Movies", 'poster' => 'uebere.png') ), ); //$playlist = array(); if (is_dir($dir)) { $dp = opendir($dir); while (false !== ($file = readdir($dp))){ if ('flv'==strtolower(substr($file, -3))) { $playlist[] = $url$file; } } closedir($dp); } echo json_encode($playlist); ?> I need it to display an output like this. [{"src":"http:\/\/192.168.0.5\/motion\/36-20110729205848.flv","type":"video\/flv","title":"Movies","poster":"uebere.png"},{"src":"http:\/\/192.168.0.5\/motion\/37-20110729210403.flv","type":"video\/flv","title":"Movies","poster":"uebere.png"}] Quote Link to comment https://forums.phpfreaks.com/topic/243271-need-help-with-my-php-script-please/ Share on other sites More sharing options...
silkfire Posted July 30, 2011 Share Posted July 30, 2011 Numeric keys that are gapless don't need to be entered: array(0 => 'item') is the same thing as array('item') Use glob('*.flv') to get a filtered array with all FLV files in a certain folder. Quote Link to comment https://forums.phpfreaks.com/topic/243271-need-help-with-my-php-script-please/#findComment-1249408 Share on other sites More sharing options...
sponji Posted July 30, 2011 Author Share Posted July 30, 2011 I was REALLY tired when I wrote that first post. Here is my 2nd attempt. (Just woke up off 20 hours of staring at php) Bare with me. Say I have a directory with 3 files "movie1.flv" "movie2.flv" "movie3.flv" The output I am going for is. [{"0":{"src":"http:\/\/192.168.0.5\/motion\/movie.flv","type":"video\/flv"},"config":{"title":"Movies","poster":"uebere.png"}},{"0":{"src":"http:\/\/192.168.0.5\/motion\/movie2.flv","type":"video\/flv"},"config":{"title":"Movies"}},{"0":{"src":"http:\/\/192.168.0.5\/motion\/movie3.flv"},"type":"video\/flv"},"config":{"title":"Movies","poster":"intro.png"}}] How do I write these url's into a undefined variable in an array? <?php $dir = './motion'; $url = 'http://192.168.0.5/motion/'; $playlist = array( '0' => array( '0' => array( 'src' => "$url$file", 'type' => 'video/flv', ), 'config' => array( 'title' => "Movies", 'poster' => 'uebere.png') ), ); //$playlist = array(); if (is_dir($dir)) { $dp = opendir($dir); while (false !== ($file = readdir($dp))){ if ('flv'==strtolower(substr($file, -3))) { $playlist[] = "$url$file"; } } closedir($dp); } echo json_encode($playlist); ?> Quote Link to comment https://forums.phpfreaks.com/topic/243271-need-help-with-my-php-script-please/#findComment-1249510 Share on other sites More sharing options...
sponji Posted July 30, 2011 Author Share Posted July 30, 2011 Thanks for your reply silkfire. I took your advise and am gaining ground now. <? $url = 'http://192.168.0.5/motion/'; $files = glob('*.flv'); $files = array_filter($files, 'is_file'); $playlist = array( '0' => array( '0' => array( 'src' => "$url$files[1]", 'type' => 'video/flv', ), 'config' => array( 'title' => "Movies", 'poster' => 'uebere.png') ), ); for ($i=0; $i < 10; $i++) { $playlist[$i]['src'] = "$url$files[$i]"; } echo json_encode($playlist); ?> I have an output like this now.. [{"0":{"src":"http:\/\/192.168.0.5\/motion\/37-20110729210403.flv","type":"video\/flv"},"config":{"title":"Movies","poster":"uebere.png"},"src":"http:\/\/192.168.0.5\/motion\/36-20110729205848.flv"},{"src":"http:\/\/192.168.0.5\/motion\/37-20110729210403.flv"},{"src":"http:\/\/192.168.0.5\/motion\/38-20110730054357.flv"},{"src":"http:\/\/192.168.0.5\/motion\/39-20110730055121.flv"},{"src":"http:\/\/192.168.0.5\/motion\/40-20110730055815.flv"},{"src":"http:\/\/192.168.0.5\/motion\/41-20110730060257.flv"},{"src":"http:\/\/192.168.0.5\/motion\/42-20110730060802.flv"},{"src":"http:\/\/192.168.0.5\/motion\/43-20110730061421.flv"},{"src":"http:\/\/192.168.0.5\/motion\/44-20110730062055.flv"},{"src":"http:\/\/192.168.0.5\/motion\/45-20110730062633.flv"}] I am just missing the "0": before eacho {"src": line. Any help? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/243271-need-help-with-my-php-script-please/#findComment-1249552 Share on other sites More sharing options...
sponji Posted July 30, 2011 Author Share Posted July 30, 2011 <?php $url = 'http://192.168.0.5/motion/'; $files = glob('*.flv'); $files = array_filter($files, 'is_file'); $video = 'video/flv'; $poster = 'uebere.png'; $title = 'Movies'; $logo = "logo.png"; $playlist = array( '0' => array( '0' => array( 'src' => "$url$files[0]", 'type' => "$video", ), 'config' => array( 'title' => "$title", 'poster' => "$logo") ), ); for ($i=0; $i < 3; $i++) { $playlist[$i]['src'] = "$url$files[$i]"; $playlist[$i]['type'] = "$video"; $playlist[$i]['title'] = "$title"; $playlist[$i]['poster'] = "$logo"; } echo json_encode($playlist); ?> I cannot seem to figure out how to add "0": before every {"src": Making the output look like this [{"0":{"src":"http:\/\/192.168.0.5\/motion\/movie.flv","type":"video\/flv"},"config":{"title":"Movies","poster":"uebere.png"}},{"0":{"src":"http:\/\/192.168.0.5\/motion\/movie2.flv","type":"video\/flv"},"config":{"title":"Movies"}},{"0":{"src":"http:\/\/192.168.0.5\/motion\/movie3.flv"},"type":"video\/flv"},"config":{"title":"Movies","poster":"intro.png"}}] and not like this [{"0":{"src":"http:\/\/192.168.0.5\/motion\/36-20110729205848.flv","type":"video\/flv"},"config":{"title":"Movies","poster":"logo.png"},"src":"http:\/\/192.168.0.5\/motion\/36-20110729205848.flv","type":"video\/flv","title":"Movies","poster":"logo.png"},{"src":"http:\/\/192.168.0.5\/motion\/37-20110729210403.flv","type":"video\/flv","title":"Movies","poster":"logo.png"},{"src":"http:\/\/192.168.0.5\/motion\/38-20110730054357.flv","type":"video\/flv","title":"Movies","poster":"logo.png"}] Quote Link to comment https://forums.phpfreaks.com/topic/243271-need-help-with-my-php-script-please/#findComment-1249565 Share on other sites More sharing options...
jcbones Posted July 31, 2011 Share Posted July 31, 2011 Remove: $playlist = array( '0' => array( '0' => array( 'src' => "$url$files[0]", 'type' => "$video", ), 'config' => array( 'title' => "$title", 'poster' => "$logo") ), ); for ($i=0; $i < 3; $i++) { $playlist[$i]['src'] = "$url$files[$i]"; $playlist[$i]['type'] = "$video"; $playlist[$i]['title'] = "$title"; $playlist[$i]['poster'] = "$logo"; } And place this: $count = count($files); for($i = 0; $i < $count; $i++) { $playlist[] = array( array( 'src' => $url.$files[$i] , 'type' => $video ), 'config' => array( 'title' => $title, 'poster' => $logo) ); } Backup your files of course. Quote Link to comment https://forums.phpfreaks.com/topic/243271-need-help-with-my-php-script-please/#findComment-1249637 Share on other sites More sharing options...
sponji Posted July 31, 2011 Author Share Posted July 31, 2011 Something like this then? <?php $url = 'http://192.168.0.5/motion/'; $files = glob('*.flv'); $files = array_filter($files, 'is_file'); $video = 'video/flv'; $poster = 'uebere.png'; $title = 'Movies'; $logo = "logo.png"; $count = count($files); for($i = 0; $i < $count; $i++) { $playlist[] = array( array( 'src' => $url.$files[$i] , 'type' => $video ), 'config' => array( 'title' => $title, 'poster' => $logo) ); } echo json_encode($playlist); ?> This doesn't work. Even putting the json line inside the for loop, returns nothing. Quote Link to comment https://forums.phpfreaks.com/topic/243271-need-help-with-my-php-script-please/#findComment-1249643 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.