7, September 2008

Open/Save option on file - webmaster forum

 
Webdigity webmaster forums
This forum shares its ad revenue with its members!
[ Home | Help | Search | Forum's Shop | Archive | Login | Register | Webmaster Directory ]
Webdigity Webmaster Forums  >  Web Development  >  PhP
Topic: Open/Save option on file
« previous next »
Pages: [1] Print

Author Topic: Open/Save option on file  (Read 409 times)
Sandwich Artist
*
Posts: 25
186 credits
Members referred : 0


« on: Feb 26, 2008, 05:04:48 PM »

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);

?>
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 8037
41179 credits
Members referred : 3



« Reply #1 on: Feb 26, 2008, 11:12:29 PM »

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;

Trial and Error my two best teachers Cool
Join us @ facebook Visit through proxy

Last blog : MIA - Where Nick and Tim
Sandwich Artist
*
Posts: 25
186 credits
Members referred : 0


« Reply #2 on: Feb 26, 2008, 11:52:46 PM »

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."
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 8037
41179 credits
Members referred : 3



« Reply #3 on: Feb 26, 2008, 11:59:30 PM »

That is not possible. Have you echoed the filename to see if it is correct?

Trial and Error my two best teachers Cool
Join us @ facebook Visit through proxy

Last blog : MIA - Where Nick and Tim
Sandwich Artist
*
Posts: 25
186 credits
Members referred : 0


« Reply #4 on: Feb 27, 2008, 12:12:19 AM »

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
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 8037
41179 credits
Members referred : 3



« Reply #5 on: Feb 27, 2008, 12:31:09 AM »

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....

Trial and Error my two best teachers Cool
Join us @ facebook Visit through proxy

Last blog : MIA - Where Nick and Tim
Sandwich Artist
*
Posts: 25
186 credits
Members referred : 0


« Reply #6 on: Feb 27, 2008, 12:37:04 AM »

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.
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 8037
41179 credits
Members referred : 3



« Reply #7 on: Feb 27, 2008, 12:41:08 AM »

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)

Trial and Error my two best teachers Cool
Join us @ facebook Visit through proxy

Last blog : MIA - Where Nick and Tim
Sandwich Artist
*
Posts: 25
186 credits
Members referred : 0


« Reply #8 on: Feb 27, 2008, 12:41:37 AM »

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.
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 8037
41179 credits
Members referred : 3



« Reply #9 on: Feb 27, 2008, 12:43:38 AM »

Headers have nothing to do with the file system though Smiley

Anyway, good to know that it finally worked Smiley

Trial and Error my two best teachers Cool
Join us @ facebook Visit through proxy

Last blog : MIA - Where Nick and Tim
Trackback URI for this entry : http://www.webdigity.com/trackback.php?topic=7613
Tags : php open/save readfile() Bookmark this thread : Digg Del.icio.us Dzone more....

Topic sponsors:
Get a permanent link here for $1.99!


Pages: [1] Print 
Webdigity Webmaster Forums  >  Web Development  >  PhP
Topic: Open/Save option on file
« previous next »
Jump to:
User Area
Welcome, Guest. Please login or register.
Did you miss your activation email?
Sep 07, 2008, 05:34:47 PM





Login with username, password and session length

Donate to our community, and get a permanent link back to your site!

Donate to our community, and get a permanent link back to your site!


Forum Statistics
Total Posts: 36.301
Total Topics: 7.479
Total Members: 3.905
Tutorials : 56
Resources : 143
Designs : 220
Latest Member: indiecorporate

23 Guests, 5 Users online :

9 users online today:



Readers

Web Design Gallery · Whois Lookup · Pagerank · Tag Browsing · Lo-fi version · Syndication · Webmaster forum history · Advertise
Developed by HumanWorks © 2005 - 2008 Webdigity webmaster community · sublime directory
Webdigity Webmaster Forums | Powered by SMF 1.0.12. © 2001-2005, Lewis Media. All Rights Reserved.