Avatar of walker6o9
walker6o9

asked on 

Save File To Hard Drive

I have a php script that currently saves a file to the user's hard drive.  I'm wondering if it is possible to create a pop-up Save As box that allow the user to choose where the file is saved to and what it is called using either Javascript or php?
JavaScriptPHPWeb Languages and Standards

Avatar of undefined
Last Comment
walker6o9
Avatar of ahalya
ahalya
Flag of Canada image

What's wrong with the web client's Saver As.. dialog ?

I use the following function to force a save, or give the option to the user to view or save a pdf file. (This can open up the client's Save As Dialog)
 
function setHeadersPDF($contentlength, $forcedSave) {
	
  if ($forcedSave) {
    if(isset($_SERVER['HTTP_USER_AGENT']) && strpos($_SERVER['HTTP_USER_AGENT'],'MSIE'))
      header('Content-Type: application/force-download');
    else
      header('Content-Type: application/octet-stream');
    
    header('Content-Length: '.$contentlength);
    header('Content-disposition: attachment; filename="tmp.pdf"');
  }
  else {
    header('Content-Type: application/pdf');
    header('Content-Length: '. $contentlength);
    header('Content-disposition: inline; filename="tmp.pdf"');
  }
}

Open in new window

Avatar of walker6o9
walker6o9

ASKER

Thanks -
What does $contentlength represent?
ASKER CERTIFIED SOLUTION
Avatar of ahalya
ahalya
Flag of Canada image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of walker6o9
walker6o9

ASKER

Is your code javascript or php?  Also, will the first set work if $contentlength is not specificed, and will the second set work by itself?
Avatar of walker6o9
walker6o9

ASKER

Some problems when I tried out your code.  I tried it both with $forcedSave as true and false.  On a localhost, nothing happens.  On a remote server I get the following errors:


Warning: Cannot modify header information - headers already sent by (output started at /nfs/c01/h07/mnt/37639/domains/v01.andrew.sparrow-stage.com/html/SAVE/save.php:4) in /nfs/c01/h07/mnt/37639/domains/v01.andrew.sparrow-stage.com/html/SAVE/save.php on line 8

Warning: Cannot modify header information - headers already sent by (output started at /nfs/c01/h07/mnt/37639/domains/v01.andrew.sparrow-stage.com/html/SAVE/save.php:4) in /nfs/c01/h07/mnt/37639/domains/v01.andrew.sparrow-stage.com/html/SAVE/save.php on line 13

<?php  
      $forcedSave = true;
  if ($forcedSave) {
    if(isset($_SERVER['HTTP_USER_AGENT']) && strpos($_SERVER['HTTP_USER_AGENT'],'MSIE'))
      header('Content-Type: application/force-download');
    else
      header('Content-Type: application/octet-stream');
    
   
    header('Content-disposition: attachment; filename="tmp.pdf"');
  }
  else {
    header('Content-Type: application/pdf');
 
    header('Content-disposition: inline; filename="tmp.pdf"');
  }
?>

Open in new window

JavaScript
JavaScript

JavaScript is a dynamic, object-based language commonly used for client-side scripting in web browsers. Recently, server side JavaScript frameworks have also emerged. JavaScript runs on nearly every operating system and in almost every mainstream web browser.

127K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo