Bramastic
Bramastic

Reputation: 399

How to receive an uploaded file which is not sent as multipart/form? PHP

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!

Upvotes: 2

Views: 111

Answers (2)

Bramastic
Bramastic

Reputation: 399

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

Upvotes: 0

Fakhruddin Ujjainwala
Fakhruddin Ujjainwala

Reputation: 2553

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.

Upvotes: 0

Related Questions