In the same way that it's possible to serve up images with php, for use in CAPTACHAS and such, is it possible to do the same with audio files?
I've tried this
<?php
$track = "sometrack.mp3";
if(file_exists($track)) {
header('Content-type: audio/mpeg');
header('Content-length: ' . filesize($track));
header('Content-Disposition: filename="sometrack.mp3"');
header('X-Pad: avoid browser bug');
header('Cache-Control: no-cache');
print file_get_contents($track);
} else {
echo "no file";
}
I'm using Safari, which can play MP3 files. It's kicking Safari into the right mode, I get the Quicktime controls for a few seconds, and then "No Video".
I'm trying to protect files from unauthorized download in case you're wondering why I'd want to do this.
Your Content-Disposition should be:
header('Content-Disposition: attachment; filename="sometrack.mp3"');
Not sure if that's the problem though. I would also recommend using readfile to output the file:
readfile($rSong);
Also, it can't hurt to use an exhaustive Content-Type header, and set the Content-Transfer-Encoding:
header("Content-Transfer-Encoding: binary");
header("Content-Type: audio/mpeg, audio/x-mpeg, audio/x-mpeg-3, audio/mpeg3");
try using This Class it supports download resume and speed limit believe me u need it as an owner of mp3 downloads website
Per the discussion here, plus readfile() from the PHP site for PHP 4, 5, 7 & 8, I came up with this:
if (file_exists($file)) {
header('Content-Description: File Transfer');
header("Content-Transfer-Encoding: binary");
header('Content-Type: audio/mpeg');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: '.filesize($file));
readfile($file);
exit;
}
Related
I am facing a problem when i am trying to download a certain file from my localhost. Everytime i download a file, either a .docx, a .pdf or a .png it is always corrupt. I have spend multiple hours on finding a solution but nothing seems to work.
This is the script im running :
$file_download="loonadministratie/{$loon_id}/{$file}";
if (file_exists($file_download)) {
header('Content-Description: File Transfer');
header("Content-Type: application/octet-stream/loonadministratie/{$loon_id}/");
header('Content-Disposition: attachment; filename='.basename($file_download));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file_download));
readfile($file_download);
exit;
}
Thanks in forehand
Try
<?php
if (ob_get_level()) ob_end_clean();
?>
It may be the issue
Im trying to workout if i can stream PDF files from behind the web root via an image/pdf viewer. Google's documentation says to embed a PDF in an HTML page you just use html and include an embed and point to the file.
http://docs.google.com/viewer?url=YourDocumentUrlHere
However for the purposes of security I am using a document/image script that streams the file to the browser and therefore the filepath remains hidden from the view of the user and unable to be accessed by google documents.
The output sent by the document image script is in the form
header('Content-type: application/pdf');
readfile("/home/******/*****/images/enquiries/23251/23251-1.pdf");
can anyone help me with this?
JUST IN CASE ANYONE WAS WONDERING... i added this to the image server script (imageserve.php)
if ($type = "application/pdf") {
header('Content-Description: File Transfer');
header('Content-Type: application/pdf');
header('Content-Disposition: inline; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Accept-Ranges: bytes');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
} else
and then on the page where i wanted to display the pdf i added
$temp = "home/..../..../development/imageserve.php?filename=xxxxxx";
<embed style="width:1200px; height:730px;" name="plugin" src="$temp" type="application/pdf">
I have the following code for downloading files automatically
at the click of a submit button, everything seems to work fine;
the file downloads in the rigth format, right size, right name, but when I
want to open it, I get an error, the file cannot be read, what could be the
problem?
$file=mysql_fetch_assoc($sel);
$file=$file['downloadlink'];
header('Content-Type: "application/octet-stream"');
header("Content-Transfer-Encoding: Binary");
header("Content-length: ".filesize($file));
header("Content-disposition: attachment; filename=\"".basename($file)."\"");
readfile($file);
you could try tweaking this function from the readfile() comments:
function DownloadFile($file) { // $file = include path
if(file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
}
}
I have an addition for this. If the file size is very big, it'll download empty file which we cannot open at all. That is not a problem with 'readfile' function itself. The problem is reading large files into memory. So, for preventing that kind of issues, we have to use 'ob_end_flush()' immediately before to the 'readfile' function for turning off output buffer.
Hope this tip will save someones time. :)
Can anyone help me to write code on PHP to download Excel File from the server.
I have created below codes using header and readfile but the downloaded file was corrupted.
//content type
header('Content-type: application/vnd.ms-excel');
//open/save dialog box
header('Content-Disposition: attachment; filename='.$fileName);
//read from server and write to buffer
readfile($reportPath);
Can anyone help me on the best way to download existing Excel file from the server.
Please see below image of data after downloaded
ob_clean();
put that code before all header declaration
I suggest to use the X-SENDFILE header. https://tn123.org/mod_xsendfile/
I use something like the following:
header("Content-Description: File Transfer");
header("Content-Type: application/octet-stream");
header('Content-Disposition: attachment; filename="'.basename($path).'"');
header("Content-Transfer-Encoding: binary");
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header("Content-Type: application/force-download");
header("Content-Type: application/download");
header("Content-Length: ".filesize($path));
readfile($path);
exit;
Make sure you're not outputting anything before or after the readfile.
I have a piece of code that allow users download file from server (document such as docs,docx,pdf etc).
Users can download files but it has some errors like the files were broken. For example, a MS Word file after download need to recovery to read content.
I wonder that if there is any mistake in this code (or problem when uploading?).
$size_of_file = filesize($download_path);
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . $file_name);
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . $size_of_file);
//read file from physical path
readfile($download_path);
Did you try like this ?
<?php
header("Content-type: application/vnd.ms-word");
header("Content-Disposition: attachment; Filename=SaveAsWordDoc.doc");
?>
I found the root of the problem, I hav some extra spaces after php close tag. Thank you guys.