sspoke Posted July 31, 2010 Share Posted July 31, 2010 Hello I already know this is possible with php <?php $file = 'monkey.gif'; if (file_exists($file)) { header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename='.basename($file)); header('Content-Transfer-Encoding: binary'); header('Expires: 0'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Pragma: public'); header('Content-Length: ' . filesize($file)); ob_clean(); flush(); readfile($file); exit; } ?> But I am wondering is it possible to just send over a string of data stored in like $data and create a file clientside (nothing server side). So i'm guessing readfile() has to be replaced with something Quote Link to comment https://forums.phpfreaks.com/topic/209457-how-to-force-file-download-of-non-existing-file/ Share on other sites More sharing options...
monkeytooth Posted July 31, 2010 Share Posted July 31, 2010 Seeing as PHP is a server side script, your not going to be able to do it with PHP. Generally speaking due to security concerns most browsers wont let you create and store a file like that other than cookies just some where on the user end without consent. On the flip side there is ways of tripping up the save dialog and starting the save process based on a means of where a file doesn't exist. But I think your going to likely need the use of javascript to do what you want as that can be used as a client side script after the page has been loaded. Quote Link to comment https://forums.phpfreaks.com/topic/209457-how-to-force-file-download-of-non-existing-file/#findComment-1093661 Share on other sites More sharing options...
Alex Posted July 31, 2010 Share Posted July 31, 2010 Not sure what monkeytooth is talking about. You can do what you're trying to do like this: $filename = 'somename.txt'; $data =<<<DATA file data... DATA; header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename=' . $filename); header('Content-Transfer-Encoding: binary'); header('Expires: 0'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Pragma: public'); header('Content-Length: ' . strlen($data)); echo $data; It's more or less the same thing, readfile just outputs the contents of the file, so you can just echo what you want the contents of the file to be. It's not like the browser knows if there's a real file on the server; it just gets the file data and the filename. Quote Link to comment https://forums.phpfreaks.com/topic/209457-how-to-force-file-download-of-non-existing-file/#findComment-1093665 Share on other sites More sharing options...
sspoke Posted July 31, 2010 Author Share Posted July 31, 2010 Not sure what monkeytooth is talking about. You can do what you're trying to do like this: $filename = 'somename.txt'; $data =<<<DATA file data... DATA; header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename=' . $filename); header('Content-Transfer-Encoding: binary'); header('Expires: 0'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Pragma: public'); header('Content-Length: ' . strlen($data)); echo $data; It's more or less the same thing, readfile just outputs the contents of the file, so you can just echo what you want the contents of the file to be. It's not like the browser knows if there's a real file on the server; it just gets the file data and the filename. yup before you wrote that I got this. header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename=chatlog-'.date("m.d.y").'.txt'); header('Content-Transfer-Encoding: binary'); header('Expires: 0'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Pragma: public'); header('Content-Length: ' . strlen($log)); ob_clean(); flush(); echo $log; I just don't know should I leave that ob_clean(); then flush(); then start outputting my data.?? forgot that echo is outputting to browser Quote Link to comment https://forums.phpfreaks.com/topic/209457-how-to-force-file-download-of-non-existing-file/#findComment-1093666 Share on other sites More sharing options...
monkeytooth Posted August 1, 2010 Share Posted August 1, 2010 your right, I wasn't thinking about using the headers combined with the string/data to do that. Then again I think when I read it early and replied I had something entirely else thought of, in mind. Quote Link to comment https://forums.phpfreaks.com/topic/209457-how-to-force-file-download-of-non-existing-file/#findComment-1093741 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.