Uploading binary data via post without html

-1

I wanted a solution to do exactly what this html does, however using only php: upload a given binary and normal fields (strings).

<center>
<form enctype="multipart/form-data" action="http://10.40.0.241/api/mailingup/index.php" method="POST">
    <!-- MAX_FILE_SIZE must precede the file input field -->
    <input type="hidden" name="MAX_FILE_SIZE" value="9000000" />
    <input type="hidden" name="uploading" value="42">

    <table cellspacing=0 cellpadding=0>
        <tr><td>Login</td><td><input type="text" name="login"></td></tr>
        <tr><td>Senha</td><td><input type="text" name="senha"></td></tr>
        <tr><td>Auto DDD</td><td><input type="checkbox" name="auto_ddd"></td></tr>
        <tr><td>DDD</td><td><input type="text" name="ddd"></td></tr>
        <tr><td class="option1">Campanha ID</td><td class="option1"><input type="text" name="CampanhaId"></td></tr>
        <tr><td class="option2">Arquivo</td><td class="option2"><input type="file" name="arquivo" style="width:500px;"></td></tr>

    </table>
    <INPUT TYPE="SUBMIT" NAME="Salvar" VALUE="Carregar" onclick="SalvarOperador();">
</form>

I made a code but it did not work.

<?php

$arq = fopen("teste.csv", 'rb');
$fileContents = stream_get_contents($arq);
fclose($arq);

$campos = array(
    "login" => "rafael",
    "senha" => "master",
    "CampanhaId" => "177",
    "arquivo" => $fileContents,
    "uploading" => "42",
    "auto_ddd" => "on",
    "ddd" => "21"
);


$content = http_build_query($campos);

$context = stream_context_create(
        array(
        "http" => array(
                'Content-type' => 'application/x-www-form-urlencoded'
                'method' => 'POST',
                'content' => $content
            )
        )
    );
$url = 'http://10.40.0.241/api/mailingup/index.php';

$fp = fopen($url, 'rb',false,$context);
echo stream_get_contents($fp);

? >

    
asked by anonymous 18.01.2017 / 19:51

1 answer

0

What you really want is to make a remote request through a PHP script, sending a file along with that request.

I have two options to offer:

Uploading files with the Guzzle \ Http library

One way I know to upload using only one PHP client is through Guzzle.

See:

$body = fopen('/arquivo.txt', 'r');
$response = $client->request('POST', 'http://httpbin.org/post', ['body' => $body]);

See that there is not much secret. In this case, it will send stream based on the file that was opened.

This stream could also be a string any. See:

$body = \GuzzleHttp\Psr7\stream_for('hello!');
 $client->request('POST', 'http://httpbin.org/post', ['body' => $body]);

To install Guzzle, you can use Composer.

composer require guzzlehttp/guzzle

If you're curious about how the library does this, you can look at repository on Github

Uploading files to the nail

In order to make this code in the face and courage , the reasoning line is actually using stream_context_create . But in this case, you should set Content-Type correctly. When it comes to uploading, you do not use x-www-form-urlencoded , but multipart/form-data .

$context = stream_context_create(array(
    "http" => array(
        "method" => "POST",
        "header" => "Content-Type: multipart/form-data; boundary=--foo\r\n",
        "content" => "--foo\r\n"
            . "Content-Disposition: form-data; name=\"myFile\"; filename=\"image.jpg\"\r\n"
            . "Content-Type: image/jpeg\r\n\r\n"
            . file_get_contents("image.jpg") . "\r\n"
            . "--foo--"
    )
));

$html = file_get_contents("http://example.com/upload.php", false, $context);

Code above has been taken from SOEN

    
18.01.2017 / 19:56