I am pretty new to PHP, but I understand the basics. My problem is I have I have an index page that needs to have a list of the names of videos stored in a different folder. Since there will be over 100 videos in the folder by the time the site is live, I want the video names to be clickable, and when clicked the video plays.
<?php
$videopath = 'videos';
$videoExts = array('webm'=> 'video/webm','mp4'=>'video/mp4','mpeg'=>'video/mp4','ogv'=>'video/ogv');
$directory = "/videos";
$phpfiles = glob($directory . "*.html");
if ($handle = opendir($videopath)) {
while (false !== ($file = readdir($handle))) {
$info =pathinfo($file);
$ext = strtolower($info['extension']);
echo $file . " \n " . "\n <br> \n";
if (array_key_exists($ext, $videoExts)) {
?>
<div class="flowplayer" data-swf="flowplayer.swf" data-ratio="0.4167">
<video>
<source type="<?php echo $videoExts[$ext]; ?>" src="<?php echo "$videopath/$file"; ?>">
</video>
</div>
<?php
}
}
closedir($handle);
} else {
echo "no dir";
}
?>
This code will list the video name (in text), and play each video. What can I do to modify this so that the video names become clickable and once clicked open the appropriate video?
Replace:
<div class="flowplayer" data-swf="flowplayer.swf" data-ratio="0.4167">
<video>
<source type="<?php echo $videoExts[$ext]; ?>" src="<?php echo "$videopath/$file"; ?>">
</video>
</div>
By:
<?php echo $file; ?>
And in page2.php
<?php
$type = $_GET['type'];
$src = $_GET['src'];
if (file_exists($src)) { ?>
<div class="flowplayer" data-swf="flowplayer.swf" data-ratio="0.4167">
<video>
<source type="<?php echo $type; ?>" src="<?php echo $src; ?>">
</video>
</div>
<?php } ?>
I would do the following (it involves a bit of jQuery). The idea is to have a list of videos and one single video player. Once you click one of them, the video player will be updated.
Steps:
List a link of videos, using the HTML5 data attribute to store the source and extension information of each video. So when clicked, the only video player will be updated with the new settings.
$videopath = 'videos';
$videoExts = array('webm'=> 'video/webm','mp4'=>'video/mp4','mpeg'=>'video/mp4','ogv'=>'video/ogv');
$directory = "/videos";
$phpfiles = glob($directory . "*.html");
// Stores first video of the list
$first_video = array();
if ($handle = opendir($videopath)) {
$counter = 0;
while (false !== ($file = readdir($handle))) {
$info =pathinfo($file);
$ext = strtolower($info['extension']);
echo $file . " \n " . "\n <br> \n";
if (array_key_exists($ext, $videoExts)) {
if ($counter == 1) {
// Save settings of first video
$first_vid['extension'] = $videoExts[$ext];
$first_vid['path'] = $videopath .'/'. $file;
// Save it in array
$first_video[] = $first_vid;
}
?>
<a class="video-item" href="#" data-type="<?php echo $videoExts[$ext]; ?>" data-src="<?php echo "$videopath/$file"; ?>"><?php echo "$videopath/$file"; ?></a><br/>
<?php
}
}
closedir($handle);
} else {
echo "no dir";
}
?>
<div class="flowplayer" data-swf="flowplayer.swf" data-ratio="0.4167">
<video>
<source type="<?php echo $first_video[0]['extension']; ?>" src="<?php echo $first_video[0]['path']; ?>">
</video>
</div>
<script>
// Don't forget to add jQuery in your project for this to work :)
$(function () {
$('.video-item').click(function (e) {
// do nothing
e.preventDefault();
// get info of video being clicked
var extension = $(this).attr("data-type");
var src = $(this).attr("data-src");
// update video player with clicked video
$('.flowplayer video').attr('type', extension);
$('.flowplayer video').attr('src', src);
});
});
</script>
This is a untested code. Hope it helps though.
Related
I am trying to write a script that displays images and/or mp4 files. The image and mp4 file names are stroed in a data table.
When the script runs it needs to loop through the array returned by the mySQL statement. Each returned record has a column called "FileType" which contains either a 1 for an image file or 2 for an mp4 file.
My thinking is that as the do while loop is executed it looks at the content of "FileType" it is trapped by the if statement and uses the appropriate code to display the file. The issue I have is the script is only
displaing rows where the column "FileType" contains 1.
So if the first row contains an image file, it displays, but if the row contains an mp4 file it is not displayed.
Can anyone see where I have gone wrong and how I can fix it so it loops through the array of rows and displays each one.
My code:
do {
$FileType = $row_signage['FileType'];
if($row['DisplayType'] == "P" ) {
$DisplayWdith = "1080";
$DisplayHeight = "1920";
} else {
$DisplayWdith = "1920";
$DisplayHeight = "1080";
}
?>
<div>
<?php if($FileType == 1) { ?>
<img src="/<?=$ImagePath . $row_signage['SignageImageName']?>" class="responsive"/>
<?php } elseif($FileType == 2) { ?>
<div id="video_player">
<video width="<?php echo $DisplayWdith;?>" height="<?php echo $DisplayHeight;?>" autoplay muted playsinline>
<source src="/<?php echo $ImagePath.''.$row['SignageImageName'];?>"/>
</video>
<?php } ?>
</div>
} while ($row_signage = mysqli_fetch_array($fb)); ?>
Code rewrite and working:
<?php while($row = $fb->fetch_array()):
$FileType = $row['FileType'];
if($row['DisplayType'] == "P" ) {
$DisplayWdith = "1080";
$DisplayHeight = "1920";
} else {
$DisplayWdith = "1920";
$DisplayHeight = "1080";
}
?>
<div class="banner-container" data-delay-time="<?=$row['TimeLapse']?>">
<div>
<a>
<?php if($FileType == 1) { ?>
<img src="/<?=$ImagePath . $row['SignageImageName']?>" class="responsive"/>
<?php } else { ?>
<video id="video_player" width="<?php echo $DisplayWdith;?>" height="<?php echo $DisplayHeight;?>" autoplay muted playsinline>
<source src="/<?php echo $ImagePath.''.$row['SignageImageName'];?>"/>
</video>
<?php } ?>
</a>
</div>
</div>
<?php endwhile ?>
I'm trying to loop images from a thumbnails folder (with already presized thumbnails) and link them to their respective full size images. The names will match exactly so I should be able to do this with one loop.
The question is do I need to open both directories before I can start looping through them? In my example, I'm pulling from the Montague Vacations folder which has both the thumbs and full size folders.
$files = array();
$dir = opendir('./assets/vacations/montague'); // open the cwd..also do an err chec
while(false != ($file = readdir($dir))) {
if(($file != ".") and ($file != "..") and ($file != "index.php")){
$files[] = $file; // put in array.
}
}
natsort($files); // sort.
$counter = 0;
// print.
foreach($files as $file) {
$counter++;
echo "<code>";
print htmlentities("
<li>
<!--[if gte IE 9]><!--><a href='#pic-$counter'><!--<![endif]-->
<!--[if lte IE 8]><a href=/userfiles/images/portfolio/web-templates/big/fastedit.png' target='_blank'><![endif]-->
<img src='%base_url%/assets/vacations/montague/thumb/$file alt='' />
<span><b>Preview</b></span>
</a>
<div id='pic-$counter' class='overlay'>
<img src='%base_url%/assets/vacations/montague/full/$file' alt='' />
<div>
<p>Picture caption here.</p>
</div>
<a href='#close' class='close' title='Close'>×</a>
</div>
</li>
");
echo ("<br />\n");
echo ("<br />\n");
echo "</code>";
}
My script uploads the selected pictures to a folder but in my other page I have to see it. But I can't see my uploaded picture.
But if I manually drag and drop the picture from the desktop to the folder then I can see the picture on the page.
This is my code where I want show the uploaded picture:
$image = "../images/accommodatie/".$row2['acco_id']."/";
$images = glob($image."*.jpg");
sort($images);
if (count($images) > 0) {
$img = $images[0];
$img = str_replace("../","", $img);
echo "<a href='acco.php'>
<div>
<div>
<img src='$img'>
</div>";
}
I checked a few times, The folder location is correct, so that can not be the problem.
You're missing and closing double quote and a semi colon after your echo :
if (count($images) > 0) {
$img = $images[0];
$img = str_replace("../","", $img);
echo "<a href='acco.php'>
<div>
<div>
<img src='$img'>
</div>"; //<----- SEMI COLON HERE
}
I have retrieved images from a folder and using fancybox plugin displayed all the images as picassa view.. only problem is how to bring all the retrieved images into one album. All the images are come one by one, because in while loop. I want all the images into a album. that is, if i have 4 images means, make all 4 into 'profilepic' album, here is my code..
PHP
//get all image files with a .jpg extension.
$images = glob($directory . "*.jpg");
//print each file name
foreach($images as $image)
{
echo $image;
?>
<a class="fancybox" rel="group" href="<?php echo $image; ?>"><img src="<?php echo $image; ?>" alt="" />
<?php
//echo "<a class='fancybox' rel='group' href='$image'><img src='$image' alt='' /></a>";
}
?>
SCRIPT
<script type="text/javascript">
$(document).ready(function() {
$(".fancybox").fancybox();
});
</script>
I am retrieving images from a directory using php. Images are displayed on PHP page (getUser.php) but how can these images be display on any HTML.
getUser.php
<?php
$dir = 'images';
$file_display = array('jpg','jpeg','png','gif');
if (file_exists ($dir) == false) {
echo 'Directory \'', $dir, '\' not found!';
}
else{
$dir_contents = scandir($dir);
foreach($dir_contents as $file) {
$file_type = strtolower(end(explode('.', $file)));
If($file !== '.' && $file !== '..' && in_array($file_type, $file_display) == true) {
echo '<img src="', $dir, '/', $file, '" alt="', $file, '" />';
/*echo '<td>';
echo '<img src =';
echo $dir."/".$file;
echo '/>';
echo '</td>'; */
}
}
}
?>
one.html
I want this array of images to be displayed in html (div). That is the source of the image tag takes reading from the PHP file. I have done this in ASP.NET but can't figure it out in PHP Is there anything like
<img src="images/vimages/<%=row["image"] %>" ASP.NET
this PHP.
<div id="pics">
<img src="img/...jpg" alt="" />
</div>
The above div will be dynamically populating using getUser.php file.
If I understand your question, you are looking for some template system in php.
Firstly, there is one already in the php. PHP allows you to mix html and php code using tags.
So you can create something like:
<?php if ($images): ?>
<?php $counter = 1 ?>
<ul>
<?php foreach ($images as $image): ?>
<li id="item-<?php echo $counter++ ?>">
<img src="<?php echo $image['source']?>">
</li>
<?php endforeach ?>
</ul>
<?php endif?>
While it is good practice to separate application logic and html code, pure php is not the best tool for this job. Fortunately, there is plenty of tools on web, for example
Smarty or Latte
Consider the following:
ASP.NET
<img src="images/vimages/<%=row["image"] %>">
PHP
<img src="images/vimages/<?=$row["image"]?>">