Cant download a SWF image or preview it using PHP - 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.

Related

Upload file(pdf,doc,jpg,png) to my local server using php with android

I want to select file from my device(android) then upload in local server using php and save its path to my database.
Next i want to download it from that upload location.
how can I do this ? help me.
I would use a http request to your php server here is a full working example:
http://androidexample.com/Upload_File_To_Server_-_Android_Example/index.php?view=article_discription&aid=83&aaid=106
To have the client download I would make a /download.php file and use a get parameter to set the database pk to the url they wanna download. next I would read the file and then use the header to change the PHP's output to file so it causes the client to download via http.
CODE EXAMPLE:
$attachment_location = "filePath";
if (file_exists($attachment_location)) {
header($_SERVER["SERVER_PROTOCOL"] . " 200 OK");
header("Cache-Control: public"); // needed for internet explorer
header("Content-Type: application/zip");
header("Content-Transfer-Encoding: Binary");
header("Content-Length:".filesize($attachment_location));
header("Content-Disposition: attachment; filename=filePath");
readfile($attachment_location);
die();
} else {
die("Error: File not found.");
}

mobi file corrupted after php download

I have some ebooks on my server, that I want people to be able to download. I uploaded them and when I download them via my ftp tool then everything is perfect. when I use my script for the user to download it, then I get the following error in calibre:
MobiError: Unknown book type: '\x00\x00\x00BOOKM'
my script that handles the output of the file is as follows:
$file_url = ABSPATH . $file['file'];
$basename = $story->post_title . $subtitle . '.' . $_POST['type'];
$filename = basename(mb_ereg_replace("([^\w\s\d\-_~,;\[\]\(\).])", '', $basename));
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
header("Content-Disposition: attachment; filename=\"" . $filename . "\"");
readfile($file_url);
die();
Then all mobi readers I got say, that there is a problem with the file. I don't know what is going wrong. Also when I open the files in a text editor they are both different type. The one from my php script looks as follows:
the one that works from my ftp tool looks like this:
Anyone who can help me to find what I am doing wrong? The .epub files by the way are no problem with my script.
I solved this problem by outputting the file as follows:
ob_clean();
flush();
readfile($path);
exit;

display pdf in browser using php

hi so im trying to display a pdf file in the browser using php so i used this code
<?php
$filename = "uploads/docs/Preamble and National Territory.pdf";
header("Content-type: application/pdf");
header("Content-Length: " . filesize($filename));
readfile($filename);
exit;
?>
i juat hardcoded the pdf file name but will change it later my problem is it does not load the pdf file in my browser here is what it says
any idea how to improve my code? or what im doing wrong? thanks in advance

download image file from given google chart api url using php

i want to download image returned by this url using a link like Download and on click of this link download box should appear so user can save image to his/her system. here is the url that return image
http://chart.apis.google.com/chart?chs=300x300&cht=qr&chld=L|0&chl=http%253A%252F%252Fnetcane.com%252Fprojects%252Fyourl%252F3
i don't want to save the image to server is it possible ?
Original Question
You can stream or proxy the file to your users by setting up a simple PHP download script on your server. When user hits the download.php script below it will set the correct headers so that their browsers asks them to save a download. It will then stream the chart image from google to the users browser.
In your HTML:
Download
In download.php:
header('Content-Type: image/png');
header('Content-Disposition: attachment; filename="chart.png"');
$image = file_get_contents('http://chart.apis.google.com/chart?chs=300x300&cht=qr&chld=L|0&chl=http%253A%252F%252Fnetcane.com%252Fprojects%252Fyourl%252F3');
header('Content-Length: ' . strlen($image));
echo $image;
Passing in dynamically generated chart API URLs
In your HTML:
<?php
$url = 'http://chart.apis.google.com/chart?my-generated-chart-api-url';
Download
In download.php:
$url = '';
if(array_key_exists('url', $_GET)
and filter_var($_GET['url'], FILTER_VALIDATE_URL)) {
$url = $_GET['url'];
}
header('Content-Type: image/png');
header('Content-Disposition: attachment; filename="chart.png"');
$image = file_get_contents($url);
header('Content-Length: ' . strlen($image));
echo $image;
No, not really. Since the image is generated at chart.apis.google.com, and you don't have control over that server, you can't make it send the Content-Disposition header with that image; therefore, browsers will display that image.
What you technically could do (but I'm not sure if Google's ToS allows it, better check), is to link to your server, which will proxy the download and add the Content-Disposition: attachment header.
I believe you will not be able to do that.
The closest thing would be to dynamically get the image data using PHP and then serving it with the header Content-Disposition: attachment; filename=qr.png
<?php
$img_data = file_get_contents("http://chart.apis.google.com/chart?chs=300x300&cht=qr&chld=L|0&chl=http%253A%252F%252Fnetcane.com%252Fprojects%252Fyourl%252F3");
header("Content-Type: image/png");
header("Content-Length: " . strlen($img_data));
header("Content-Disposition: attachment; filename=qr.png");
print $img_data;
?>
Code is untested, but I think you get the gist of it.
Hope its what you're looking for.

how to download the video using php script

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');

Categories