Skip to content

Instantly share code, notes, and snippets.

@delineas
Forked from maxivak/__upload_file.md
Created March 22, 2021 08:33
Show Gist options
  • Select an option

  • Save delineas/e1174aaf6f52d57575796c53b0f4febe to your computer and use it in GitHub Desktop.

Select an option

Save delineas/e1174aaf6f52d57575796c53b0f4febe to your computer and use it in GitHub Desktop.

Revisions

  1. @maxivak maxivak revised this gist Nov 23, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion __upload_file.md
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,7 @@ We will use curl functions.
    $fields = array("f1"=>"value1", "another_field2"=>"anothervalue");
    // files to upload
    $filenames = array("/tmp/1.jpg", "/tmp/2.png");;
    $filenames = array("/tmp/1.jpg", "/tmp/2.png");
    $files = array();
    foreach ($filenames as $f){
  2. @maxivak maxivak renamed this gist Aug 23, 2017. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. @maxivak maxivak revised this gist Aug 23, 2017. 2 changed files with 10 additions and 6 deletions.
    2 changes: 2 additions & 0 deletions references
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,2 @@
    see examples:
    https://gist.github.com/iansltx/a6ed41d19852adf2e496
    14 changes: 8 additions & 6 deletions upload_file.md
    Original file line number Diff line number Diff line change
    @@ -2,22 +2,24 @@ We want to upload file to a server with POST HTTP request.
    We will use curl functions.

    ```
    // data fields for POST request
    $fields = array("f1"=>"value1", "another_field2"=>"anothervalue");
    // files to upload
    $filenames = array("/tmp/1.jpg", "/tmp/2.png");;
    $files = array();
    foreach ($filenames as $f){
    $files[$f] = file_get_contents($f);
    }
    // more fields for POST request
    $fields = array("f1"=>"value1", "another_field2"=>"anothervalue");
    // URL to upload to
    $url = "http://site.com/upload";
    // curl
    $curl = curl_init();
    $url_data = http_build_query($data);
  4. @maxivak maxivak revised this gist Aug 23, 2017. 1 changed file with 6 additions and 1 deletion.
    7 changes: 6 additions & 1 deletion upload_file.md
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,12 @@ We will use curl functions.
    ```
    $filenames = array("/tmp/1.jpg", "/tmp/2.png");;
    $files = [$file_tmp_name => file_get_contents($file_tmp_name)];
    $files = array();
    foreach ($filenames as $f){
    $files[$f] = file_get_contents($f);
    }
    // more fields for POST request
    $fields = array("f1"=>"value1", "another_field2"=>"anothervalue");
  5. @maxivak maxivak created this gist Aug 23, 2017.
    94 changes: 94 additions & 0 deletions upload_file.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,94 @@
    We want to upload file to a server with POST HTTP request.
    We will use curl functions.

    ```
    $filenames = array("/tmp/1.jpg", "/tmp/2.png");;
    $files = [$file_tmp_name => file_get_contents($file_tmp_name)];
    // more fields for POST request
    $fields = array("f1"=>"value1", "another_field2"=>"anothervalue");
    $url = "http://site.com/upload";
    $curl = curl_init();
    $url_data = http_build_query($data);
    $boundary = uniqid();
    $delimiter = '-------------' . $boundary;
    $post_data = build_data_files($boundary, $fields, $files);
    curl_setopt_array($curl, array(
    CURLOPT_URL => $url,
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 30,
    //CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "POST",
    CURLOPT_POST => 1,
    CURLOPT_POSTFIELDS => $post_data,
    CURLOPT_HTTPHEADER => array(
    //"Authorization: Bearer $TOKEN",
    "Content-Type: multipart/form-data; boundary=" . $delimiter,
    "Content-Length: " . strlen($post_data)
    ),
    ));
    //
    $response = curl_exec($curl);
    $info = curl_getinfo($curl);
    //echo "code: ${info['http_code']}";
    //print_r($info['request_header']);
    var_dump($response);
    $err = curl_error($curl);
    echo "error";
    var_dump($err);
    curl_close($curl);
    function build_data_files($boundary, $fields, $files){
    $data = '';
    $eol = "\r\n";
    $delimiter = '-------------' . $boundary;
    foreach ($fields as $name => $content) {
    $data .= "--" . $delimiter . $eol
    . 'Content-Disposition: form-data; name="' . $name . "\"".$eol.$eol
    . $content . $eol;
    }
    foreach ($files as $name => $content) {
    $data .= "--" . $delimiter . $eol
    . 'Content-Disposition: form-data; name="' . $name . '"; filename="' . $name . '"' . $eol
    //. 'Content-Type: image/png'.$eol
    . 'Content-Transfer-Encoding: binary'.$eol
    ;
    $data .= $eol;
    $data .= $content . $eol;
    }
    $data .= "--" . $delimiter . "--".$eol;
    return $data;
    }
    ```