-1

Looking at Force Download of Files with PHP and Force download file with PHP giving empty file and the one that brought me closest, How to force download an image without a script, but with $_GET?

I'm still unable to come up with a solution to my problem.

I have a html file that allows a user to submit a file to post a file to my 'upload_file.php' PHP script.

<html>
<body>

<form action="upload_file.php" method="post" enctype="multipart/form-data">
    <label for="file">Filename:</label>
    <input type="file" name="file" id="file"><br>
    <input type="submit" name="submit" value="Submit">
</form>

</body>
</html>

upload_file.php takes in the file, does a bunch of changes to it, pulls data out, and generates an xlsx file in the end via PHPExcel. It's a simple format conversion with a little bit of logic. Currently, for testing, I just had the file written alongside the php file. I could have the upload.php file output a link to said file and call it good.

However, I'd rather the user click the initial upload button and after a few seconds of crunching, the generated file starts downloading to their system. The above links seem to talk about csv or html files being downloaded -- vs an actual file.

Just 'echoing' the filename doesn't seem to do it (as has been suggested in those other threads). I also see a suggestion to just 'set the headers' correctly, which I found the content type should be "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" for an xlsx file -- but no one really fleshes out fully what 'set the headers' means nor how to do it.

I can't imagine it's very hard, but I can't seem to sort it. Can anyone help this beginner?

Also, if possible, I'd love it if the file never existed on the server at all -- but I'm not sure if that's possible with PHPExcel objects...

Community
  • 1
  • 1
lowcrawler
  • 6,777
  • 9
  • 37
  • 79
  • 2
    " The above links seem to talk about csv or html files being downloaded -- vs an actual file." ?? Because html files or csv files are fake files somehow? – PeeHaa Aug 28 '14 at 16:17
  • 1
    Your question is so bloated with unneeded info that I don't understand your actual problem... – Niels Keurentjes Aug 28 '14 at 16:19
  • @NielsKeurentjes When someone uploads a file, he wishes to perform some mutations, turn it into an xlsx file, then "prompt" for file download once that task has run. – Ohgodwhy Aug 28 '14 at 16:21
  • So it was all just about `Content-disposition`? I couldn't make head nor tails from it haha. – Niels Keurentjes Aug 28 '14 at 20:42
  • @NielsKeurentjes - Strange that others didn't seem to have a problem understanding. Thanks to those that helped! – lowcrawler Aug 30 '14 at 20:23

1 Answers1

3

The user uploads the file and you can remember the name of the file uploaded.

By then you can use the answers previously answered to questions on how to force file downloads.

Pit Digger's solution:

$file_url = 'http://www.myremoteserver.com/file.exe';
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary"); 
header("Content-disposition: attachment; filename=\"" . basename($file_url) . "\""); 
readfile($file_url); // do the double-download-dance (dirty but worky)

genesis's solution:

header("Content-Type: application/octet-stream");
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"file.exe\""); 
echo readfile($url);

or, for exe files:

header("Location: $url");

Note: It would be nice to see what you've tried so far to see if you have any idea about how to achieve the task, it even might tell us better what you're trying to achieve. If this is not the answer to your question then I highly suggest that you edit your question to enlighten us because it could be more clear.

Community
  • 1
  • 1
Jonast92
  • 4,964
  • 1
  • 18
  • 32
  • 1
    This did the trick, in that the file downloads now. I added genesis's solution. However, the file downloads 'incorrectly' (ie: the file on the server opens fine, but the file that is downloaded is viewed as corrupt) and I will open a new question about that. – lowcrawler Aug 28 '14 at 17:34