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

yipeiwu_com5年前PHP代码库

本文实例讲述了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创建XML的方法示例【基于DOMDocument类及SimpleXMLElement类】

本文实例讲述了PHP创建XML的方法。分享给大家供大家参考,具体如下: 使用DOMDocument类创建xml config.php <?php $doc = new...

PHP 验证身份证是否合法的函数

话不多说,请看代码: function is_idcard( $id ) { $id = strtoupper($id); $regx = "/(^\d{15}$)|(^\d...

PHP中其实也可以用方法链

简单示意一下: 复制代码 代码如下: <?php class test { private $_name = ''; public function setName($name)...

php让图片可以下载的代码第1/2页

// 文件目录 define(‘DL_DIR', ‘temp/'); // 常见扩展名所对应的MIME类型 $MIMETypes = array( ‘ez' => ‘applica...

支持生僻字且自动识别utf-8编码的php汉字转拼音类

拼音类文件py_class.php源码如下: <?php class py_class{ function py_class(){ $this ->...