I'm trying to download file in php. I used both codeigniter download helper and pure php code readfile. I can get the contents of file in browser's Network but I can not download it.
When I tried the readfile function in an independent php file it worked.
Here is my code block for dowload file.
// codeigniter
$this->load->helper('download');
$data = file_get_contents(base_url("/images/logo.png"));
force_download($decodedFileInfo->fileName, $data);
// php readfile
$file = "./images/logo.png"; //. $decodedFileInfo->fileName;
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
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;
}else{
var_dump("hst");die;
}
Thank you..
Try Saving the file name and image name in variables and echo them on the screen.
I tested this functions works perfectly:
$this->load->dbutil();
//Calling a store procedure
$sp ="exec secondProc '5 Aug 2013'";
$query = $sqlsrvr->query($sp);//->result();
$jawad = $query->result_array();
//pass it to db utility function
$new_report = $this->dbutil->csv_from_result($query);
//Downloading the file
$this->load->helper('download');
force_download('csv_file.csv', $new_report);
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header('Content-Description: File Transfer');
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename={$name}");
header("Expires: 0");
header("Pragma: public");
Related
My code create Word template and instantly download when i run php file.
How to create a button that allow me to create and download word file when i click on it?
Thank you!
<?php
require_once 'vendor/autoload.php';
$phpword = new \PhpOffice\PhpWord\PhpWord();
$ran = time();
$section = $phpword->addSection();
$section->getStyle()
->setPaperSize('Letter')
->setLandscape()
;
$section->addText("Hello World!");
$file = 'test.docx';
header("Content-Description: File Transfer");
header('Content-Disposition: attachment; filename="' . $file . '"');
header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Expires: 0');
$phpword->save("php://output");
?>
You can make a curl request on a php file who create, save the word and return the path, then use js to make a auto-click on a hidden link who you have set the "href=" with the path and have the attribute "download=your_file_name".
Create a tmp folder the same directory youhave the code and add this
objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save('tmp/helloWorld.docx');
$file = 'tmp/helloWorld.docx';
if (file_exists($file)) {`enter code here`
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
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;
}
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 having a problem that I just can't solve and I'm going madness with this!
Basically I have to export a csv file. The site is obviously running on a server, and the idea is that onclick the file is generated and automatically downloaded.
Everything is just fine, however, the file is being download to the server directory instead of the local user machine. I just can't make it.
What is wrong? Thank you very much!
if(!isset($crm_contas))
$crm_contas = new crm_contas;
$resultGetValues = $crm_contas->getExportContas($filtros, $idsToSend);
// filename for download
$filename = "ContasExportadas" . date('Ymd') . ".csv";
$output = fopen($filename, 'w');
foreach($resultGetValues as $row) {
fputcsv($output, $row);
}
fclose($output);
$filename = realpath($filename);
$fp = #fopen($filename, 'rb');
if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE"))
{
header('Content-Type: "application/octet-stream"');
header('Content-Disposition: attachment; filename='.$filename);
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header("Content-Transfer-Encoding: binary");
header('Pragma: public');
header("Content-Length: ".filesize($filename));
} else {
header('Content-Type: "application/octet-stream"');
header('Content-Disposition: attachment; filename='.$filename);
header("Content-Transfer-Encoding: binary");
header('Expires: 0');
header('Pragma: no-cache');
header("Content-Length: ".filesize($filename));
}
fpassthru($fp);
fclose($fp);`
--> This is the action that calls the code above.
echo "<a href='#' onclick='changeFormAction(\"form_list\",\"".$CONF['HOME']."/contas/index.php?view=export\");document.form_list.submit();changeFormAction(\"form_list\",pesquisa);'><img src='".$CONF['HOME']."/images/structure/excel.png' alt='Exportar' title='Exportar'></a>";
Meanwhile, I noticed that error_log is writting down this lines when I try do download.
[21-Apr-2016 16:23:44 UTC] PHP Warning: Module 'imap' already loaded in Unknown on line 0
I have some php code which is suppose to download a zip file...this code is called from a file called download_file.php (very original, i know)
The relevant function is as follows :
function download_clientfile() {
if ($this->download_file === FALSE) {
$this->log->log(sprintf("The download file was not generated."), PEAR_LOG_CRIT);
return;
}
$fsize = filesize($this->download_file);
$path_parts = pathinfo($this->download_file);
$ext = strtolower($path_parts["extension"]);
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; file='.$path_parts['basename']);
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($this->download_file));
ob_clean();
flush();
readfile($this->download_file);
exit;
}
Problem : The file downloaded is the zip file that is expected, however its downloaded with the name 'download_file.php'
Any suggestions on how I can get it to download by the name of the zip file.
Thanks
Use filename instead of file:
header('Content-Disposition: attachment; filename='.$path_parts['basename']);
Also, you may encounter browsers which use the extension anyway. In that case, you can call the script like this:
download_file.php?file=myfile.zip
or:
download_file.php/myfile.zip
This way, the URL ends in .zip, which tricks the browser into thinking it is downloading a ZIP file.
Try changing file= to filename=
header('Content-Disposition: attachment; filename='.$path_parts['basename']);
use
header("Content-Disposition: attachment; filename=\"$filename\"");
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;