开发者

POST file contents as Byte[] from PHP application to .NET ASP

开发者 https://www.devze.com 2023-04-13 01:25 出处:网络
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 t

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-开发者_C百科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!


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号