We have some mp3 file collection and flash player used this mp3 file, to track mp3 file listing we render mp3 file using php file below is code
http://www/example.com/getFile.php?fileid=12
<?php
$id = $_REQUEST['fileid'];
// Some code to store download traking and get filename for this id in $filename var.
$filename = getFileName($id);
header('Content-type: audio/mpeg');
header('Content-Disposition: filename=' . $id . '.mp3');
readfile($filename);
?>
But mp3 file size in very big and player get break in IE, for that we also use below code
<?php
$id = $_REQUEST['fileid'];
// Some code to store download traking and get filename for this id in $filename var.
$filename = getFileName($id);
header('Location: '. $filename);
?>
this code working fine but its also chnages current URL
i.e http://www/example.com/getFile.php?fileid=12 to http://www/example.com/files/xyz.mp3
so user can easily download mp3 file direct how i prevent this? using php or other way ?
Let Flash fetch the file. Return just the URL to Flash through the PHP.
(Sorry, didn't read the question properly when I originally posted - I see now that the problem is with IE and large file size)
You could try telling IE the length, and flushing the buffers:
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
Related
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.
<?php
ob_clean();
header('Content-Type: image/jpeg');
header('Content-Length: ' . filesize($file));
$file = '111111-11111.jpg';
$handle = fopen($file, "r");
echo file_get_contents($file);
?>
At one point my php webpage stopped displaying all and any images I had successfully uploaded to MySQL database. I wanted to get down to the roots of this problem and see if it could output a .jpg image stored on HDD and all I get is the same output as when I'm trying access this image from database:
The image [...] could not be displayed because it contains errors.
The image location is correct, there are no whitespaces before header, the output buffer has been cleaned. I have spent days on google searching for the answer, but without luck.
I am quite new to PHP so if you can help, please be as descriptive as you can be, so I could understand.
Thank you in advance.
Be sure that the file you are trying to output is in the correct path (from the code, the same as the PHP script) and that you have the read permissions on the file.
$file = '111111-11111.jpg';
$type = 'image/jpeg';
header('Content-Type:'.$type);
header('Content-Length: ' . filesize($file));
readfile($file);
I have a php file which converts the form data to csv format and then it should get downloaded automatically to the user's local download folder.
$time = time();
$filename = 'exceldownloads/myreport_'.$time.'.csv';
$file = fopen($filename,'w');
fputcsv($file,$rowexcel);
The above code works fine and stores the csv file in the specified folder in server. But my requirement is to download it to a local folder. I have seen many solutions to the above problem, but they are working only if we know the local destination folder. However, My requirement is to make it downloadable to the end-user local download's folder (whose download location Im unaware of). Is there anyway to get it downloaded on to the end user system without specifically mentioning the destination path.
You can export the output of your web page as an attachment, which will be shown as a download to the user. You can do this by outputting appropriate headers right before you make any output to the user.
Here's an example, that creates a download of a CSV file called foo.csv:
header("Content-type: text/csv");
header("Content-Disposition: attachment;Filename=foo.csv");
After outputting the headers, you just output all of the file's data to the page content.
*Edit: * Here's a working snippet, as requested:
header("Content-type: text/csv");
header("Content-Disposition: attachment;Filename=foo.csv");
echo implode(";", $rowexcel) . "\r\n"; // you should expand this accordingly
alternatively, here is another snippet, based on your code:
$filename = 'myreport'.time().'.csv';
$f = fopen($filename,'w');
fputcsv($f,$rowexcel);
header('Content-type: application/csv');
header('Content-Disposition: attachment;filename="'.$filename.'"');
readfile($filename);
If you are not getting any download, make sure that you don't output anything before the header() calls. Also, make sure that you don't have any UTF8 BOM bytes at the beginning of your PHP file, as these can be misinterpreted for output
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.
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');