1

I need to post the contents of a file to a webpage (C#) in the form of a Byte[] from PHP. I've tried using cURL and stream_context_create() but what I think it comes down to is how I'm reading out the file.

My post content/CURLOPT_POSTFIELDS looks like:

$data .= "\r\n--" . $boundary . "\r\n";
$data .= "Content-Disposition: form-data; name=\"file\"; filename=\"" . $files['name'][$id] . "\"\r\n";
$data .= "Content-Type: application/octet-stream\r\n\r\n";
$data .= file_get_contents($files['tmp_name'][$id]) . "\r\n";
$data .= "--" . $boundary . "--\r\n";

But this just doesn't work.

Post byte array from PHP to .NET WCF Service says that file_get_contents() is compatible - but I'm not finding any evidence of this.

http://bytes.com/topic/php/answers/869014-how-convert-gif-file-byte-array-thanks states this isn't possible. (Nothing is impossible!)

I've also tried using variations of fread() fopen() and file() etc

PS I know the webservice is working fine as a somewhat identical script in C++/Java etc works fine. file.readAll() being the only difference where the file_get_contents() line is...

Grateful for any insigtfull comments or an answer to fix my woes!

Community
  • 1
  • 1
amcc
  • 2,608
  • 1
  • 20
  • 28

1 Answers1

1

file_get_contents() is compatible. It will return the binary string of the file's content which is what you're looking for according to your question. Just to clarify this upfront.

I assume that you have an error else-where or you are using that data in the wrong context. Where is not clear from your question to me.

What I see in your code however is, that you don't do any error checking and handling.

Set PHP's error handling to the maximum level in your PHP configuration and log errors. Then monitor the error log.

Also check for errors in the file upload process, the PHP Manual has a complete entry about how it works incl. error handling of file-uploads.

hakre
  • 193,403
  • 52
  • 435
  • 836
  • I am inclined to agree that the error is likely at my end. I do have a number of try catches about the place as well as a standard `if ($err === UPLOAD_ERR_OK)`. The form is definitely posting to the server and the content-length is being consistent with the size of the files I'm uploading. I will try upping the error handling (to my knowledge it's already maxed out) however.. – amcc Oct 12 '11 at 16:39
  • At least for the `file_get_contents` line I see no error checking, the other notes were merely a tip as it's somehow common that folks tend to forget. – hakre Oct 12 '11 at 16:43
  • I took it out for the purpose of the simplicity in my example ^ – amcc Oct 12 '11 at 16:46
  • @Vanthel: Oh well. So I think you should [hexdump](http://stackoverflow.com/questions/1057572/how-can-i-get-a-hex-dump-of-a-string-in-php) the binary string and triple check you're accessing the right file. – hakre Oct 12 '11 at 17:02
  • I can't for the life of me find anything wrong with my code if indeed `file_get_contents()` is enough. Can only assume something in the request is missing that the server isn't checking for. Will hopefully confirm this with the server engineer and be able to prove you right :) – amcc Oct 12 '11 at 17:44
  • 1
    Well, jump in with a network sniffer and ensure the data you send matches your expected transfer format. Should help you to find the place where things get broken. – hakre Oct 12 '11 at 17:49
  • Good shout on the sniffing. Fixed a few problems and we're rolling! Thanks a lot for the help. All the inspiration I needed – amcc Oct 12 '11 at 19:24