Including source file name on readfile? - php

I have a very simple hide-download-path-script setup like this:
On index.html I have this link:
save
On savefile.php I have this bit of code:
$file = 'http://www.mysite.com/files/correct_horse_battery_staple.rar';
header("Content-Type: application/force-download");
#readfile($file);
This does seems to work, but unfortunately it downloads the file as savefile.php rather than correct_horse_battery_staple.rar.
Is there any way to change not only the file name but also the extension?

I have had same problem
Solved as below:
header("Content-Type: application/force-download");
header('Content-Disposition: attachment; filename="'.$filename.'"');
readfile($filename);
I hope it help u

Your link goes to savefile.php, and the browser never got another filename than savefile.php.
You need to add a header like:
header('Content-Disposition: attachment; filename="correct_horse_battery_staple.rar"');
or better...
header('Content-Disposition: attachment; filename="'.basename($file).'"');
Hope it helps!

Related

Force file download with header is corrupting file

So I am using PHP headers to force download a file, but when the file is saved it gets corrupted because for whatever reason the HTML code from the download page is also added into the file code/contents:
Beginning of downloaded file:
End of downloaded file:
and here is my code that forces the download, I also downloaded the same file straight from my server (FTP) and it does not have this in its file code, so I know it has to be the following code causing the problem:
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename={$qfile['rfname']}");
header("Content-Type: application/zip");
header("Content-Transfer-Encoding: binary");
readfile($file['dl_path']);
I've looked around but I don't see anything pertaining to this, would anybody have an idea as to why this could be happening?
Add content type to application/octet-stream as below:
header("Content-Type: application/octet-stream");
Like Barmar said, I needed to exit();/die(); after the function readfile();.
So the fix would look like:
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename={$qfile['rfname']}");
header("Content-Type: application/zip");
header("Content-Transfer-Encoding: binary");
readfile($file['dl_path']);
exit(); // Or you can use die(); here

Download Excel File from Server has been corrupted - PHP

I have a problem about downloading Excel file from server.
the excel file was already saved on the server and I downloaded it using the code below.
if(file_exists($reportPath)){
//content type
header('Content-type: application/vnd.ms-excel');
//open/save dialog box
header('Content-Disposition: attachment; filename='.$dirFile[count($dirFile)-1]);
//read from server and write to buffer
readfile($reportPath);
}
But the downloaded file was corrupted.
I'm pretty sure that the file saved on the server is not corrupted since I have get it manually from the server to my local desktop.
Meaning, the data has been corrupted on the fly.
Please help, thank you, I'm using PHP
Can you try these headers?
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="'.$dirFile[count($dirFile)-1].'"');
header('Cache-Control: max-age=0');
And see if it's working... Cheers!
Download script should be separate file. Actually you should not print out anything in this script
//Add below to download the text file created
$filename = $file; //name of the file
$filepath = $file; //location of the file. I have put $file since your file is create on the same folder where this script is
header("Cache-control: private");
header("Content-type: application/force-download");
header("Content-transfer-encoding: binary\n");
header("Content-disposition: attachment; filename=\"$filename\"");
header("Content-Length: ".filesize($filepath));
readfile($filepath);
exit;
$fileName = "data.xls";
$object_writer = PHPExcel_IOFactory::createWriter($object, 'Excel5');
ob_end_clean();
header("Content-Type: application/download");
header('Content-Disposition: attachment;filename=' . $fileName);
$object_writer->save('php://output');
use ob_end_clean() to clear the output buffer.
This is what fixed my issue :
adding ob_end_clean funct after the save.
adding exit at the end of the script.

readfile function for PDF download issues

I know this question has been asked, but the solutions offered are not working for me. I am able to download PDFs, but Adobe Reader is telling me it's either corrupt or decoded improperly. Does anyone have an alternative solution, or possible fix?
$path = "http://www.laerdalmail.com/discoversimulation/downloads/needs_assessment_form.pdf";
header('Content-Type: application/pdf');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=needs_assessment_form.pdf");
readfile($path);
exit;
readfile should be given a filesystem path, not a URL. something like this:
$path = '/websitefolder/discoversimulation/downloads/needs_assessment_form.pdff';
// or
$parh = 'c:\something\needs_assessment_form.pdff';
Try adding in content length too:
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($path));

Download automatically a file(.sql) in wwwroot using PHP

So far here what i've tried it can download the sql file but it is empty
//test.php
<?php
header("Content-type: application/octet-stream");
header('Content-Disposition: attachment; filename=wordpress_db1.sql');
?>
Here is what my root folder look like
I want to download the wordpress_db1.sql file when I run the test.php but I always get empty on it. How can I fix this? thanks!
Below code will do the trick for you.
<?php
$file_name = 'file.sql';
$file_url = 'http://www.example.com/' . $file_name;
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"".$file_name."\"");
readfile($file_url);
?>
What have you gone wrong is readfile($file_url);. Setting headers will not get the job done. you have use readfile($file_url);
Setting the headers doesn't read the file. You can name the file anything you want in the attachment. You have to actually emit the file:
readfile('wordpress_db1.sql');

Save image to user machine

I am trying to do something like Facebook's "Download" Photo link when viewing an album photo. Trying to avoid opening popups.
Any Javascript/jQuery/PHP method to do it?
I am aware of this: http://www.jtricks.com/bits/content_disposition.html
But I don't have control over the server configuration to do so.
Please let me know what is the best way to achieve this.
You can simply use the header function of PHP to set the Content-Disposition header, for example:
header("Content-Type: image/jpeg");
header("Content-Disposition: attachment; filename=\"".$name."\"");
header("Content-Transfer-Encoding: binary");
header("Accept-Ranges: none");
header("Content-Length: ".$length);
echo $data;
You don't need to access the server configuration. Just use header()—before any HTML output.
Example:
header('Content-Disposition: attachment; filename=example.pdf');
You can send a Content-Disposition header using PHP. No need to change the server configuration. So maybe something like this:
<?php
$file = 'folder/file.jpg';
header('Content-Disposition: attachment; filename='.basename($file));
readfile($file);
?>

Categories