【问题标题】:php file upload with "file_get_contents" to a server使用“file_get_contents”将php文件上传到服务器
【发布时间】:2011-10-08 19:59:46
【问题描述】:

是否可以像这样将文件发送到服务器:

$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);

谢谢

【问题讨论】:

标签: php http post


【解决方案1】:

为什么不使用 cURL 而不是流?很简单:

$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);

【讨论】:

    【解决方案2】:

    是的,但这是一个乏味的过程。如果您想发布内容,请使用 cURL 或普通的 $_FILE 方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-03
      • 1970-01-01
      • 2016-03-31
      • 2019-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多