include loading webpages in slideshow - php

I am totally new to stackoverflow. I'm trying to adjust an excisting script in which an folder containing photo and video is getting loaded and displayed like a slideshow.
I'd like to add the option to load webpages also. Is there any easy way to do this?
Thank you so much.
This is my code:
<?php
include "class.getFiles.php";
$images = new getFiles();
// list of all files in the images folder (includes videos)
$imageArray = $images->getImageArray();
$sortedImages = new sortFiles();
$sortedImages->sortImageArray($imageArray);
// remove files not in the correct time period
$imageArray = $sortedImages->getImageArray();
$randImage = $sortedImages->randomImageNum();
$fileName = $imageArray[$randImage];
$info = new SplFileInfo($fileName);
?>
<!DOCTYPE html>
<html>
<head>
<title>Fiction Slideshow</title>
<link rel="stylesheet" type="text/css" href="main.css">
</head>
<body>
<?php
if($info->getExtension() == "mp4")
{
echo '<video id="vid" class="videoDisplay" autoplay>
<source src="images/'.$fileName.'" type="video/mp4">
Your browser does not support the video tag.
</video>';
echo '<script type="text/javascript">
var vid = document.getElementById("vid");
vid.addEventListener("ended", function(){
window.location.reload();
});
</script>';
}
else
{
echo '<img class="imageDisplay" src="images/'.$fileName.'" />';
echo '<script type="text/javascript">
setTimeout(function(){
window.location.reload();
}, 30000);
</script>';
}
?>
</body>
</html>
This is the class.getFiles.php file that the other script calls.
<?php
class getFiles{
protected $dir;
protected $imageArray;
function __construct()
{
$this->get_dir();
$this->get_images();
}
protected function get_dir()
{
$this->dir = getcwd();
}
protected function get_images()
{
if(count(scandir($this->dir."/images")) != 2)
{
$this->imageArray = scandir($this->dir."/images");
}
else
{
die("There are no files in the directory");
}
}
public function getImageArray()
{
return $this->imageArray;
}
}
class sortFiles{
protected $sortedImageArray = [];
public function sortImageArray($imageArray)
{
foreach ($imageArray as $imageFile )
{
if($imageFile !== ".." && $imageFile !== ".")
{
$imagePath = $imageFile;
$imageFile = (substr($imageFile, 0, -4));
$BeginningPos = strpos($imageFile, '_');
$beginningDate = (substr($imageFile, 0, $BeginningPos));
$beginningDateformatted = str_replace("-","/", $beginningDate);
$stringToStartTime = strtotime($beginningDateformatted);
$EndingPos = strpos($imageFile, '_', $BeginningPos + strlen('_'));
$EndingPos = $EndingPos + 1;
$EndingDate = (substr($imageFile, $EndingPos));
$EndingDateformatted = str_replace("-","/", $EndingDate);
$stringToEndTime = strtotime($EndingDateformatted);
$time = time();
if($time <= $stringToStartTime && $time >= $stringToEndTime)
{
array_push($this->sortedImageArray, $imagePath);
}
}
}
}
public function getImageArray()
{
if(count($this->sortedImageArray) != 0)
{
return $this->sortedImageArray;
}
else
{
die("There are no files in the time range");
}
}
public function randomImageNum()
{
$imageArrayLength = count($this->sortedImageArray);
$imageRand = rand(0, $imageArrayLength-1);
return $imageRand;
}
}
?>

Well you can save the web pages as links within your media folder. For example if you want to display the web page http://www.example.com, then you can create a file webpage1.txt inside the media folder. This file can have the link http://www.example.com.
In your code you can add a new condition that checks if the file type is txt. If the file type is txt, then your code can read the link inside the file and display the link using an iframe. The following code should work:
else if(strpos($fileName, "weblink") !== false)
{
/** The path to the web page link */
$file_path = (getcwd() . DIRECTORY_SEPARATOR . "images" . DIRECTORY_SEPARATOR . $fileName);
/** Read contents of file */
$web_page_link = file_get_contents($file_path);
/** Display the link in iframe */
echo "<iframe src='".$web_page_link."' width='100%' height='100%'></iframe>";
}
The above code should go above the else statement

Related

How do I retrieve youtube videos from a channel without timing out?

I have this program that retrieves videos from a channel and shuffles them, as you can see, the program gets the channel's name from the variable $playlistId
<?php
session_start();
class PlayList {
public function __construct($id) {
$this->id = $id;
$this->videos = [];
$this->currentVideo = 0;
$this->addVideos('https://gdata.youtube.com/feeds/api/users/' . $this->id . '/uploads?v=2&alt=json');
shuffle($this->videos);
}
public function addVideos($url) {
$cont = json_decode(file_get_contents($url));
foreach ($cont->feed->entry as $entry) {
$video = [];
$video['title'] = $entry->title->{'$t'};
$video['desc'] = $entry->{'media$group'}->{'media$description'}->{'$t'};
$video['id'] = $entry->{'media$group'}->{'yt$videoid'}->{'$t'};
$this->videos[] = $video;
}
foreach ($cont->feed->link as $link) {
if ($link->rel === 'next') {
$this->addVideos($link->href);
break;
}
}
}
public function getId() {
return $this->id;
}
public function nextVideo() {
if ($this->currentVideo < count($this->videos) - 1) {
$this->currentVideo += 1;
} else {
$this->currentVideo = 0;
}
}
public function getVideoTitle() {
return $this->videos[$this->currentVideo]['title'];
}
public function getVideoDesc() {
return $this->videos[$this->currentVideo]['desc'];
}
public function getVideoSrc() {
return "http://www.youtube.com/embed/" . $this->videos[$this->currentVideo]['id'];
}
public function printList() {
foreach ($this->videos as $video) {
echo $video['title'] . '<br>';
}
}
}
?>
<?php
$playlistId = "NAKCollection";
if (!isset($_SESSION['playlist']) || $_SESSION['playlist']->getId() != $playlistId) {
$_SESSION['playlist'] = new PlayList($playlistId);
} else {
$_SESSION['playlist']->nextVideo();
}
?>
<html>
<head>
<title>Video Shuffle</title>
</head>
<body>
<div style="float: right;">
<?php
$_SESSION['playlist']->printList();
?>
</div>
<div>
<?php echo $_SESSION['playlist']->getVideoTitle(); ?>
</div>
<div>
<iframe type="text/html" src="<?php echo $_SESSION['playlist']->getVideoSrc(); ?>" allowfullscreen></iframe>
</div>
</body>
</html>
If the channel has like 300 videos the page times out, how can I retrieve 50 videos at a time like a batch and then another 50 when the first group is done?
I know I can alter php.ini but I don't want to do that now
This is using the deprecated GData API, if you use the new Data API v3, while retrieving channel's uploaded videos, you can set maxresults and get your results with paging, so you can iterate through pages.

Call function php uing jquery

Hello i want to call a function using jquery. I tried a lot of ways and I can't get it.
This my principal webpage.
I'am uploading a file csv and pressing 'Crear' button, it uplaod the file while call the function.
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.3.js"></script>
<script src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
<SCRIPT type="text/javascript">
$(function (){
$('#btnxml').click(function (){
alert("aki");
$('#contenidos').load('csv.php');
});
});
</SCRIPT>
</head>
<body>
<form action="index.php" id="filecsv" method="post" enctype="multipart/form-data">
<input type="file" multiple="multiple" id="file" name="up_csv[]"/>
<input type="submit" value="Cargar" name="btnxml" id="btnxml" /><br />
</form>
<?php
global $archivocsv;
//tipos de archivos permitidos
$extensionxml = array('csv','txt');
//destino
$rutaupcsv = './csv/';
//multicargador de archivos
$vt=0;
for($i=0;$i<count($_FILES['up_csv']['size']);$i++){
for ($j=-1; $j<count($extensionxml); $j++) {
if (strripos($_FILES['up_csv']['name'][$i], $extensionxml[$j])!== false) {
$filename = 'lista';
$destino = $rutaupcsv.$_FILES['up_csv']['name'][$i];
$archivocsv = basename($_FILES['up_csv']['name'][$i]);
move_uploaded_file($_FILES['up_csv']['tmp_name'][$i],$destino);
$vt=$vt+1;
break;
}
$ns=1;
}
}
?>
<div id="contenidos"></div>
csv.php
<?php
echo '<html>';
echo '<head>';
echo '<meta content="text/html;charset=utf-8" http-equiv="Content-Type">';
echo '<meta content="utf-8" http-equiv="encoding">';
echo '</head>';
function makecsv() {
global $archivocsv;
$csv = './csv/' . $archivocsv;
$doc = new DOMDocument();
$row = 1;
$handle = fopen($csv, "r");
# Rows Counter
$csvxrow = file($csv);
$csvxrow[0] = chop($csvxrow[0]);
$anzdata = count($csvxrow);
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
$row++;
#Load Predefined XML Template
$xml2 = $xml;
$xmlruta = './Templates/';
$xml = $xmlruta.$data[1].'.xml';
$doc->load($xml);
$xp = new DOMXPath($doc);
for ($c=0; $c < $num; $c++) {
foreach($xp->query('/ROOT/HEADER/#KEY[. != ""]') as $attrib)
{
$attrib->nodeValue = $data[0];
}
foreach($xp->query('/ROOT/DATA/SAPMES/LOIPRO/E1FKOL/#AUFNR[. != ""]') as $attrib)
{
$attrib->nodeValue = $data[0];
}
foreach($xp->query('/ROOT/DATA/SAPMES/LOIPRO/E1FKOL/#MATNR[. != ""]') as $attrib)
{
$attrib->nodeValue = $data[1];
}
foreach($xp->query('/ROOT/DATA/SAPMES/LOIPRO/E1FKOL/#GAMNG[. != ""]') as $attrib)
{
$attrib->nodeValue = $data[2];
}
foreach($xp->query('/ROOT/DATA/SAPMES/LOIPRO/E1AFFLL/E1FVOL/#MGVRG[. != ""]') as $attrib)
{
$attrib->nodeValue = $data[2];
}
foreach($xp->query('/ROOT/DATA/SAPMES/LOIPRO/E1FKOL/#GSTRS[. != ""]') as $attrib)
{
$attrib->nodeValue = $data[3];
}
foreach($xp->query('/ROOT/DATA/SAPMES/LOIPRO/E1FKOL/#GLTRS[. != ""]') as $attrib)
{
$fecha = new DateTime($data[3]);
$fecha->add(new DateInterval('P1M'));
$attrib->nodeValue = $fecha->format('Y-m-d');
}
}
$name = $data[0] .'-'. $data[1];
$doc->formatOutput = true;
echo $doc->saveXML();
$rutafinal = './XML/';
$doc->save($rutafinal.$name.'.xml');
}
fclose($handle);
echo $anzdata . " XML Creados" . "<br />";
return $data;
}
makecsv();
echo '</html>';
?>
I can't call the function.
it doesn't do anything when i try to call it.
EDIT: I think the problem is in my function. function edite
Javascript can´t play with php directly because JS is client side (only in browser) and PHP is server side (only in browser). What you can do is a PHP file that has the code you want to invoke, and make JS call that page.
Separate the CVS code from the form and make JS call the new php with the CVS php functionality.
jQuery runs in the client's browser whereas your PHP is running on your web server. If you wish to call a PHP function from your jQuery code, your best option is to do so using AJAX.
You can find documentation for implementing an AJAX call in jQuery here: https://api.jquery.com/jQuery.ajax/
You'll need to print the actual hmtl to do the function.
PHP
<?php
print '<script> makecsv() </script>';
?>

Randomize photos from a directory via PHP

I have the following code that is functional that will randomize the photos I have in my 'photos' folder each time the refresh button is clicked. I know this may not be the most efficient way for this to be coded but for my matter it works. I am looking for help regarding my PHP code that will make the photos more random. I currently have 200+ pictures in the folder and often get repeated pictures more than I'd like. What changes to it can I make? (PS. ignore the AJAX/JavaScript I was playing around with)
<html>
<head>
<title>Pictures!</title>
<style type="text/css">
body{ background-color:D3DFDE; }
</style>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
</head>
<body>
<div id='main'>
<?php
function randomimages(){
$dirname = isset($_REQUEST['dir'])? $_REQUEST['dir'] : './photos/';
$numimages = isset($_REQUEST['num'])? $_REQUEST['num'] : 1;
$pattern = '#\.(jpg|jpeg|png|gif|bmp)$#i';
$files = array();
if($handle = opendir($dirname)){
while(($file = readdir($handle)) !== false){
if(preg_match($pattern, $file)){
array_push($files, "<center><img src='" . $dirname . $file . "' alt='' /></br><br/><hr/></center>");
}
}
closedir($handle);
shuffle($files);
}
return implode("<center><br/>", array_slice($files, 0, $numimages)) . "<br/> </center>";
}
?>
<!-- <center><a id="myButton" href="#">MAS PICTURES!</a></center> -->
<center><input type='button' onClick='window.location.reload(true)' value='MAS PICTURES!!!' style="height:200px; width:150px" /></center>
<hr/>
<script type="text/javascript">
$(function() {
$("#myButton").click(function() {
$("#main").load("index.php");
});
});
</script>
<?php echo randomimages(); ?>
<center>Created by: Matt & Joe</center>
</div>
</body>
</html>
You can do the following:
Optimize the code by not reading the directory over and over. What you can do this by reading the directory once (and say then store the entries as an array in APC cache). Set a timeout for this APC key to bust the cache once in a while.
Call the `mt_rand` function with min as `0` and max as `count(array)-1` and access that index.
Generic code to read from directory can be as follows (needs modification to match your needs):
<?php
function &list_directory($dirpath) {
if (!is_dir($dirpath) || !is_readable($dirpath)) {
error_log(__FUNCTION__ . ": Argument should be a path to valid, readable directory (" . var_export($dirpath, true) . " provided)");
return null;
}
$paths = array();
$dir = realpath($dirpath);
$dh = opendir($dir);
while (false !== ($f = readdir($dh))) {
if (strpos("$f", '.') !== 0) { // Ignore ones starting with '.'
$paths[] = "$dir/$f";
}
}
closedir($dh);
return $paths;
}
Provide the directory full path to the variable $dirpath
$image_source_array=scandir($dirpath);
sort($image_source_array);
Use mt_rand function with min as 0 and max as count($image_source_array)-1 and access that index from the array to get the image name
and then access the image with the $dirpath/image name you will get the random image each time
Create function like this it will be the shortest approch
function randomimages() {
$dirname = isset($_REQUEST['dir']) ? $_REQUEST['dir'] : './photos/';
$image_source_array = scandir($dirname);
sort($image_source_array);
$image_count = count($image_source_array) - 1;
$rand_index = mt_rand(3, $image_count);
//Starting with 3 because scandir returns directory also in the 2 indexes like '.' and '..'
$rand_image_path = $dirname . $image_source_array[$rand_index];
return $rand_image_path;
}
For the sake of simplicity and reusability, you might want to use RegexIterator together with DirectoryIterator:
function randomimages($path, $num_images)
{
$images = array();
foreach (new RegexIterator(new DirectoryIterator($path),
'#\.(jpe?g|gif|png|bmp)$#i') as $file) {
$images[] = $file->getPathname();
}
shuffle($images);
return array_slice($images, 0, $num_images);
}
Using:
$path = isset($_REQUEST['dir']) ? $_REQUEST['dir'] : './photos/';
$num_images = isset($_REQUEST['num']) ? $_REQUEST['num'] : 1;
print implode('<br />', randomimages($path, $num_images));

Base an if else statement on what folder an image is from on server?

I've used php to open and list the files in the folder, but I'd like to somehow turn it into an if else statement so that if its from the 'dark' background folder a white logo will be displayed and if its from a 'light' background folder a black logo will be displayed.
<?php
//path to directory to open
$directory = "backgrounds/dark";
$dir_handle = #opendir($directory) or die("Unable to open folder");
while(false !== ($file = readdir($dir_handle)))
{
if($file != '.' && $file != '..')
{
echo "{image : 'http://occa-art.com/backgrounds/dark/".$file."'}, ";
}
}
closedir($dir_handle);
?>
The {image: 'http://' } format is used because the images are listed within the Supersized background script.
---EDIT---
Maybe something like this (untested) to determine which folder the background image is from:
<?php
$doc = new DOMDocument();
$doc->loadHTML($htmlAsString);
$xpath = new DOMXPath($doc);
$nodeList = $xpath->query('//img[#class="bg"]/#src');
for ($i = 0; $i < $nodeList->length; $i++) {
# Xpath query for attributes gives a NodeList containing DOMAttr objects.
# http://php.net/manual/en/class.domattr.php
echo $nodeList->item($i)->value . "<br/>\n";
}
$bg_url = $nodeList->item($i)->value; // not sure about this?
$parent = dirname("$bg_url");
if ($parent == 'dark') { ?>
<img src="<?php bloginfo('template_url'); ?>/images/OCCAIndexHeaderwhite.png" alt="OCCA logo" />
<?php } else { ?>
<img src="<?php bloginfo('template_url'); ?>/images/OCCAIndexHeaderblack.png" alt="OCCA logo" />
<?php } ?>
used code found here and here

Paginate list of files in php

I have php code for list all ".swf" files in a folder. (The name of the files is always:
"99-dd-mm-YY_HH-mm-ss.swf", example: "01-19-06-2011_18-40-00.swf".
When I have more than 500 files in the folder is complicated to see and to refresh the page.
I need paginate the list of files.
<html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="content-type">
<title></title>
<script type="text/javascript">
function submitform()
{
document.myform.submit();
}
</script>
</head>
<body>
<form name="myform" action="some.php" method="post">
<?php
echo "\n<br>\n";
echo "<a href='javascript:this.location.reload();' style='color: #000000; font-weight: normal'>Refresh</a></br>";
echo "<tr>\n<td>\n<a href='javascript:javascript:history.go(-1)'>\n";
echo "<img src='../../inc/img/back.png' alt='Back'";
echo " border=0>\n";
echo "<b> Back</b></a></td>\n";
echo "\n</tr>\n";
echo "\n<br>\n\n<br>\n";
$folder='.';
function order($a,$b){
global $folder;
$directory='.';
return strcmp(strtolower($a), strtolower($b));
}
$folder=opendir($folder);
while($files=readdir($folder)){
$ext = pathinfo($files, PATHINFO_EXTENSION);
if ($ext == 'swf') { //con esta línea saco el nombre index.php del listado
$file[]=$files;
usort($file, "order");
}
}
$n = 0;
foreach($file as $archiv){
$n = $n + 1;
$day = substr($archiv, 3,10);
$day = str_replace("-","/", $day);
$hour = substr($archiv, 14,8);
$hour = str_replace("-",":", $hour);
echo "<img alt='Ver $archiv' src='../../inc/img/video.png'> Video $n, Día: $day, hour: $hour\n ";
echo "<input type='submit' name='xxx' value='$archiv'></td>\n";
echo "\n</tr>\n";
echo "<br>";
}
closedir($folder);
echo "\n<br>\n";
echo "<tr>\n<td>\n<a href='javascript:javascript:history.go(-1)'>\n";
echo "<img src='../../inc/img/back.png' alt='Back'";
echo " border=0>\n";
echo "<b> Back</b></a></td>\n";
echo "\n</tr>\n";
?>
</form>
</body>
</html>
When required to go through lots of folders and files, try the Iterator object. A nice example:
function get_files($dir)
{
$dir = new DirectoryIterator($dir);
$list = iterator_to_array($dir, false);
return array_slice($list, 2);
}
This will get all the file names (if you have php 5.3 or higher) very fast and will do the if dir_exists / file_exists for you! The array_slice so it removes the . and .. directory.
I used this code for simple pagination
<?php
// Include the pagination class
include 'pagination.class.php';
// Create the pagination object
$pagination = new pagination;
// some example data
foreach (range(1, 100) as $value) {
$products[] = array(
'Product' => 'Product '.$value,
'Price' => rand(100, 1000),
);
}
// If we have an array with items
if (count($products)) {
// Parse through the pagination class
$productPages = $pagination->generate($products, 20);
// If we have items
if (count($productPages) != 0) {
// Create the page numbers
echo $pageNumbers = '<div>'.$pagination->links().'</div>';
// Loop through all the items in the array
foreach ($productPages as $productID => $productArray) {
// Show the information about the item
echo '<p><b>'.$productArray['Product'].'</b> 243'.$productArray['Price'].'</p>';
}
// print out the page numbers beneath the results
echo $pageNumbers;
}
}
?>
Here there is pagination class and the example for download:
http://lotsofcode.com/php/php-array-pagination.htm
Thanks for all!
Like #Tessmore said, Spl Iterators are teh awesomesauce. According to the docs, you only need PHP > 5.1 for the basic iterators.
Cross-posting an example --
DirectoryIterator and LimitIterator are my new best friends, although glob seems to prefilter more easily. You could also write a custom FilterIterator. Needs PHP > 5.1, I think.
No prefilter:
$dir_iterator = new DirectoryIterator($dir);
$paginated = new LimitIterator($dir_iterator, $page * $perpage, $perpage);
Glob prefilter:
$dir_glob = $dir . '/*.{jpg,gif,png}';
$dir_iterator = new ArrayObject(glob($dir_glob, GLOB_BRACE));
$dir_iterator = $dir_iterator->getIterator();
$paginated = new LimitIterator($dir_iterator, $page * $perpage, $perpage);
Then, do your thing:
foreach ($paginated as $file) { ... }
Note that in the case of the DirectoryIterator example, $file will be an instance of SplFileInfo, whereas glob example is just the disk path.

Categories