2

I got the docs from the third party that is sending me a file over http protocol and I need to write a script that will successfully receive the sent file. Content-type is set as application/gzip so I can't pick up the file uploaded using a $_FILES variable as it would be easy with multipart/form-data.

This link gave me a hint: http://php.net/manual/en/features.file-upload.post-method.php

Note: Be sure your file upload form has attribute enctype="multipart/form-data" otherwise the file upload will not work.

I tried to reproduce their "client" side to test my server using the example in the following url http://blog.derakkilgo.com/2009/06/07/send-a-file-via-post-with-curl-and-php/

And to ensure crosdomain posting is available, I used a function posted and explained by @slashingweapon CORS with php headers

There must be a way to do it - Halp!

Community
  • 1
  • 1
Bramastic
  • 399
  • 4
  • 16
  • Third party sending you URL of file ? – Monty Feb 22 '16 at 10:55
  • 1
    I think we need some additional informations. What is in the POST? Only the file? In this case, have you tried through `file_get_contents("php://input")`? – fusion3k Feb 22 '16 at 10:57
  • 1
    Also: I understand that 3rd part connect to your http server sending data: it is correct? – fusion3k Feb 22 '16 at 11:10
  • @fusion3k This is giving some results. Now I need to parse the input. It looks like a trap. Bin files might get corrupted if not done properly. – Bramastic Feb 22 '16 at 11:12

2 Answers2

0

Hi what I understand is you just need to download the file that has been uploaded to a server, try getting the file in binary mode using the below code:

ftp_fget($conn_id, $file, FTP_BINARY, 0);

Ref: http://php.net/manual/en/function.ftp-get.php

The connection details must be shared with you by the 3rd party who is sharing the file.

Fakhruddin Ujjainwala
  • 2,493
  • 17
  • 26
  • Thank you for the answer but that's not how it should work. Exclusively HTTP protocol is used. – Bramastic Feb 22 '16 at 11:08
  • You must be having a documentation given from 3rd part over how to fetch it. Can I have a look over the testing code shared with you to exactly know what is required to be done. – Fakhruddin Ujjainwala Feb 22 '16 at 11:10
0

Thanks to @fusion3k who pointed me to the right direction.

$rawData = file_get_contents("php://input") gave the the RAW data from which I had to parse the file from. It was painful to extract it because it was a binary file. I used

  1. Explode the data with "\n" as a delimiter $rawData = explode("\n", $rawData)
  2. Skip first few lines (it was 3 lines of header data for me) and take the rest into $valuableData
  3. Convert the last line of data from string to binary using $convertedLine = unpack("H*", $valuableData[count($valuableData)-1])
  4. Cut out the last byte of data from the last row $convertedLine[1] = substr($convertedLine[1], 0, -2) ( have no idea why is `unpack returning an array)
  5. $valuableData[count($valuableData)-1] = pack("H*", $convertedLine[1])
  6. implode("\n", $valuableData) and write that to a file
Bramastic
  • 399
  • 4
  • 16