开发者

php file upload with "file_get_contents" to a server

开发者 https://www.devze.com 2023-04-11 12:13 出处:网络
is it possible to send a file to a server at the same way like this: $file = \'myfile.txt\'; or $file = file_get_contents(./myfile.txt);

is it possible to send a file to a server at the same way like this:

$file = 'myfile.txt';

or

$file = file_get_contents(./myfile.txt);

...

$postdata = http_build_query( array( 
'var1' => 'some content',
'var2' => $file 
) );

$opts = array('http' => array( 'method' => 'POST', 'header' => 'Content-type: application/x-www开发者_开发技巧-form-urlencoded', 'content' => $postdata ) );

$context = stream_context_create($opts);

$result = file_get_contents('http://example.com/submit.php', false, $context);

Thanks


Why don't you use cURL instead of streams? It's so easy:

$ch = curl_init('http://www.url.com');

curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
    'file_input' => '@/path/to/file',
));

curl_exec($ch);


Yes, but its a tedious process. If you want to post something use cURL or with the normal $_FILE method.

0

精彩评论

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