首頁 >後端開發 >php教程 >PHP响应post请求上传文件的方法_PHP

PHP响应post请求上传文件的方法_PHP

WBOY
WBOY原創
2016-05-28 13:13:151508瀏覽

本文实例讲述了PHP响应post请求上传文件的方法。分享给大家供大家参考,具体如下:

function send_file($url, $post = '', $file = '') {
  $eol = "\r\n";
  $mime_boundary = md5 ( time () );
  $data = '';
  $confirmation = '';
  date_default_timezone_set ( "Asia/Shanghai" );
  $time = date ( "Y-m-d H:i:s " );
  $post ["filename"] = $file [filename];
  foreach ( $post as $key => $value ) {
    $data .= '--' . $mime_boundary . $eol;
    $data .= 'Content-Disposition: form-data; ';
    $data .= "name=" . $key . $eol . $eol;
    $data .= $value . $eol;
  }
  $data .= '--' . $mime_boundary . $eol;
  $data .= 'Content-Disposition: form-data; name=' . $file [name] . '; filename=' . $file [filename] . $eol;
  $data .= 'Content-Type: text/plain' . $eol;
  $data .= 'Content-Transfer-Encoding: binary' . $eol . $eol;
  $data .= $file [filedata] . $eol;
  $data .= "--" . $mime_boundary . "--" . $eol . $eol;
  $params = array ('http' => array ('method' => 'POST', 'header' => 'Content-Type: multipart/form-data;boundary=' . $mime_boundary . $eol, 'content' => $data ) );
  $ctx = stream_context_create ( $params );
  $response = file_get_contents ( $url, FILE_TEXT, $ctx );
  return $response;
}

希望本文所述对大家PHP程序设计有所帮助。

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn