I am creating a CSV file in PHP, no biggie there, and then force downloading it for the user, again no biggie there. The problem I am coming across is the redirect portion. I know that you cannot create a file and push to the user and then use php redirect afterwards, as it kills the CSV creation due to the HTTP protocols. So instead, I am doing a javascript redirect as follows:
echo '<script>';
echo ' window.location = "'.base_url().'link/'.$item_id.'"';
echo '</script>';
die;
This works fine (it reloads the page), but the portion above that is echoed gets added on to the CSV file when it is downloaded. Here is the php code that creates the CSV and force downloads it:
fwrite($csv, $csv_data);
fclose($csv);
header('Content-type: application/vnd.ms-excel');
header('Content-Length: ' . strlen($csv_data));
header("Content-disposition: attachment; filename=$file_name");
readfile($file_path);
What am I missing in terms of not having the <script> text getting appended to the CSV?
Thanks in advance.
Related
Good evening,
I would like to create a feature for my users that allows them to read a pdf file without downloading or printing it.
They will only be able to read the pdf file.
I tried pdf.js which did not work,
I also tried to convert my pdf to html via a script,
I also tried this php code but the download and print button appears:
<?php
// The location of the PDF file
// on the server
$filename = "/path/to/the/file.pdf";
// Header content type
header("Content-type: application/pdf");
header("Content-Length: " . filesize($filename));
// Send the file to the browser.
readfile($filename);
?>
Do you have any solutions or technologies to suggest to me,
I use PHP, HTML and JS.
Try this one:
<?php
// Store the file name into variable
$file = '/path/to/the/filename.pdf';
$filename = 'filename.pdf';
// Header content type
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="' . $filename . '"');
header('Content-Transfer-Encoding: binary');
header('Accept-Ranges: bytes');
// Read the file
#readfile($file);
?>
PHP uses a standard code to display the pdf file in web browser. The process of displaying pdf involves location of the PDF file on the server and it uses various types of headers to define content composition in form of type, Disposition, Transfer-Encoding etc. PHP passes the PDF files to read it on the browser. Browser either shows it or download it from localhost server then display pdf.
Note: PHP is not actually reading the PDF file. It does not recognize File as pdf. It only passes the PDF file to the browser to be read there. If copy the pdf file inside htdocs folder of XAMPP then it does not need to specify the file path.
The php script you offered only can download the file. if you use php ,you can try the demo ,which is using pdf2text here
or try the method in this link read-pdf-files-with-php
How can I download file (I just know the URL - the file can have x MB) and then save via dialog to user computer? (Dialog = user can choose the directory, where he want to save the file). Just like in screen.
I have absolutely no idea how to do it. I Google it and I found only how to load the file via FILE input, but I do not need that.
File suffix is "*.db", thanks guys, I'm not such a web guy.
You can do that pretty easily.
Make a php file, let's call it index.php. And this should be all the code you need:
<?php
$file = "/your/location/file.db";
header("Content-Type: application/octet-stream");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename='" . basename($file) . "'");
readfile ($file);
?>
I hope I've understood what you actually need. This should do the trick.
I have website, and I already can highlight code that is written in HTML.
I can store documents on my website, but when I store document (like .php , .c, , .cpp) I can only download them, not opening in next window.
I need some idea how to pass raw code of that file to another page.
I have apache server and I use PHP
I'm not 100% sure what you're trying, but how about posting the file name to a PHP script and reading the file contents using fopen/fread and then just echo it? You should of course make sure that it'll only output files in a specific folder, so this can't be used to hack your site!
<?php
if(isset($_POST['filename']) && is_allowed_to_be_shown($_POST['filename']))
{
$filename = $_POST['filename'];
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);
echo $contents;
}
?>
Try accessing the pages ending in .php, .c, .cpp, etc via typing in "localhost/[path to file]" in the address bar. That way the files are read by the sever, before being outputted to the browser, meaning that .php (and the like) files will be handled correctly rather than just HTML files.
I am using this code to make a php file become a javascript file
<?php
session_start();
header("Content-Type: text/javascript");
?>
Within the file, I need to get the document's url in both javascript and php.
Javascript returns the URL of the page, but PHP returns the URL of the script.
How can I force PHP to return the URL of the page?
My PHP echo $_SERVER["PHP_SELF"];
My JS var url = document.URL
I have a form button, when clicked it submits the form.
I'd like that at the same time, the browser starts downloading a PDF file. I wonder how can I do this? Please keep in mind that PDF are usually opened by the browser by default, but I'd like the browser to "Save As" the file and not open the file. Is this do-able using html only or do I need javascript?
If you are using PHP in the server side try this. This is tested code which is running in one of my websites.
<?php
ob_start();
$file = 'YOUR-FILENAME-HERE.pdf';
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();
}
?>
Browser will prompt to download without displaying it in the browser.
You can use this plugin to download the files using javascript.
Otherwise in javascript, you can write code for it.
$('a').click(function(e) {
e.preventDefault(); //stop the browser from following
window.location.href = 'downloads/file.pdf';
});
Download now!
Or, you can use this .
In PHP :
<?php
header('Content-type: application/pdf');
header('Content-disposition: attachment; filename=filename.pdf');
readfile("file.pdf");
?>
In Javascript :
<body>
<script>
function downloadme(x){
winObj = window.open(x,'','left=10000,screenX=10000');
winObj.document.execCommand('SaveAs','null','download.pdf');
winObj.close();
}
</script>
<a href=javascript:downloadme('file.pdf');>Download this pdf</a>
</body>
In that case, you'd have to configure your webserver to force a PDF to Download.
If you're using Apache, you might as well take a look at this article ->
http://www.thingy-ma-jig.co.uk/comment/7264
Or, if you don't have to support all browsers, you could get away with just using the html5 download attribute -> http://updates.html5rocks.com/2011/08/Downloading-resources-in-HTML5-a-download
Option 1: You can let the url, which your form submits to, return the download.
Option 2: Use an ajax call to submit the data onclick, set the window.location to your download url.
Whether the pdf is opened in the browser or downloaded, is a client side setting which you cannot control.