Not able to display video in PHP - php

On my site, users are able to upload videos.
And I am storing that videos in a folder and their path in the mysql database.
I want to retrieve that videos from the folder and get display on the page.
That folder also contains images.
I am showing that images also.
Images are getting displayed. And videos only play audio, and not the video. And also, I have only two videos in my database and one image. Then also 3 video files and 3 image are getting display, from which two images are displayed with alternate text.
Following is the code I tried in PHP:
if ($db_found)
{
$query ="SELECT * FROM `files`";
$result = mysql_query($query,$db_handle);
echo "<div class='product'>";
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$src=$row['File'];
echo "<img src='$src' height='200' width='200' alt='Image not Found'/>";
echo "<video width='200' height='200' autoplay controls>
<source src='$src' type='video/mp4'>
</video>";
}
echo "</div>";
}

You need to check which are videos and which are images using something like:
$src=$row['File'];
if (preg_match("/\.jpg$/i",$src)){
echo "<img src='$src' height='200' width='200' alt='Image not Found'/>";
}else{
echo "<video width='200' height='200' autoplay controls> <source src='$src' type='video/mp4'> </video>";
}
Edit
Oh and please stop using the deprecated mysql functions
Choose a more modern method of connecting to the db

You really should add (at the very least) some way to separate your videos and images. For example, a second column in your files table that tells you if the file in question is a video or image.
Then you can simply do something like this:
SELECT * FROM `files` WHERE `filetype` == 1
Where 1 = image, 2 = video, etc.
Then you can simply process that array as you please:
<?php
foreach($images as $image) { ?>
<img src='<?= $image["src" ?>' alt=''>
<?php } ?>
And
<?php
foreach($videos as $video) { ?>
<video id="video" preload="auto" autoplay="autoplay">
<source src="<?= $video["src"] ?>.mp4">
</video>
<?php } ?>
Note: The fact that only audio is playing on your videos has NOTHING to do with your code.

Related

How do I populate Image gallery using PHP on html page, including image code/classes either side?

I am creating a webpage that has a photo gallery, which is a lightbox, that uses a lot of images - around 80-150 per page. The number of the images changes week by week, and I would like to have the website automatically populate the image gallery from the images in a subfolder, whilst including the code attached to the image to make it display correctly.
For example, this is what each images code will look like. And please note that i'll need the image located twice on each line.
<a data-fancybox="gallery" href="images/001.jpg"><img alt="" class="lazy" data-src="images/001.jpg" /></a>
I am attempting to use the below script, but it doesn't appear to be working.
<?php
$dirname = "../images/";
$images = glob($dirname."*.jpg");
foreach($images as $image) {
echo '<a data-fancybox="gallery" href="'.$image.'"><img alt=""
class="lazy" data-src="'.$image.'" /></a>';
}
?>
In this case for each line I have included the .$image. in two locations, inbetween the echo's but it doesn't seem to be working.
If you have any advice for me it will be greatly appreciated.
Your <img /> tag doesn't have the path set in the src attribute which is needed to render the image by the browser. It only has data-src attribute filled.
Should be:
<?php
$dirname = "../images/";
$images = glob($dirname."*.jpg");
foreach($images as $image) {
echo '<a data-fancybox="gallery" href="'.$image.'"><img alt=""
class="lazy" src="'.$image.'" data-src="' . $image . '" /></a>';
}
?>

Loop through a directory and generate a page of embedded HTML5 videos

Using PHP, how do I recursively loop through a directory of mp4 files and convert the output to produce a page containing embedded HTML5 videos?
The PHP code that I'm working with is:
$files = scandir('folder/');
foreach($files as $file) {
//do your work here
}
<video width="400" controls>
<source src="mov_bbb.mp4" type="video/mp4">
Your browser does not support HTML5 video.
</video>
It is possible
so the code will be something like this:
<?php
$videodir = opendir('video_directory');
while(false !== ($filename = readdir($videodir))){
echo '
<video width="400" controls>
<source src="video_directory/'.$filename.'" type="video/mp4">
Your browser does not support HTML5 video.
</video>'
}
?>
If your intent is to simply mix the PHP logic and HTML together than the code is simply:
$files = scandir('folder/');
foreach($files as $file) {
echo '<video width="400" controls>
<source src="' . basename( $file ) . '" type="video/mp4">
Your browser does not support HTML5 video.
</video>';
}
Although I find it cleaner to have the logic in a separate file and do it like this:
<?php foreach($files as $file) { ?>
<video width="400" controls>
<source src="<?=basename( $file )?>" type="video/mp4">
Your browser does not support HTML5 video.
</video>
<?php } ?>
However both are valid and only preference.
Keep in mind of the security of this issue. For example if a different file is in this directory, this could cause a security issue.

audio not playing in live server codeigniter

below code display the audio, audio tag works perfectly, it play the music in local server, but on live server audio not working, its not playing the music. it is appearing like disabled things in chrome and in mozilla it is not even showing the play button. i think problem is with source url.
<audio controls id = "myaudio">
<source src="<?php echo base_url(); ?>worship/assets/<?php echo $row->language_folder; ?>/<?php echo $row->album_folder; ?>/<?php echo $row->song_name; ?>" type="audio/mp3">
</audio>
Please advise me.
Hope this will help you :
Just change the Media Type OR MIME-type from audio/mp3 to audio/mpeg
<source type="audio/mpeg" src="<?php echo base_url(); ?>worship/assets/<?php echo $row->language_folder; ?>/<?php echo $row->album_folder; ?>/<?php echo $row->song_name; ?>" >
and use base_url like this (just suggestion) ;
<source type="audio/mpeg" src="<?php echo base_url('worship/assets/'.$row->language_folder.'/'.$row->album_folder.'/'.$row->song_name); ?>" >
For more :
https://www.w3schools.com/tags/tag_audio.asp

viewing an image recieved from database in form of an blob datatype to a new page in php

I have made an code which is used to receive image from database and it's working perfectly. But now as user click's on the image it should be opened on a new page,and i have tried few thing but it's not working...
the below code id used to display the image
$query=mysqli_query($conn,"select * from images") or die("unable to
connect");
$i=1;
while($row=mysqli_fetch_array($query,MYSQLI_BOTH))
{
//echo '<img id="my" height="150" width="320" src="data:image;base64,'.$row['image'].' "> ';
echo '
<tr>
<t>'.#$row["id"].'</td>
<a target="_blank" href="09.jpg">
<img height="150" width="150" src="data:image/jpeg;base64,'.base64_encode( $row['image'] ).'"/>
<td>x</td>
</td>
';
$i++;
}
?>
Now here i want is when user click's on the image , a new page will show an enlarged image...
Don’t save image in database, save it as file in server then save the file name into database
You must be save image as two sizes, small and normal, the smal for display in table, when the user click on image, go to normal image
You can save one name in database, example (img1.jpg)
And in images directory save (sm-img1.jpg) and (img1.jpg)
The code
<a href="url/mydirectory/$row['image']" target="_blank">
<img width="150" height="150" src="url/mydirectory/sm-$row['image']" />
</a>
Or
<img width="150" height="150" src="url/mydirectory/sm-$row['image']" onclick="window.open('url/mydirectory/$row['image']', '_blank');" />

How to Display Video from Wordpress Custom Meta Box?

I used this plugin as a starting point to create a custom meta box that allows users to select a featured video. The meta box is working great, and now I am trying to figure out how to display the video in the post. The following code displays the video:
<video controls="controls" preload="auto" width="100%" height="100%">
<source src="<?php
// Retrieves the stored value from the database
$meta_value = get_post_meta( get_the_ID(), 'meta-image', true );
// Checks and displays the retrieved value
if( !empty( $meta_value ) ) {
echo $meta_value;
} ?>" type="video/mp4" />
</video>
That's great. But I want to write a statement that says "if the post has a featured video, display it, if not display the featured thumbnail." Anyone know how to do this?
EDIT: I am getting closer. The following code almost works, but for the posts that have featured images (not videos), it displays an empty video player instead of the featured image. How can I modify the below code so that the featured images work?
<?php
$slam_featured_video = get_post_meta( get_the_ID(), 'meta-image', true );
if (isset($meta_value)) {
echo '<video controls="controls" preload="auto" width="100%" height="100%">
<source src="'. $slam_featured_video. '" type="video/mp4" />
</video>';
} elseif (empty($meta_value)) {
echo the_post_thumbnail('full');
}
?>
You almost got it!
if there is no featured video, you will get back an empty string (""). isset("") = true, so you'll still end up in the featured video block.
Just an empty string by itself will evaluate to false, so just do:
if ($meta_value) {
echo '<video controls="controls" preload="auto" width="100%" height="100%">
<source src="'. $slam_featured_video. '" type="video/mp4" />
</video>';
} elseif (empty($meta_value)) {
echo the_post_thumbnail('full');
}
After some more research and experimentation, I was able to find a solution. The following code works for me. Thanks to #manishie for setting me on the right track.
<?php
$slam_featured_video = get_post_meta( get_the_ID(), 'meta-image', true );
if (!empty($slam_featured_video)) {
echo '<video controls="controls" preload="auto" width="100%" height="100%">
<source src="'. $slam_featured_video. '" type="video/mp4" />
</video>';
} elseif (empty($slam_featured_video)) {
echo the_post_thumbnail('full');
}
?>

Categories