Jump to content

How to force file download of non-existing file?


sspoke

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 :P

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.