Upload binary data via html-free post

Asked

Viewed 291 times

-1

I wanted a solution to do exactly what this html does, however using only php: uploading a binary data 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 didn’t 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);

?>

1 answer

0

What you really want is to make a remote request through a PHP script by sending a file along with this 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.

Behold:

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

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

That one 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 to know how the library does to perform this procedure, you can look at the repository on Github

Upload files to nail

To make that code in face and courage, the line of reasoning is actually to use stream_context_create. But in this case, you must configure the Content-Type correctly. When it comes to upload, you don’t use x-www-form-urlencoded, and yes 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 was taken from SOEN

  • Helped friend. you would know to give the same example using Curl library ?

  • I can search and post but since I’m on my cell phone this could take a couple of hours ;)

  • I love you, man.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.