Hello need to download generated csv file and i'm used below code but on server i'm not able to download csv file. The same code working fine in my local sytem but not working on live server.
<?php
function downloadFile($filename,$filepath)
{
header("Content-type: text/csv"); // instead of pdf use others... for text
header("Content-disposition: attachment; filename=".$filename);
readfile($filepath);
}
?>
check your server, may be there is space limitation.
Related
I have xampp web server, and trying to download file using headers! Don't know what is wrong, but file not starting to download and not appears in browser! In http response I have the source of file!
header("Content-disposition: attachment;filename=d:\\archive\\result.csv");
header("Content-type: application/pdf");
readfile("sample.pdf");
Can any one halp me, please!
Try this
header('Content-Type: application/csv');
header('Content-Disposition: attachment; filename=file:///D:\archive\result.csv' );
I am using PHPExcel library for downloading excel sheet in CodeIgniter framework. Its working fine in local server, its downloading excel sheet file, opening and showing correct data, now when I run that same code in live server it is downloading, when I open that file, it is giving this error, The file you are trying to open 'filename.xsl' is in a different format than specified by the file extension. verify that the file is not corrupted and is from a trusted source before opening the file.
Here is the code.
$this->excel->createSheet();
$this->excel->setActiveSheetIndex(2);
$filename='Monthly Report'.date("m-d-Y, h:i:s").'.xls';
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename="'.$filename.'"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel5');
$objWriter->save('php://output');
I also used the method ob_end_clean(); before $objWriter, still got the same error. Any help will be appreciated.[it's in a live server on the local server it's working fine]
ob_end_clean();
ob_start();
Used the above lines of code before $objWriter->save('php://output');
and it worked.
Now I am developing codeigniter site.
I have one issue.
In order to read&open pdf into web browser.
header("Content-type:application/pdf");
header('Content-Type: application/vnd.ms-excel'); //mime type
header('Cache-Control: max-age=0'); //no cache
header("Content-Disposition:attachment;filename=\"".$file."\"");
readfile($filePath);
above code $file is filename and $filePath is pdf file path.
When it runs on local server, $filePath is value such as "http://localhost/.pdf" and it runs well.
But when runs on hosting server, this value is "http://.com/***.pdf"
And doesn't run.
We can not open with pdf format error.
file content didn't include readed.
I know that is cause of URL issue.
But I have no issue!
Your Content-Disposition should be inline if you want to display the file in the browser and not as a download but I guess it doesn't really matter if you can get it to work.
If your allow_url_fopen config in PHP is set to Off, you will not be able to read URL file from within your PHP script.
Anyway, your code should look something like this.
<?php
$file = "lesson2.pdf";
$filePath = "http://kmmc.in/wp-content/uploads/2014/01/lesson2.pdf";
header("Content-Type: application/pdf");
header("Cache-Control: max-age=0");
header("Content-Disposition: inline; filename=\"" . $file . "\"");
readfile($filePath);
I know there are a lot of mentions of this but I have tried all the suggestions and nothing seems to work.
I have this script to force download files, but when using docx formats it downloads ok but then says the file is corrupt. However word does manage to open it ok.
Does anyone know why the docx would keep saying there corrupt. I have double checked them by ftp them down from the server and they are fine and open first time.
$documentDir = '/home/';
$file = $_GET['d'];
$fileLocation = $documentDir.$file;
header('Content-type: application/octet-stream');
header('Content-Length: ' . filesize($fileLocation));
header('Content-disposition: attachment; filename="'.$file.'"');
readfile($fileLocation);
exit(0);
This script works for me.
Are you sure that all your documents are situated in /home/ ?
Don't you mean the relative path home/
If this still doesn't work, can you please tell what's the size of the downloaded docx, and open that file in a text editor like sublime_text, the error will probably be written in there.
You could try modifying disposition line and adding encoding line to ensure encoding is done correctly.
header ("Content-Disposition: attachment; filename*=UTF-8'en'$file");
header('Content-Transfer-Encoding: binary');
I have a PHP script that is currently working locally that I'd like to put on a server.
Currently, the user choose a .txt file, the PHP script works on it and outputs a new file based on what it read in the file.
The problem is that I can only select files in the folder with the script, and not elsewhere.
I use a to get the file name, but it only gives out the name of the file, and not it's absolute path.
From what I've read, I think that I need to upload the file to the server, process it with the script and then give it back to the user.
I'm not sure this is the correct method though.
Also, while I have found plenty of informations on uploading files to the server, I don't know how to put the new file created by the script in the folder where the original file is located.
You cannot read or write files directly on the client's machine. The client will need to upload the file by selecting it in the browser, the server receives the data, processes the data and returns data. This returned data can be presented in the form of a file download by setting the appropriate HTTP headers. The client will have to acknowledge the file download and save it somewhere of his choosing.
Your server has no business knowing anything about files or folders on the client's machine. It can only communicate with it over the HTTP protocol and send and receive data.
You will have to give the file back to the client, as a downloadable file. You can "write" it to the user by setting some headers. Take a look:
<?php
$file = 'random_text_file.txt';
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');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
}
?>
That will prompt a download of the file to the user.