Load Video with PHP - php

I am trying to load a video file in php using by selecting the file with _GET.
Here is the code
<?php
$id = $_GET["id"];
$video = "vid" . $id . ".mp4";
if (isset($_GET["id"]) AND $_GET["id"] == $id) {
header('Content-Type: video/mp4');
print file_get_contents($video);
}
else {
echo "File not found.";
}
?>
This is my web link.
http://www.animesour.com/video.php?id=555 This will get the file vid555.mp4
However, when you visit the link it doesn't load the video or play it.
It will load images fine, but doesn't seem to work when it's video.
Basically I need it so that when I link http://www.animesour.com/video.php?id=555 in an I frame, that it will load the video. Thank you in advance.

<?php
if (isset($_GET["id"])) {
$id = $_GET["id"];
$video = "{URL TO VIDEO BASED ON $id}";
echo "<video controls><source src='{$video}' type='video/mp4'></video>";
} else {
echo "File not found.";
}
?>

Related

Displaying Image uploaded to mysql PHP - need to display string

I was trying to figure out how to upload an image into mysql then display it. I got the uploading part set. The problem I am have is displaying it. When I go to display it does one of the following:
Displays no image but only has a little image icon 10x10px image on top left
Displays the whole string of the image and then properly renders the image.
When I remove/comment out the echo $contentIMG statement, nothing displays.
<?php
include 'connections/conn.php';
error_reporting(E_ALL);
// some basic sanity checks
if(isset($_GET['id']) && is_numeric($_GET['id'])) {
$contentIMG = $row_Recordset1['content'];
echo $row_Recordset1['content'];
//echo $contentIMG;
// set the header for the image
//header("Content-type: image/jpeg");
header("Content-type: image/jpeg\n\n");
echo "where is the image #1<br>";
//echo '<img src="data:image/jpeg;base64,'.$contentIMG.'"/>';
echo "where is the image #2<br>";
echo '<img src="data:image/jpeg;base64,'.base64_encode( $contentIMG ).'"/>';
if (isset($contentIMG)){
//echo '<img src="data:image/jpg;base64,'.base64_encode($contentIMG).'" />';
}
//echo mysql_result($result, 0);
// close the db link
mysql_close($result);
}
else {
echo 'Please use a real id number';
}
echo "break <br>";
$img=base64_encode($row_Recordset1['content']);
?>
I looks like this
enter image description here
I may be wrong but I think that when you set your header Content-type to image/jpeg, you should just return the image data (assuming it is stored as a blob in your database)
<?php
header('Content-type: image/jpeg');
echo $contentIMG;
?>
Your code should look like this:
<?php
include 'connections/conn.php';
error_reporting(E_ALL);
// some basic sanity checks
if(isset($_GET['id']) && is_numeric($_GET['id'])) {
$contentIMG = $row_Recordset1['content'];
header('Content-type: image/jpeg');
echo $row_Recordset1['content'];
// close the db link
mysql_close($result);
}
else {
echo 'Please use a real id number';
}
?>

Variable for file directory path not being recognized

so far I've successfully moved an uploaded image to its designated directory and stored the file path of the moved image into a database I have.
Problem is, however, is that the img src I have echoed fails to read the variable containing the file path of the image. I've been spending time verifying the validity of my variables, the code syntax in echoing the img src, and the successful execution of the move/storing code, but I still get <img src='' when I refer to the view source of the page that is supposed to display the file path contained in the variable.
I believe the file path is stored within the variable because the variable was able to be recognized by the functions that both moved the image to a directory and the query to database.
My coding and troubleshooting experience is still very adolescent, thus pardon me if the nature of my question is bothersomely trivial.
Before asking this question, I've searched for questions within SOF but none of the answers directly addressed my issue (of the questions I've searched at least).
Main PHP Block
//assigning post values to simple variables
$location = $_POST['avatar'];
.
.
.
//re-new session variables to show most recent entries
$_SESSION["avatar"] = $location;
.
.
.
if (is_uploaded_file($_FILES["avatar"]["tmp_name"])) {
//define variables relevant to image uploading
$type = explode('.', $_FILES["avatar"]["name"]);
$type = $type[count($type)-1];
$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$rdn = substr(str_shuffle($chars), 0, 15);
//check image size
if($_FILES["avatar"]["size"] > 6500000) {
echo"Image must be below 6.5 MB.";
unlink($_FILES["avatar"]["tmp_name"]);
exit();
}
//if image passes size check continue
else {
$location = "user_data/user_avatars/$rdn/".uniqid(rand()).'.'.$type;
mkdir("user_data/user_avatars/$rdn/");
move_uploaded_file( $_FILES["avatar"]["tmp_name"], $location);
}
}
else {
$location = "img/default_pic.jpg";
}
HTML Block
<div class="profileImage">
<?php
echo "<img src='".$location."' class='profilePic' id='profilePic'/>";
?><br />
<input type="file" name="avatar" id="avatar" accept=".jpg,.png,.jpeg"/>
.
.
.
View Source
<div class="profileImage">
<img src='' class='profilePic' id='profilePic'/><br />
<input type="file" name="avatar" id="avatar" accept=".jpg,.png,.jpeg"/>
.
.
.
Alright, I've finally found the error and was able to successfully solve it!
Simply declare a avatar session variable to the $location variable after updating the table, update the html insert by replacing all $location variables with $_SESSION["avatar_column"] and you are set!
PHP:
$updateCD = "UPDATE users SET languages=?, interests=?, hobbies=?, bio=?, personal_link=?, country=?, avatar=? WHERE email=?";
$updateST = $con->prepare($updateCD);
$updateST->bind_param('ssssssss', $lg, $it, $hb, $bio, $pl, $ct, $location, $_SESSION["email_login"]);
$updateST->execute();
$_SESSION["avatar"] = $location; //Important!
if ($updateST->errno) {
echo "FAILURE!!! " . $updateST->error;
}
HTML:
<div class="profileImage">
<?php
$_SESSION["avatar"] = (empty($_SESSION["avatar"])) ? "img/default_pic.jpg" : $_SESSION["avatar"] ;
echo "<img src='".$_SESSION["avatar"]."' class= 'profilePic' id='profilePic'> ";
?>
.
.
.
Thank you!
try this code
<?php
error_reporting(E_ALL);
ini_set('display_errors','on');
$location = ""; //path
if($_POST && $_FILES)
{
if(is_uploaded_file())
{
// your code
if(<your condition >)
{
}
else
{
$location = "./user_data/user_avatars/".$rdn."/".uniqid(rand()).'.'.$type;
if(!is_dir("./user_data/user_avatars/".$rdn."/"))
{
mkdir("./user_data/user_avatars/".$rdn."/",0777,true);
}
move_uploaded_file( $_FILES["avatar"]["tmp_name"], $location);
}
}
else
{
$location = "img/default_pic.jpg";
}
}
?>
Html Code :-
<div>
<?php
$location = (empty($location)) ? "img/default_pic.jpg" : $location ;
echo "<img src='".location."' alt='".."'> ";
?>
</div>
If it helpful don't forget to marked as answer so another can get correct answer easily.
Good Luck..

Image from blob object not displayed in a page

This is my HTML code for displaying image stored as blob object which is stored in a database.
<tr><td>photo</td><td><img src="image.php?id=<?php echo $employee['id']; ?>" /></td></tr>
This is the image.php file:
<?php
include db.php;
$employee_id = (isset($_GET['id']) && is_numeric($_GET['id'])) ? intval($_GET['id']) : 0;
try
{
$sql = "SELECT photo
FROM employee where id = $employee_id limit 1";
$s = $pdo->prepare($sql);
$s->execute();
}
catch (PDOException $e)
{
$error = 'Employee not found' . $e->getMessage();
include 'error.html.php';
exit();
}
$employee = $s->fetch();
$image = $employee['photo'];
header('Content-Type: image/jpeg');
echo $image;
?>
When I look at the HTML code in Firebug, I can see
<img src="image.php?id=9">
But no image is displayed in the table row. Do you know how to find out what is wrong?
When I open the page ...image.php?id=9 in Firefox
It shows a message
The image ...image.php?id=9 cannot be
displayed because it contains errors.
It is strange that every image I have contains an error. I inserted only images with
size smaller than the blob object.
do you get an error?
try to use "exit;" after the echo and remove the closing php tag.
it's optional and you don't really need it (http://php.net/manual/en/language.basic-syntax.instruction-separation.php).
header('Content-Type: image/jpeg');
echo $image;
exit;

How to Displaying an image with path stored in Database?

I have stored images path in database and images in project folder. Now i am trying to view all IMAGES with their PRODUCTS in my HTML template. I have written the following code and its given me an empty images box in the output and when i open that image box in the new tab, it opens the following URL.
http://localhost/cms/1
Kindly tell me the way of referring images in 'img src ' tag.
include 'connect.php';
$image_query = "select * from product_images";
$image_query_run = mysql_query($image_query);
$image_query_fetch = mysql_fetch_array($image_query_run);
if (!$query=mysql_query ("select product_id,name from products")) {
echo mysql_error();
} else {
while ($query_array = mysql_fetch_array($query)) {
echo '</br><pre>';
$product_id = $query_array['product_id'];
echo "<a href='single_product.php?product_id=$product_id' >";
print_r ($query_array['name']);
echo "</a>";
echo "<img src=". $image_query_fetch
['images'] ." alt=\"\" />";
echo '</pre>';
}
}
} else {
echo "You need to Log in to visit this Page";
}
Add your local image path before source
example
echo "<img src='http://localhost/cms/1/". $image_query_fetch['images'] .'" alt=\"\" />";
*Use PHP *
<?php echo "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; ?>
You can use the HTML base tag to set a base URL that relative URLs will be resolved from.
See -> http://www.w3schools.com/tags/tag_base.asp

PHP Image not showing in HTML using img element

Hello there i have a php file with the included:
The image shows properly when i access the PHP file, however when I try to show it in the HTML template, it shows as the little img with a crack in it, so basically saying "image not found"
<img src="http://konvictgaming.com/status.php?channel=blindsniper47">
is what i'm using to display it in the HTML template, however it just doesn't seem to want to show, I've tried searching with next to no results for my specific issue, although I'm certain I've probably searched the wrong title
adding code from the OP below
$clientId = ''; // Register your application and get a client ID at http://www.twitch.tv/settings?section=applications
$online = 'online.png'; // Set online image here
$offline = 'offline.png'; // Set offline image here
$json_array = json_decode(file_get_contents('https://api.twitch.tv/kraken/streams/'.strtolower($channelName).'?client_id='.$clientId), true);
if ($json_array['stream'] != NULL) {
$channelTitle = $json_array['stream']['channel']['display_name'];
$streamTitle = $json_array['stream']['channel']['status'];
$currentGame = $json_array['stream']['channel']['game'];
echo "<img src='$online' />";
} else {
echo "<img src='$offline' />";
}
The url is not an image, it is a webpage with the following content
<img src='offline.png' alt='Offline' />
Webpages cannot be displayed as images. You will need to edit the page to only transmit the actual image, with the correct http-headers.
You can probably find some help on this by googling for "php dynamic image".
Specify in the HTTP header that it's a PNG (or whatever) image!
(By default they are interpreted as text/html)
in your status.php file, where you output the markup of <img src=... change it to read as follows
$image = file_get_contents("offline.png");
header("Content-Type: image/png");
echo $image;
Which will send an actual image for the request instead of sending markup. markup is not valid src for an img tag.
UPDATE your code modified below.
$clientId = ''; // Register your application and get a client ID at http://www.twitch.tv/settings?section=applications
$online = 'online.png'; // Set online image here
$offline = 'offline.png'; // Set offline image here
$json_array = json_decode(file_get_contents('https://api.twitch.tv/kraken/streams/'.strtolower($channelName).'?client_id='.$clientId), true);
header("Content-Type: image/png");
$image = null;
if ($json_array['stream'] != NULL) {
$channelTitle = $json_array['stream']['channel']['display_name'];
$streamTitle = $json_array['stream']['channel']['status'];
$currentGame = $json_array['stream']['channel']['game'];
$image = file_get_contents($online);
} else {
$image = file_get_contents($offline);
}
echo $image;
I suppose you change the picture dynmaclly on this page.
Easiest way with least changes will just be using an iframe:
<iframe src="http://konvictgaming.com/status.php?channel=blindsniper47"> </iframe>

Categories