msthac01
Tue 26 February 2008, 04:04 pm GMT +0100
Another part of the project I'm working on is taking the embedded pdf file out of an xml reply I'm receiving and give the user an option to open/save the pdf file. Well the save option works correctly, but the open option does open Adobe, but then gives the error that the file can't be found. What could be the issue...here's the code I have so far:
<?php
//Display the previously created PDF file
$path = "CreditReports/".$PDFName;
//echo $path;
$fr = fopen($path, "rb");
$filedata = fread($fr, filesize($path));
fclose($fr);
//octet-stream
header ( "Content-Length: " . filesize ( $path ) );
header("Content-type: application/octet-stream");
header("Content-disposition: attachment; filename=".basename($path));
readfile($path);
?>
Nikolas
Tue 26 February 2008, 10:12 pm GMT +0100
I suppose if you will work with absolute paths it could be much better (especially when you are running scripts in CLI mode)
For instance this :
$path = "CreditReports/".$PDFName;?>
should become this:
$path = dirname(__FILE__) . "/CreditReports/".$PDFName;?>
msthac01
Tue 26 February 2008, 10:52 pm GMT +0100
I still get the same behavior with adding the dirname(__FILE__) . to the path, the save option still works and the open option still says "file can't be found."
Nikolas
Tue 26 February 2008, 10:59 pm GMT +0100
That is not possible. Have you echoed the filename to see if it is correct?
msthac01
Tue 26 February 2008, 11:12 pm GMT +0100
here's the echo of $filename looks like its an issue with the slashes...I cut out the first part of the path but it is correct...
...\public\Enterprise/CreditReports/BarnDiane.pdf
Nikolas
Tue 26 February 2008, 11:31 pm GMT +0100
Slashes should not be an issue. I guess you are on a windows server, so case should not be an issue too.
Looks like a very strange problem....
msthac01
Tue 26 February 2008, 11:37 pm GMT +0100
yes this all resides on a w2k3 server running php 4.4.4, and yes its very strange...the really weird part is that at one point the mbstring.dll was enabled in the .ini file and while that was the case both options worked fine. but issues began to arise after the mbstring.dll was uncommented in the .ini file so we had to comment it back out, and since that change I've been unable to get the open option to work again. Makes no sense to me, but then again I've only been coding php for about 6 months.
Nikolas
Tue 26 February 2008, 11:41 pm GMT +0100
I am coding php for years and I am sure this code is ok. If you haven't done anything wrong (like giving a false filename or a path that does not exist) I guess it can be some kind of issue with w2k server (I use linux and apache)
msthac01
Tue 26 February 2008, 11:41 pm GMT +0100
I just received this response to my thread on another forum.....
I had this problem with a .csv file and solved it by adding caching headers. I think IE doesn't keep it in the temporary files, since it's generated by php, so when it tries to open from there, it fails.
I added:
header("Expires: ".date("D, j M Y H:i:s",time()+(300))." UTC");
header("Cache-Control: Public");
header("Pragma: Public");
Adding those headers in worked, now Open and Save options both work. Hope this helps anyone else that comes across the same issue...Thanks for everyones help trying to figure this out.
Nikolas
Tue 26 February 2008, 11:43 pm GMT +0100
Headers have nothing to do with the file system though :)
Anyway, good to know that it finally worked :)