Jump to content


Photo

Making IE windows download/open text files. SOLVED THANKS!


  • Please log in to reply
8 replies to this topic

#1 jefkin

jefkin
  • Members
  • PipPipPip
  • Advanced Member
  • 55 posts

Posted 08 September 2006 - 08:57 PM

Hi all,

I have a problem that I can't find a resource for.

Basically, my client needs the ability to have links on their site.

These links refer to text files on their site.  They want their users to click these links, and the links 'open' in the users favorite text editor.

I've done similar linking for excel (.xls) and word (.doc) files, so I thought it would be a breeze. 

Turns out that I've got some code that will work on every browser I've tested it on, except, of course, IE.

There are some security restrictions, this linking is only to work on '.txt' files and only on files under web root.

So, here's what I have

in html or php links of the form:

<a href="localopen.php?file=/foo.txt">foo.txt</a >

localopen.php is a php file like:

<?PHP

$file = $_GET['file'];

if (!preg_match('/\.txt/', $file))
{
  die("not a valid '.txt' file request $file");
}
elseif (preg_match('/\.\./', $file))
{
  die("invalid path characters '..' in file request $file");
}
else
{
  if ('/' == substring($file, 0, 1))
  {
     $file = substring($file, 1);
  }
  $url  = "/var/www/html/$file";

  header('Content-Description: File Transfer');
  header('Content-Type: application/octet-stream');
  header( 'Content-Disposition: attachment; filename="' .
          basename($url). '";');
  header('Content-Length: ' . (strlen($url + 3) + filesize($url)));

  @readfile($url) OR die();
}

?>

So this beast works great everywhere except the target platform.

On an IE 6 browser, after clicking the link, the browser seems to correctly launch the file dialog, and when the user chooses to open it, textpad (or whatever) opens up, but then IE doesn't seem to be passing a valid 'path' to the editor.  The editor fails to open up:

C:\.... \Temporary Internet Files\....  \Zx36Pls34 doesn't exist create it?

Any ideas?

Jeff

#2 jefkin

jefkin
  • Members
  • PipPipPip
  • Advanced Member
  • 55 posts

Posted 09 September 2006 - 12:25 AM

bump

#3 jefkin

jefkin
  • Members
  • PipPipPip
  • Advanced Member
  • 55 posts

Posted 09 September 2006 - 05:30 AM

bump again :(

#4 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 09 September 2006 - 06:41 AM

stop bumping not helping now!

look up header that the only clue your getting sorry.
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#5 jefkin

jefkin
  • Members
  • PipPipPip
  • Advanced Member
  • 55 posts

Posted 09 September 2006 - 05:06 PM

Yes Sir!  :)

Redarrow, Sir!  :o

I have already read everything I could find on headers, Sir!  ;D

As I mentioned, the script works EVERYWHERE except for IE.  I've scoured bulitin boards, and news groups, and mailing lists.  I chose this group because the tone was usually very helpful and quick.  And I'm not asking for a complete solution, though I'd be a fool to ignore it, I'm just looking for a resource.  I'm looking for:  "Oh, checkout this thread, or this site."

You gave me a hint that I already knew, my friend.  I didn't just cut and paste this code without understanding it.  I do understand it, read the RFC's top to bottom, and I don't know if I'm dealing with a IE bug, a windows bug, or if I've missed some other header to make it work there.  I have been searching for a solution to this for over 3 weeks.  The client is getting antsy, and so am I.

I've shown the code.  Is it realy so much to ask for a bit more than 'look up header'?

I hope I don't offend you redarrow, I'm just glad I got a repsonse.  Maybe this post should move to some other forum here.  Or maybe someone can suggest a group that can help me answer my question.

Jeff

#6 wenonae

wenonae
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 10 September 2006 - 02:09 AM

OK-  I'm as far from a *.php expert or PHP programmer as you can get- but what's ideas hurt.
I use osCommerce.  I had a situation where the files would not download on IE- was fine on Firefox and Opera.

The fix was to add this little additional code in my cache and another place to make IE recognize the file. 
Here's the thread on IE that gave me my clues.
http://forums.oscomm...=ST&f=1&t=31123

Be sure to read the whole thing.

Good luck!


#7 jefkin

jefkin
  • Members
  • PipPipPip
  • Advanced Member
  • 55 posts

Posted 10 September 2006 - 02:16 AM

Thanks wenonae,

Reading it now...

Jeff

#8 jefkin

jefkin
  • Members
  • PipPipPip
  • Advanced Member
  • 55 posts

Posted 10 September 2006 - 03:04 AM

wenonae,

I think this might do it.  However, since it's after dark on a Saturday night, and I don't have an ie machine hanging around, I'll have to test it tomorrow or worst case monday :)

But I'll send a preemptive 'Thank you'!!!

(If you're the same wenonae as on the other formum, cool, you earned yourself a double Thank-you!!)

Jeff

#9 jefkin

jefkin
  • Members
  • PipPipPip
  • Advanced Member
  • 55 posts

Posted 11 September 2006 - 01:26 AM

Hi Wenonae,

I tried the exact headers in your post, but that didn't cut it.  Instead I had to sort of merge somethings from yours and my original.

Since it wasn't just a cut and paste, I've included the final code:

if (!preg_match('/\.txt/', $file))
{
  die("not a valid '.txt' file request $file");
}
elseif (preg_match('/\.\./', $file))
{
  die("invalid path characters '..' in file request $file");
}
else
{
  if ('/' == substring($file, 0, 1))
  {
     $file = substring($file, 1);
  }
  $url  = "/var/www/html/$file";
  $name = basename($url);
  $size = filesize($url);

  header('Pragma: public');
  header('Cache-control: cache, must-revalidate');
  header("Accept-Ranges: bytes");
  header('Content-Type: application/octet-stream');
  header("Content-Transfer-Encoding: Binary");
  header('Content-Description: File Transfer');
  header("Content-Disposition: attachment; filename=\"$name\";");
  header("Content-Length: $size");

  @readfile($url) OR die();
}
?>

But thanks for your help.  With it, I got this to work.  And maybe this will work for other people who find themselves in a similar fix... :)

Jeff




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users