i have this very simple download page to get an xml file.
the script works ok in firefox/IE. but chrome renames the extension of the file to ".download".
and this happens only to .xml, when you use another extension like .txt it does it without problems.
the body of the html is this:
<body>
descarga
</body>
and the php is this:
header('Content-type: "text/xml"; charset="utf8"');
header('Content-disposition: attachment; filename="example.xml"');
echo "that's it";
its very strange. any solution for this??
This is not a definite answer, just some information for you.
From the bug report:
The downloaded file may get a different name if it is considered potentially dangerous
for your computer (e.g. exe). You should then get an UI prompt in the download shelf
asking you to confirm the download (with the file still downloading in the background).
try removing 'echo "that's it";'
it makes the xml invalid and might confuse the browser.
if it doesn't help, check the actual http headers of both request and response.
Related
I'm trying to create, download then remove a gzipped file.
The file created on the server is correct. I can download it with a direct link and the file decompresses correctly locally.
If I use header(), the file won't decompress and gives an error "Not In Gzip Format". It seems like I have to use header(), to be able to unlink().
The gist of the code is:
# This creates the gz file correctly..
$tar = 'meta-info.tar';
$gz = $tar.'.gz';
$p = new PharData($tar);
$p->compress(Phar::GZ)->buildFromDirectory('../');
# This creates the dialog then removes the file correctly..
header('Content-Type:'.mime_content_type($gz));
header('Content-Length:'.filesize($gz));
header("Content-Disposition:attachment;filename=$gz");
flush();
readfile($gz);
unlink($gz);
# ...but the downloaded file is not correct.
Oddly, a 'zip' file downloads and decompresses correctly.
I've looked all over stackoverflow and tried a number of things, but nothings working.. Any ideas?
Thanks in advance!
The issue was that header info had already been sent.
Using ob_end_clean() fixed it (the structure of the page can't be changed).
ob_end_clean();
header("Content-Disposition:attachment;filename=$gz");
header('Content-Type:application/x-gzip');
header('Content-Length:'.filesize($gz));
flush();
readfile($gz);
unlink($gz);
Hi I am trying to proxy a woff font file using PHP
this is the code I am using $path is the path to file on the harddisk. But I get an error in the console
Failed to decode downloaded font: http://localhost/font/fontawesome-webfont.woff?v=3.2.1
fakemboard.com/:1 OTS parsing error: invalid version tag
If I use PHPStorm default http server it works fine.
I have attached two images:
1) The first is the problematic response with my PHP proxy
2) the second one is the OK one using PHPStorm default server
Can you help me find out what is missing with my proxy? I believe it may be headers buy I am weak at that. So it will be really helpful if you provide the missing code. Thanks
header('content-type: application/font-woff');
$file = fopen($path, 'rb');
if ($file) {
fpassthru($file);
exit;
}
I flowed Mike 'Pomax' Kamermans advice and compered the good file font file with the bad font file. Indeed they were different. The bad file has two extra blank lines in the beginning.
It happened because that in one of the PHP files that I have included I accidentally added blank lines before the <?php tag and php rendered them. Hope this helps someone else
I did an xml file and force download it by these headers:
header('Content-disposition: attachment; filename="export.xml"');
header('Content-type: application/xml; charset=utf8');
readfile('export.xml');
But before the download I see a dialog that this file can be harmful for my computer? How to get rid of this dialog? Maybe my headers is wrong?
upd Well, can do nothing, I did a test on my test-hosting, u can check it here: site with generation link, and an xml file as is: export.xml
Try changing application/xml to text/xml. Probably your browser thinks that application means executable.
Try this :
<?php
header('Content-disposition: attachment; filename="export.xml"');
header('Content-type: "text/xml"; charset="utf8"');
readfile('export.xml');
?>
Note: This does not solve your issue, however it did solve an issue I had on my computer giving that notice (windows, chrome, apache webserver, PHP 5.4.10). I leave it here for future visitors.
Some browsers do not only look for the headers but also for the "filename" in the URL.
For example if you download a PHP file that contains XML, the browser might identify it as a dangerous file (because it can be executed on your system or is not within some whitelist or what not):
http://example.com/xml-download.php
A simple solution is to make this file not end with .php any longer, for example by adding a ?:
http://example.com/xml-download.php?
And continue with that to even signal the filename that way:
http://example.com/xml-download.php?export.xml
(the last one is not necessary but can be useful especially with some older browsers)
I redirect the visitors in my website from page A to page B. In page B I expect users to get the downloaded PDF file (to be downloaded when page B is loading).
I have taken the code from another article (see a previous question answered here) and my code of page B is the following:
<?php
header('Content-Disposition: attachment; filename=nature.pdf');
header('Content-type: application/pdf');
$fn=fopen("/wp-content/nature.pdf","r");
fpassthru($fn);
?>
The output is not by opening a download dialog box, instead some unreadable characters are displayed in browser such as the following (I have just picked up a small sample below):
%PDF-1.4 %���� 3 0 obj <>stream x���MK1�o�+�$zIg&�� V=T�=Xo����K��i+#V�yx3��(BX�pW`
Server: OS Linux; PHP version: 5.2.17
The visitor -> Browser: Firefox; OS: Windows 2000
Is it possible to fail due to the old OS on client side? If not, does anybody know a solution how to force the download? Any help would be highly appreciated.
Thanks.
Try it with the Content-Length header:
ob_clean(); ob_start();
header('Content-Disposition: attachment; filename=nature.pdf');
header('Content-type: application/pdf');
header ("Content-Length: ".filesize("/wp-content/nature.pdf"));
readfile("/wp-content/nature.pdf");
exit;
There was a quirk in the really old browsers when Content-disposition was first being introduced, some of the really old browsers wouldn't show the "Save As" dialogue unless it couldn't recognize the type of file you were trying to open. Try setting the Content-type to nothing (or something unrecognizable), and see if that'll force the older browser to pop the save-as dialogue.
header('Content-type: ');
If that works, then I'd suggest adding in a line of PHP to detect whether or not they're on an old browser before running that line, as modern browsers will use that header to determine what program the file should be opened with.
i want to open a notepad from php file and the notepad should contain the text which i declare as string in php file. essentially a notepad should open with the text I pass from php file.
If the PHP file is executing on a web server you cannot cause a web browser to open a new process like that. I'm sure you can imagine what a security hole that would be!
If you're running a PHP file as a local script in CLI mode, you should be able to launch notepad like any other process, e.g. using backticks or exec etc.
However, if you really wanted to do this server side, the best you could do is have a PHP script which used a Content-Disposition header, e.g.
//tell client we're delivering text
header('Content-type: text/plain');
//hint that it's a downloadable file
header('Content-Disposition: attachment; filename="textfile.txt"');
//output our text
echo "The quick brown\nfox jumps over\nthe lazy dog.";
The user can then save this file and open in their editor of choice.
You can't get PHP to open a window on the user's machine, because PHP is run entirely on the server. By the time the output reaches the browser the script will generally have terminated - you can only do what you can ask the browser to do and what it will let you (using HTML / headers etc.). For security purposes the browser will not (or should not) let an arbitrary website do very much with your machine - e.g. it will not let you spawn new Windows processes.
The best I think you could do is something like this:
$string = 'a string';
header('Content-type: text/plain');
header('Content-Disposition: attachment; filename="file.txt"');
echo $string;
This will send the relevant headers so that the browser will treat the content as a download called file.txt of type plain text. The browser should prompt them to download a file which will be likely to open in notepad, unless they have changed the file association for .txt .
However you won't be able to get any changes back that the user makes to the document unless you ask them to upload it, so I'm not sure this is a good solution to what you are trying to acheive.
It is possible to execute a program from php but only server-side.
So imagine the server runs Windows, it would start notepad server-side.
PHP gets executed on the server, and has nothing todo what's running client-side.
Technically, to do this you'd have to create a file and then execute a system with that file as a parameter. Something like this:
//String to show in notepad
$myStringToDisplay = "some text to show in notepad";
//Write this string to a file
$myFile = "somefile.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, $myStringToDisplay);
fclose($fh);
//Execute notepad with this file as a parameter
system("notepad.exe ".$myFile);
However, this is going to execute Notepad on the server running the PHP file (if system calls are even enabled on your server), which is probably not what you want to do. PHP cannot execute any code on the client machine, and certainly cannot make a system call to execute any program it wants on the client (thank god). It would be a huge, huge security breach.
If this does not accomplish the desired functionality, please tell us what you're trying to do and why. This does not sound like a very sensible request.
It is not possible to open a program from your php application. But you ca load the text file using a PHP text editor. You will also be able to load the values which you are talking about.
http://www.fckeditor.net/ is one such editor.
First you can "create" the file and fill it with text.
Execute shell command: echo $text >> $filename
then execute: notepad $filenameToOpen
Thats it.
To open a notepad from php script we will use command line inter phase.
Firstly we will create one php file in that we will write:
var_dump(popen('notepad','r'));
Then we will save that with some name like notepad.php
then open command prompat there we will give the path of our file to run our file
like:
d:/>wamp>www>php notepad.php
It will run our php file and it will oopen notpad.