1

How is it possible to automically redirect thankyou.php page to download ".DAT" file which is available at different directory.

File Dir = http://domain.com/storage/file.dat While thankyou.php is available at root http://domain.com/thankyou.php

Thanks for everyone answering my question.

Following @bruno-fidelis which I thank alot I'm getting the below error message for the file downloaded. I mean when the file is downloaded and open it I get this error message

<html>
<head>
<title>Thank You</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
</head>
<body>
<br />
<b>Warning</b>:  readfile(http://domain.com/path/to/file/filename.dat): failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found
in <b>/home/user/public_html/dir/success.php</b> on line <b>159</b><br />


Warning: filesize(): stat failed for http://domain.com/path/to/file/filename.dat in /home/user/public_html/dir/success.php on line 160

WiTon Nope
  • 166
  • 4
  • 20

2 Answers2

0

Set header in your PHP file to download file automatically.

header("Content-Disposition: attachment; filename=\"" . basename($File) . "\"");
header("Content-Type: application/force-download");
header("Content-Length: " . filesize($File));
header("Connection: close");

To download larger file size from server, change the below settings in php.ini file:

Upload_max_filesize  - 1500 M
Max_input_time  - 1000
Memory_limit    - 640M
Max_execution_time -  1800
Post_max_size - 2000 M

Refer this link also.

Hope it will help you :)

Community
  • 1
  • 1
Ravi Hirani
  • 6,511
  • 1
  • 27
  • 42
0

You can simply make a script setting the headers to the file. Example.

<?php

$file = file_get_contents('path/to/file.dat');
$size = filesize('path/to/file.dat');
$quotedFileName = '"'. basename('path/to/file.dat') .'"';

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . $quotedFileName);
header('Content-Transfer-Encoding: binary');
header('Connection: Keep-Alive');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . $size);
flush();
echo $file;
exit();

I suggest you read about http headers. Let me know if it works ;)

  • thanks for share it helps sure but still facing the size issue. – WiTon Nope Feb 18 '16 at 11:32
  • what kind of size issue ? if the file is to large you'll have to configure php.ini file to accept larger post size [check this link](http://stackoverflow.com/questions/3176942/using-php-to-download-files-not-working-on-large-files) – Bruno Fidelis Feb 18 '16 at 11:47
  • The file size ranges between 1MB till 4MB – WiTon Nope Feb 18 '16 at 12:04
  • @WiTonNope i've added flush() after the headers, give it a try. Just of curiosity, why you say it's a size issue ? – Bruno Fidelis Feb 18 '16 at 12:18
  • sorry for the late reply and thanks for updating the code. Unfortunately it didn't work also. I am saying it's size issue because the file is 1.74MB and when downloading it I get only 776B that's even less the half of the real size. – WiTon Nope Feb 18 '16 at 14:35
  • You could try using readfile($file); instead of file_get_contents there's an example on php [documentation](http://ca2.php.net/manual/en/function.readfile.php#48683) – Bruno Fidelis Feb 18 '16 at 15:28
  • I think I found the reason if I'm correct, the file link is auto-generated and not static everytime so this is the code I am using `$file = file_get_contents(''.$url.'/used/'.$products.'/'.$orderid.'.dat'); ` I think this is wrong maybe – WiTon Nope Feb 18 '16 at 15:52
  • Only one way to find out, in that case you should check if the file_exists(); – Bruno Fidelis Feb 18 '16 at 16:01
  • Hi sorry for the late reply. Thanks for the info it turns to be that file_get_contents not reading the dir correctly when I used readfile it worked however I had to override php.ini of the server as readfile() was disabled by server do you think it's risky to open allow_fopen? – WiTon Nope Feb 19 '16 at 16:42
  • It is risky to allow_fopen because you can potentially allow for attackers inject code on your application. That been said, if you need to fetch a remote file safely, an alternative is to use php function curl(); witch does not require allow_fopen. – Bruno Fidelis Feb 19 '16 at 16:54
  • exactly this is what I know thus I've blocked allow_fopen from the server and opened it into the account side however I'll try curl as you said I missed that part sorry. One thing is the file isn't being read in the right way still I'll update my question with error please check it. – WiTon Nope Feb 19 '16 at 17:21