I am trying to get my php video handler file to serve up videos and am testing it on the file handler page itself without luck.
Weird behaviour: A window just pops up asking me if I want to download the file, even if I delete the readfile(). If I change video/x-flv to video/flv then a player loads in my window but the file does not play. Also if I take away that header all together my browser crashes.
I figured this script should place the video in the browser at the least and have it be playable in the browser if I am testing the file directly with the browser. The file path is correct after the query... Also the file is outside of my web directory but I don't think that should matter because I can serve images outside the directory successfully using a similar script. Anyone have any ideas?
$sql="SELECT file_name FROM video WHERE vid_id=?";
$stmt=$conn->prepare($sql);
$result=$stmt->execute(array($ID));
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
$file_name = $row['file_name'].".flv";
}
$path="/home/g/Desktop/processed/".$file_name."";
//check if image is readible and type
if (is_readable($path)) {
header('Content-Length: '.filesize($path)); // provide file size
header("Expires: -1");
header('Content-Type: video/x-flv');
$content=readfile($path);
}
else {
error_log("Can't serve video: $file_name");
}
Streaming doesn't work like that.
To have a proper streaming you need to use a Player like: http://flowplayer.org/
If you just send the content of the video, the browser will pop up the save dialog
I was unable to use php to pass videos to OS player. I switched to flow player and things are working.
Related
I have a single image file on my server which its content changes every 100 ms.
I can load and serve this image as usual:
$image = file_get_contents('path/to/image.jpg');
header('Content-type: image/jpeg');
echo $image;
<img src="image.php">
Thus, every time the user updates the screen (pressing F5 for example) the server would reply a different image. I can also use Java Script (using setInterval for example) to update the image continuously so the users needn't to update the screen themselves.
However, I need to serve this image as a CONTINUOUS FLOW such as a LIVE VIDEO STREAM in order to be shown as an HTML5 video instead of a static image.
Some examples I`ve found so far use PHP-FFMpeg library for stream videos. It turns out that those examples require that I have a video file at hand (a file in the OS or a URL to a file) instead of a single (dynamic) image as I've described above.
I found this example for how to use PHP to streaming. It looks promisssing. But again the code supposes I have a video file url which I haven't.
I'm wondering if is it possible to adapt this code to my needs. For example, how to adapt the setHeader() method to the scenario where there are no begin and end? And considering that I have loaded the image contents using file_get_contents or so, how to change stream() properly? Or, at other hand, is there other way to serve this image as a video stream?
The code below should reload the image ever 200 ms. Adding a random number avoids any potential caching which is unlikely since you are requesting a PHP page.
<html>
<header>
<script>
function ReloadImage()
{
var image_element = document.getElementById('image_id');
image_element.src = 'image.php?rand=' + Math.random();
}
setInterval(ReloadImage,200);
</script>
</header>
<body>
<img src="image.php" id="image_id">
</body>
</html>
Well, I just found a solution for my needs:
$identifier = "an_identifier";
//set headers
header('Accept-Range: bytes');
header('Connection: close');
header('Content-Type: multipart/x-mixed-replace;boundary=' . $identifier);
header('Cache-Control: no-cache');
// loop to continuously serve an image
while(true) {
$image = load_image_contents();
echo "--" . $identifier . "\r\nContent-Type: image/jpeg\r\nContent-Length: ".strlen($image)."\r\n\r\n".$image;
flush();
usleep(50000);
}
On the browser side I just set a regular image tag:
<img src="image.php">
I want to save an image from an URL to the user's local machine in PHP. Is this possible? I've been researching for a while, and I can't seem to find the answer. This is my code
function Save()
{
header('Content-Type: image/png');
header('Content-Disposition: attachment; filename="thumbnail.png"');
imagepng("url?");
}
If there is a download link that the user clicks on, you can have it re-direct to the url of the file you want to serve directly, the client browser should handle the rest.
If the file needs to be re-served from your system having a php file that sets those headers and then echo's the imagepng output should do it.
I've successfully implemented some PHP to download an image:
PHP:
<?php
$image = "path/to/image.jpg"
header('Content-disposition: attachment; filename=image.jpg');
header('Content-type: image/jpeg');
readfile($image);
?>
Here is an example image that I've saved to my server: http://www.thenally.com/work/php-tests/header/files/herzog.jpg
If I view the image in the browser, then right-click to save, it acts as a typical image on my local Windows box (ie. viewable in Windows Photo Viewer). When I download the image using the PHP script above, the image can't be viewed in the same way, although the filesize is accurate and I can open it in Photoshop. I don't know much about image encoding/decoding etc but obviously the file changes when I download using the method above.
EDIT:
The Windows Photo Viewer error is as follows:
"Windows Photo Viewer can't open this picture because either Photo Viewer doesn't support this file format, or you don't have the latest updates to Photo Viewer".
There are no PHP errors in my log file, but this came up in my Chrome console:
"Resource interpreted as Document but transferred with MIME type image/jpeg: "http://thenally.com/work/php-tests/header/index.php".
try this
header('Content-Transfer-Encoding: binary');
I ran a similar issue and managed to fix it by removing any blank space/new lines between PHP tags.
Basically had to do this:
<?php
//stuff
?>
--REMOVE THIS SPACE--
<?php
//stuff
?>
Just make sure you do not have any blank spaces before any PHP tags and you should be good!
This seems like it should be simple. I have a set of files I have to store outside of the webroot and have an access script to call them. I also need to sometimes tell a PDF that must be called via this proxy script to open at a specific page. Releveant part of the script below:
header('Content-type: application/pdf');
header('Content-Length: ' . filesize($file));
header('Content-Disposition: attachment; filename="'.$file_name.'"');
readfile($file);
$file_name is just the basename() of the file, and $file is the path to the file, with #page=2, or #page=10, or whatever appended to it. If I remove the hashtag portion, the script works fine and the PDF opens with no errors. When the hashtag portion is there, all the programs tell me the PDF has been corrupted and can't be open.
I can't seem to find anything on here or Google as to what I need to do. Do I need to set an additional header to simulate the hash tag? Use exec() to call some command line code instead of using readfile()?
Any insight would be greatly appreciated.
You have to append the #page=2 (or whatever page you want to open) to the URL in the browser, not the filename in the proxy-script.
You currently try to open a file myFile.pdf#page=2 from the filesystem that does not exist as the filename is myFile.pdf
The feature to open a pdf-file on a specific page on the other hand is implemented in the browser or it's PDF-plugin. Therefore the information which page to open has to be given to the browser via the URL. So you should call your proxyscript like this: http://example.com/proxy.php?myFile.php#page=2
Update:
If you want to download the file and open it at a specific page every time the file is opened from the local file-system of the user, you will have to edit (or recreate) the PDF-File.
My site uses bookmarklets to gather data from external sites, kinda like Pinterest. I'm concerned about security and want to move the images the bookmarklet gathers from the doc root up one level. My script has some hefty security checks in place, but I want to add this as a last line of defense.
How do I access my images within my script? Obviously using ../userimages/id/image.jpg wont work. I'm using Apache.
Thanks!
Proxy the image
You would use a proxy script to feed the images through like the following example:
// open the file in a binary mode
$name = '../userimages/id/image.jpg';
$fp = fopen($name, 'rb');
// send the right headers
header("Content-Type: image/png");
header("Content-Length: " . filesize($name));
// you may like to set some cache headers here
// dump the picture and stop the script
fpassthru($fp);
exit;
This example is from the PHP manuals fpassthru() page. You would save this script somewhere in your servers document root/httpdocs folder.
"Spoofing" the URL to the image
The easiest way to give the PHP file the appearance of being an image file to a user/browser is to use Apaches mod_rewrite. Usually I use a URL structure something like this:
http://www.example.org/image-id/image.png
Where image-id is the unique identifier for that particular image. This way the file has the correct extensions of an image instead of .php.