PHP响应post请求上传文件的方法
程序员文章站
2022-08-10 17:51:28
本文实例讲述了php响应post请求上传文件的方法。分享给大家供大家参考,具体如下:
function send_file($url, $post = '', $...
本文实例讲述了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程序设计有所帮助。
下一篇: 深入php内核之php in array