I have a site that was working on my local host, but swapping to a production server has caused a headache of problems. I can't really change the server at this point and trying to handle it.
I am attempting to download a file from a remote server. It uses .net to return a Zip file with a bunch of files in it:
$url = 'https://myremoteserver.com/getFiles?model=12345&Files=1,2,3,4';
If I enter the URL into my browser, the file downloads perfectly. But this server isn't accessible outside (my laptop can when on the VPN).
While hacked together from SO answers, my original code looks like this and works on my localhost:
ob_start();
$filepath= $url; //your folder file path
header($_SERVER['SERVER_PROTOCOL'].' 200 OK');
header("Content-Type: application/zip");
header("Content-Transfer-Encoding: Binary");
header("Content-Disposition:attachment;filename=".$filename);
while (ob_get_level())
{
ob_end_clean();
}
readfile($filepath);
exit;
On the server, I get no response to this at all. Chasing that tail, it looks like that security/SSL is stopping it from working.
I was able to get this code to work (but with CURLOPT_SSL_VERIFYHOST, false, and CURLOP_SSL_VERIFYPEER,false), but it returns the file as a string (basically a bunch of jibberish - the string shows some of the file path info in the jibberish).
The header is showing the file name, and now I need to :
- Grab the filename from the header
- Download the file to the client with the filename.
- All of the files are .zip
THE PHP FILE:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
//Need to figure out how to fix this too!
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($ch);
// Then, after your curl_exec call:
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = substr($response, 0, $header_size);
$body = substr($response, $header_size);
var_dump($header);
echo('</br></br>');
//File Name:
echo($body);
The Response:
string(409) "HTTP/1.1 200 OK Cache-Control: private Content-Type: application/zip Server: Microsoft-IIS/8.5 Access-Control-Allow-Origin: * Set-Cookie: ASP.NET_SessionId=rx1zv2ihyhui4eaqp2jkwtv1; path=/; HttpOnly; SameSite=Lax Content-Disposition: attachment; filename = 2012-08-16-MORE-FILE-NAME.zip X-AspNet-Version: 4.0.30319 X-Powered-By: ASP.NET Date: Thu, 10 Feb 2022 22:17:30 GMT Content-Length: 868 "
PK-/�JT���������FolderName\FILE.txtL���n�0�Olbi��E�^�L[��M��f�]�Dm��H�yP)�!M�N��^ xނwA���m�Ŏ�o;�#'�O��縃�sX�x�}�6j�����! �Z}[�ʯ�����;:`�)���s����$o�k��ˬw�of�e�ca#�-��3K��[��=@4�F��"�-4�����n��T��UGc��DC�bo���n1��U�·��%y��~�-�OmR��YZ��gn�A�#d�&u;��x��V�0�R�%�9����wny9V^~�B�f�����"Sm:)�)�j�ٱeq����n�P���ލ\@ٰ �3����-�u�~`�y�b��b��}�o�έ� ��68�<�s�m����;B�F,7�{%��!�}�]�%=�&פ������[�s�U��n�m�;��`U��,��)�N� �N���~o�-��Ywcay!]�A��#p0�NI6�W�7�&bt%v��v�8�O��6�n,Ǘ~�/Kmtܓq,W�� /L�hC#4�C�U������D�mVK�"M�# k�Zrv��S�!���o�ܠ�w2H:�G�a[�Hc����K�㫷�/����ٮ�v{R��\-�j���5���vvW����c}@b����)�����o�����~-��u���$v�b��L�j��"���B��#�K`�.��K�� ���PK3-/�JT�����������FolderName\FILE.txL�PKS�
So how do I get the $body to change to ZIP file (named in the Content Disposition)to a download?
Thank you for the help.
CodePudding user response:
As I understand you want your PHP script to serve the zip file as a download to the client.
So what you want to do is set a header to let the client know what it is being sent.
header("Content-Type: application/zip");
That should cause most browsers to download the file, but it really depends on what the handlers are that the client has for that mime type. To be anal you could use all these headers:
header("Content-Type: application/force-download");
header("Content-Type: application/download");
header("Content-Type: application/zip");
Then you might want to specify a file name for the download so that it isn't named after your php script:
header("Content-Disposition: attachment;filename=".$file_name);
Then you can output the body form your CURL request.
echo($body);
You don't want to output anything else, so no var_dump($header); or echoing any html tags.