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;; 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; }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/42bb71f3ccd1fc5f1defd80bee294f9b.html