Jump to content

Archived

This topic is now archived and is closed to further replies.

jefkin

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

Recommended Posts

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:

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

localopen.php is a php file like:

[code]
<?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();
}

?>
[/code]

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

Share this post


Link to post
Share on other sites
stop bumping not helping now!

look up header that the only clue your getting sorry.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.oscommerce.com/index.php?act=ST&f=1&t=31123

Be sure to read the whole thing.

Good luck!

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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:

[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();
}
?>
[/code]

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

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.