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!

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

7 Comments

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..
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.
I took it out for the purpose of the simplicity in my example ^
@Vanthel: Oh well. So I think you should hexdump the binary string and triple check you're accessing the right file.
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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.