how to download the video using php script - php

In my program I want to add a download option to download the currently straming video. I tried this code:
$psp = "Tom_20_amp__20Jerry_20race-1.flv";
header("Content-type:application/octet-stream");
header("Content-Disposition:attachment;filename=$psp");
But I get this error:
"C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\Tom_20_amp__20Jerry_20race-1-5.flv" is not a valid FLV file.
the video streaming properly. please guide me

Change you're header from :
header("Content-type:application/octet-stream");
To :
header("Content-type: video/flv");
Then you can do :
header("Content-Disposition:attachment;filename=\"$psp\"");
//allways a good idea to let the browser know how much data to expect
header("Content-length: " . filesize($psp) . "\n\n");
echo file_get_contents($psp); //$psp should contain the full path to the video

If you are looking to allow files to be downloaded instead of streamed then something like this should work. You will obviously need to change the paths.
// We'll be outputting a PDF
header('Content-type: application/pdf');
// It will be called downloaded.pdf
header('Content-Disposition: attachment; filename="download.pdf"');
// The PDF source is in original.pdf
readfile('original.pdf');
EDIT
In your case it will be something like this.
// We'll be outputting a video
header('Content-type: video/flv');
// It will be called video.flv
header('Content-Disposition: attachment; filename="video.flv"');
// The PDF source is in original.flv
readfile('original.flv');

Related

Download PDF from Binary SQL SERVER With PHP

I need help with this script. I'm trying to get this binary file stored in a sql server DB . The main problem is that , each time im trying to show it in my browser or to download it, the file is corrupted . Heres my code:
$binary = $row['PDF_FILE_STORED'];
file_put_contents('my.pdf', $binary);
header('Content-type: application/pdf');
header("Content-Transfer-Encoding: Binary");
header("Content-Length: ".filesize($binary));
header("Content-Disposition: attachment;filename=my.pdf");
ob_clean();
flush();
echo $binary;
Is there a problem with the encoding aspect? I just got this warning in my brower's console : "Resource interpreted as Document but transferred with MIME type application/pdf" . Any advices ?
To just download the pdf file, you don't need to save it locally on the server. You don't need to send content-length as that should be done automatically and I would skip the content-transfer-encoding as well.
If there was no output yet, you can also skip dealing with the output buffer.
Try this:
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename=my.pdf');
echo $row['PDF_FILE_STORED'];

Cant download a SWF image or preview it using PHP

Hi Stackoverflow community,
I've been trying to download a swf image from a extern server, which you can reach by a simple url.
Sadly the downloaded SWF wouldn't show up in the browser, so i assume its a broken file. Also I'm not able to preview the SWF file through PHP.
I know I could embed the SWF but i kinda need a screenshot or a downloaded version of it.
Code I've tested to download the SWF to my server:
function save_swf(){
$swf_file = file_get_contents('http://assets.zwinky.com/assets3/avatar/avatar10.8.swf?u=' . $this->username);
file_put_contents(strtolower($this->award . '_' . $this->username . '.swf'), $this->swf);
}
Code which i used to check if the image can be previewn by PHP:
<?php
$data = #file_get_contents('http://assets.zwinky.com/assets3/avatar/avatar10.swf?u=dane');
header("content-type: application/x-shockwave-flash");
header("Content-Disposition: inline;" echo $data);
header("accept-ranges: bytes", true);
header("connection: keep-alive", true);
echo $data;
?>
Does anyone have a idea why it wouldn't work?
Basis SWF im trying to get the file from: http://assets.zwinky.com/assets3/avatar/avatar10.8.swf?u=dane
If you write the data to a file locally on the server, the following should work:
// Write swf file locally using file_put_contents();
$fileName = ""; // The filepath where you've written it to
header("Content-Type: application/x-shockwave-flash",true);
header("Content-Length: {strlen($fileName)}",true);
header("Accept-Ranges: bytes",true);
header("Connection: keep-alive",true);
header("Content-Disposition: inline; filename=$fileName");
readfile($fileName);
Let me know if you need any addition help with this.

Force download file that's not placed on the server

I'm trying to force download a pdf file that I'm generating. I don't need the pdf file to be actually saved on the server.
So when I generate my pdf file, I get the file content. I then encode it with base64. Now the problem is that I need to force download it. I've looked all over the web, but I haven't found any search results that tells me how to do this without the file actually being placed on the site.
I've tried the following code:
header("Content-type: application/pdf");
header("Content-disposition: attachment; filename=\"invoice.pdf\"");
header("Content-Length: ".filesize($pdffile));
readfile(base64_decode($pdffile));
But, it's giving me a corrupt pdf file, (1 kb). The actual file should be around 50kb.
Any ideas, as to what I can try?
readfile trying to output content from file, but you have only data string. Try this instead:
header("Content-type: application/pdf");
header("Content-disposition: attachment; filename=\"invoice.pdf\"");
echo base64_decode($pdffile);
I also suggest rename $pdffile to $pdfcontent for even better clarification.

PHP force download, Not working with variables

When
header('Content-disposition: attachment; filename=1330554893-COVER.jpg');
header('Content-type: jpeg');
readfile('watermarked/1330554893-COVER.jpg');
Is run in a file for example "testdownload.php" It downloads the image
"watermarked/1330554893-COVER.jpg"
and names it
"1330554893-COVER.jpg"
But when I try make the code dynamic to download different files.
header("Content-disposition: attachment; filename={$newFileName}");
header("Content-type: jpeg");
readfile("{$findFile}");
where
$newFileName = "1330554893-COVER.jpg" and $findFile = "watermarked/1330554893-COVER.jpg"
It downloads an image "1330554893-COVER.jpg" but it cannot be opened and I get an error "Windows Photo Viewer can't open this picture because eaither Photo Viewer doesn't support this file format"
Thanks for helping :)
Allrite then, don't use readfile(), try echo file_get_contents after the headers!

Show a PDF files in users browser via PHP/Perl

I want to show my users PDF files. The reason why I use CGI to show the PDF is I want to track the clicks for the PDF, and cloak the real location of the saved PDF.
I've been searching on the Internet and only found how to show save dialog to the users and creating a PDF, not show the files to the users.
What I wanted for is show the users my PDF files, not creating or download the PDF.
Here is what I got form the official PHP documentation:
<?php
header('Content-type: application/pdf');
readfile('the.pdf');
?>
Also my google-search-result perl code:
open(PDF, "the.pdf") or die "could not open PDF [$!]";
binmode PDF;
my $output = do { local $/; <PDF> };
close (PDF);
print "Content-Type: application/pdf\n";
print "Content-Length: " .length($output) . "\n\n";
print $output
if you do it on ruby, please say it to me. But I'm not sure if my server support rails.
Sorry if my code is too far away from the method to show the pdf, since I don't know anything about pdf processing and how to implement this problem.
Lets assume that the users have the Adobe Reader plug-in. So, how to fix my problem?
edit : I want to show plain PDF file. My primary purpose: track my pdf files and use some fancy urls.
edit : Here's my main php code:
<?php
$file='/files/the.pdf';
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="the.pdf"');
#readfile($file);
?>
edit : Now the code is working. But the loading progress bar (on Adobe Reader X plugin) doesn't shows up. Why? Anyone can help me? Here's my main code:
<?php
$file='./files/the.pdf';
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="the.pdf"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($file));
#readfile($file);
?>
edit : All my problems solved. Here's the final code:
<?php
$file = './path/to/the.pdf';
$filename = 'Custom file name for the.pdf'; /* Note: Always use .pdf at the end. */
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="' . $filename . '"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($file));
header('Accept-Ranges: bytes');
#readfile($file);
?>
Thanks! :)
I assume you want the PDF to display in the browser, rather than forcing a download. If that is the case, try setting the Content-Disposition header with a value of inline.
Also remember that this will also be affected by browser settings - some browsers may be configured to always download PDF files or open them in a different application (e.g. Adobe Reader)
$url ="https://yourFile.pdf";
$content = file_get_contents($url);
header('Content-Type: application/pdf');
header('Content-Length: ' . strlen($content));
header('Content-Disposition: inline; filename="YourFileName.pdf"');
header('Cache-Control: private, max-age=0, must-revalidate');
header('Pragma: public');
ini_set('zlib.output_compression','0');
die($content);
Tested and works fine. If you want the file to download instead, replace
Content-Disposition: inline
with
Content-Disposition: attachment
You could modify a PDF renderer such as xpdf or evince to render into a graphics image on your server, and then deliver the image to the user. This is how Google's quick view of PDF files works, they render it locally, then deliver images to the user. No downloaded PDF file, and the source is pretty well obscured. :)
The safest way to have a PDF display instead of download seems to be embedding it using an object or iframe element. There are also 3rd party solutions like Google's PDF viewer.
See Best Way to Embed PDF in HTML for an overview.
There's also DoPDF, a Java based In-browser PDF viewer. I can't speak to its quality but it looks interesting.
You can also use fpdf class available at: http://www.fpdf.org.
It gives options for both outputting to a file and displaying on browser.
There is a simple solution using the embed tag:
<span class="fileShow">
<a href="aa.pdf" onclick="event.stopPropagation();" target="_blank">
<embed style="width:450px; height:300px; max-width:450px; max-height:300px" src="aa.pdf">
</a>
</span>

Categories