I have the following code
header("Content-Description: File Transfer");
header('Content-Type: audio/mp3');
header("Content-Disposition: attachment; filename=" . $arrResults['audioLink'] . ".mp3");
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header("Content-Transfer-Encoding: binary");
$strLink = 'http://ag-org.s3.amazonaws.com/members/tele_classes/' . $arrResults['audioLink'];
ob_clean();
flush();
readfile($strLink);
However when the download link is clicked that executes this code it always returns a 0 byte file. All files are set to public in the bucket.
What am I doing wrong here?
Is the .mp3 extension really not part of the filename as it's stored on S3?
You're appending .mp3 to the filename in the Content-Disposition header, but not the URL you're passing to readfile.
Related
I want to offer a PNG image to download only for some users, and the image should not be located anywhere, so the users should not see it's path.
That's why I inserted it to the download.php as a base64 image. The problem is, however the image is offered for download, but it shows 0 B size when downloaded.
$base64strImg = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAxoAAARjCAIAAABnqMWCAAAACXBIWXM...';
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=myimage.png');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
ob_clean();
flush();
readfile(base64_decode($base64strImg));
exit;
Not sure where is the problem, if it can't handle big image or why it can't be downloaded.
I also tried this way:
header('Content-Disposition: attachment;filename="test.png"');
header('Content-Type: application/force-download');
echo base64_decode($base64strImg);
Now the image has correct size, but still, can't be opened after download. The image viewer says unsupported format.
And third option - without decoding, it also has a correct size, but still can't be opened.
header('Content-Description: File Transfer');
header('Content-Type: image/png');
header('Content-Disposition: attachment; filename=test.png');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . strlen($base64strImg));
ob_clean();
flush();
echo $base64strImg;
exit;
According to advice from #Lawrence I updated my code and this way it works:
$resultimg = str_replace("data:image/png;base64,","",$base64strImg);
header('Content-Disposition: attachment;filename="test.png"');
header('Content-Type: image/png');
echo base64_decode($resultimg);
you don't have to do all that you can just upload image and restrict access to it by htaccess or permissions and use readfile with the headers in download.php and check if the user has the permission to download the file .
I Want to add download file in the site, but when I click on download It shows weird character. I dont know what the error is. I have added type application/octet-stream in my code. I think there is problem of permission but I have also set permission in c-panel :
Here is my file download code:
if(isset($_REQUEST["file"])){
$file = urldecode($_REQUEST["file"]);
$filepath = ABSPATH.'/wp-content/themes/xorisk/document_upload/'.$file;
if(file_exists($filepath)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($filepath).'"');
header("Content-Type: mime/type");
header('Expires: 0');
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header('Cache-Control: must-revalidate');
header('Pragma: public');
header("Content-Transfer-Encoding: binary");
header('Content-Length: ' . filesize($filepath));
readfile($filepath);
exit;
I am downloading a WAV file from my remote server (Centos 5.8). using the following PHP script:
header('Content-Description: File Transfer');
header('Content-Type: audio/x-wav');
header('Content-Disposition: attachment; filename=' . basename($realLink));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($realLink));
ob_clean();
flush();
readfile($realLink);
When I try to play the downloaded file using either Windows Media Player or Itunes, it will not play. Even though the downloaded WAV on my local machine has a file size that is the same as the version of the file on the server, the downloaded file's properties show 00:00 length and will not play. Also, the file downloads and plays just fine if I manually download it using ftp. The apparent corruption only happens when I use the above script to download. I would appreciate any help with this.
The following are valid MIME types for WAVs:
audio/vnd.wave
audio/wav
audio/wave
audio/x-wav
You're using application/x-wav. See here and here for references.
This code work for download wav audio.
$ctype ='audio/x-wav'; // file type
$filepath = '/var/www/html/audio.wav';//absolute url
$size = filesize($fichero); // size of file
$name = basename($fichero); // name of file
$fp=fopen($fichero, "rb");
if ($size && $ctype && $fp) {
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: wav file");
header("Content-Type: " . $ctype);
header("Content-Disposition: attachment; filename=" . $name);
header("Content-Transfer-Encoding: binary");
header("Content-length: " . $size);
ob_clean();
fpassthru($fp);
}
I have be trying to figure out what is wrong but every time i download the image and try to open it, it says that the file is corrupt.
$h is the path which is pulled from the database, the $h displays the image on the page successfully but I dont get why it wont download. Any ideas ??
header("Pragma: public"); // required
header("Cache-Control: private",false); // required for certain browsers
header('Content-Length: '. filesize("../".$h));
header('Content-Type: application/octet-stream');
header('Content-Disposition: inline; filename="'.md5($h).$ext.'"');
header('Content-Transfer-Encoding:binary');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
readfile("../".$h);
Maybe try to add the following 2 commands before your readfile line.
ob_clean();
flush();
readfile($file);
These lines were in the example for the PHP docs on readfile.
Try this:
$localPath = realpath("../$h");
if (!file_exists($localPath)) {
exit("Cannot find file located at '$localPath'");
}
header('Pragma: public'); // required
header('Content-Length: '.filesize($localPath));
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.md5($localPath).'.'.$ext.'"');
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0', false);
header('Cache-Control: private', false); // required for certain browsers
readfile($localPath);
exit;
Generally, browsers show the image and pdf files without embedding them in html. I need some codes to make these files not to show in the browsers but make them downloadable like doc files.
Please help me out with this.
This isn't up to you, it is up to the browser.
However, you can make a suggestion as to what to do with it by setting the content-disposition header...
header("Content-Disposition: attachment; filename=\"yourfilename.pdf\"");
Read the doc on the header() function: http://php.net/manual/en/function.header.php
In case this isn't clear... this is for whatever resource is returned by the PHP document. You may need a readfile() in there to do what you are trying to do.
Set a couple of headers:
$filename = ...;
$mime_type = ...; //whichever applicable MIME type
header('Pragma: public');
header('Expires: 0');
header("Content-Disposition: attachment; filename=\"$filename\"");
header("Content-Type: $mime_type");
header('Content-Length: ' . filesize($filename));
readfile($filename);
<?php
header('Content-disposition: attachment; filename=myfile.pdf');
header('Content-type: application/pdf');
readfile('myfile.pdf');
?>
You want to send a content type header to make the browser download the file.
If you aren't' generating it dynamically, you will need to read it off the disk first.
$fullPath = "/path/to/file/on/server.pdf";
$fsize = filesize($fullPath);
$content = file_get_contents($fullPath);
header("Pragma: public"); // required
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false); // required for certain browsers
header("Content-Type: application/pdf");
header("Content-Disposition: attachment; filename=\"".basename($fullPath)."\";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".$fsize);
echo $content;
try this one :
$file = 'youfile.fileextention';
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;