pmtamal Posted June 3, 2008 Share Posted June 3, 2008 I have a link on my page like this \\myfolder\data\a.xls.So when somebody clicks the browser opens it.But i want excel to open it independently not embed in the browser.Is it possible.Can anyone hava any idea Link to comment https://forums.phpfreaks.com/topic/108519-opening-link-not-in-browser/ Share on other sites More sharing options...
jonsjava Posted June 3, 2008 Share Posted June 3, 2008 you'll need to force download. This script should do it (I didn't write it, by the way): <?php $mime = "application/vnd.ms-excel"; $file = "\\myfolder\data\a.xls"; $name = "a.xls"; $fh = fopen($file, "r"); $data = fread($fh, filesize($file)); fclose($fh); function force_download ($data, $name, $mimetype='', $filesize=false) { // File size not set? if ($filesize == false OR !is_numeric($filesize)) { $filesize = strlen($data); } // Mimetype not set? if (empty($mimetype)) { $mimetype = 'application/octet-stream'; } // Make sure there's not anything else left ob_clean_all(); // Start sending headers header("Pragma: public"); // required header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Cache-Control: private",false); // required for certain browsers header("Content-Transfer-Encoding: binary"); header("Content-Type: " . $mimetype); header("Content-Length: " . $filesize); header("Content-Disposition: attachment; filename=\"" . $name . "\";" ); // Send data echo $data; die(); } function ob_clean_all () { $ob_active = ob_get_length () !== false; while($ob_active) { ob_end_clean(); $ob_active = ob_get_length () !== false; } return true; } force_download ($data, $name, $mime, $filesize=false); ?> The origional script can be found here. Link to comment https://forums.phpfreaks.com/topic/108519-opening-link-not-in-browser/#findComment-556498 Share on other sites More sharing options...
pmtamal Posted June 4, 2008 Author Share Posted June 4, 2008 thanks jonsjava Link to comment https://forums.phpfreaks.com/topic/108519-opening-link-not-in-browser/#findComment-557175 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.