Jump to content

Need help with my php script - please!


sponji

Recommended Posts

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"}]

Link to comment
https://forums.phpfreaks.com/topic/243271-need-help-with-my-php-script-please/
Share on other sites

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);

?>

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

 

<?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"}]

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.

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.