Home Backend Development PHP Tutorial PHP method to upload files in response to post request

PHP method to upload files in response to post request

Jul 29, 2016 am 09:10 AM
content data eol file

The example in this article describes the method of PHP responding to post requestuploading files. Share it with everyone for your reference, the details are as follows:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

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;

}

Copy after login

I hope this article will be helpful to everyone in PHP programming.

The above introduces the method of uploading files in PHP in response to post requests, including the content of uploading files. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hongmeng native application random poetry Hongmeng native application random poetry Feb 19, 2024 pm 01:36 PM

Hongmeng native application random poetry

Use java's File.length() function to get the size of the file Use java's File.length() function to get the size of the file Jul 24, 2023 am 08:36 AM

Use java's File.length() function to get the size of the file

How to convert php blob to file How to convert php blob to file Mar 16, 2023 am 10:47 AM

How to convert php blob to file

Rename files using java's File.renameTo() function Rename files using java's File.renameTo() function Jul 25, 2023 pm 03:45 PM

Rename files using java's File.renameTo() function

Understand the meaning of eol in PHP Understand the meaning of eol in PHP Mar 20, 2024 am 11:09 AM

Understand the meaning of eol in PHP

Use java's File.getParentFile() function to get the parent directory of the file Use java's File.getParentFile() function to get the parent directory of the file Jul 27, 2023 am 11:45 AM

Use java's File.getParentFile() function to get the parent directory of the file

Use java's File.getParent() function to get the parent path of the file Use java's File.getParent() function to get the parent path of the file Jul 24, 2023 pm 01:40 PM

Use java's File.getParent() function to get the parent path of the file

The definition and use of eol in PHP The definition and use of eol in PHP Mar 20, 2024 pm 12:12 PM

The definition and use of eol in PHP

See all articles