0

I have a very complicated task here and I need help. Maye somebody could help and has the missing clue.

I need to request a php-form-page with curl (and send cookies). All of this is fine and works. But the form page search for a $_FILE['picture'] and can't find it.

I tried it before with a simple empty 'picture' name and also with 'picture' => '@'.

I can send a "real" file name from my desk and create a CURL-FILE-object, but I don't want to add a file.

It should be the same, if I have an empty upload form.

I want to get:

$_FILE = Array
(
    [bild] => Array
        (
            [name] => 
            [type] => 
            [tmp_name] => 
            [error] => 4
            [size] => 0
        )

)

How can I force this with curl if I request the PHP-page?

I tried it with this way: PHP Send local file by cURL and it works. But I don't want to send a file, I will only force the file array to pass the form.

Any ideas? Thank you in advance for helping!

This is the current/last state:

<?php

$ch = curl_init();

$data = array('name' => 'Foo', 'file' => '@');

curl_setopt($ch, CURLOPT_URL, 'http://localhost/testpage.php');
curl_setopt ($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_HTTPHEADER,
            array(
                'Content-Type: multipart/form-data'
            )
);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

curl_exec($ch);
curl_close($ch);

?>

And results only:

$_POST:     Array
    (
        [name] => Foo
        [file] => @
    )
$_FILES:    Array
    (
    )
Community
  • 1
  • 1
rene
  • 1
  • 2

1 Answers1

0

You can do it by building the raw post data manually. Here is an example

$file = 'empty.txt';

$ending = "\r\n";

$boundary = md5(microtime());
$fullBoundary = sprintf("--%s%s", $boundary, $ending);

$body = '';

$body .= $fullBoundary;
$body .= "Content-Disposition: form-data; name=\"name\"" . $ending . $ending;
$body .= "Foo" . $ending;
$body .= $fullBoundary;
$body .= "Content-Disposition: form-data; name=\"file\"; filename=\"$file\"" . $ending;
$body .= "Content-Type: text/plain; charset=utf8" . $ending . $ending;
$body .= chunk_split("contents here") . $ending;
$body .= "--" . $boundary . "--" . $ending . $ending;

$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    "Content-Type: multipart/form-data; boundary=".$boundary)
);

curl_setopt($ch, CURLOPT_URL, "http://localhost/testpage.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);

This results in a body of

--f47db631bcd2c6ca1de88f95da0e132a
Content-Disposition: form-data; name="name"

Foo
--f47db631bcd2c6ca1de88f95da0e132a
Content-Disposition: form-data; name="file"; filename="empty.txt"
Content-Type: text/plain; charset=utf8

contents here
--f47db631bcd2c6ca1de88f95da0e132a--

And then server side, here is the output

$_POST    Array
(
    [name] => Foo
)
$_FILES    Array
(
    [file] => Array
    (
        [name] => empty.txt
        [type] => text/plain
        [tmp_name] => /tmp/phpdTOn9n
        [error] => 0
        [size] => 13
    )

)