1--TEST--
2CURL file uploading
3--EXTENSIONS--
4curl
5--FILE--
6<?php
7
8function testcurl($ch, $name, $mime = '', $postname = '')
9{
10    if(!empty($postname)) {
11        $file = new CurlFile($name, $mime, $postname);
12    } else if(!empty($mime)) {
13        $file = new CurlFile($name, $mime);
14    } else {
15        $file = new CurlFile($name);
16    }
17    curl_setopt($ch, CURLOPT_POSTFIELDS, array("file" => $file));
18    var_dump(curl_exec($ch));
19}
20
21include 'server.inc';
22$host = curl_cli_server_start();
23$ch = curl_init();
24curl_setopt($ch, CURLOPT_URL, "{$host}/get.inc?test=file");
25curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
26
27testcurl($ch, __DIR__ . '/curl_testdata1.txt');
28testcurl($ch, __DIR__ . '/curl_testdata1.txt', 'text/plain');
29testcurl($ch, __DIR__ . '/curl_testdata1.txt', '', 'foo.txt');
30testcurl($ch, __DIR__ . '/curl_testdata1.txt', 'text/plain', 'foo.txt');
31
32$file = new CurlFile(__DIR__ . '/curl_testdata1.txt');
33$file->setMimeType('text/plain');
34var_dump($file->getMimeType());
35var_dump($file->getFilename());
36curl_setopt($ch, CURLOPT_POSTFIELDS, array("file" => $file));
37var_dump(curl_exec($ch));
38
39$file = curl_file_create(__DIR__ . '/curl_testdata1.txt');
40$file->setPostFilename('foo.txt');
41var_dump($file->getPostFilename());
42curl_setopt($ch, CURLOPT_POSTFIELDS, array("file" => $file));
43var_dump(curl_exec($ch));
44
45try {
46    curl_setopt($ch, CURLOPT_SAFE_UPLOAD, 0);
47} catch (ValueError $exception) {
48    echo $exception->getMessage() . "\n";
49}
50
51$params = array('file' => '@' . __DIR__ . '/curl_testdata1.txt');
52curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
53var_dump(curl_exec($ch));
54
55curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true);
56$params = array('file' => '@' . __DIR__ . '/curl_testdata1.txt');
57curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
58var_dump(curl_exec($ch));
59
60curl_setopt($ch, CURLOPT_URL, "{$host}/get.inc?test=post");
61$params = array('file' => '@' . __DIR__ . '/curl_testdata1.txt');
62curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
63var_dump(curl_exec($ch));
64
65curl_close($ch);
66?>
67--EXPECTF--
68string(%d) "curl_testdata1.txt|application/octet-stream|6"
69string(%d) "curl_testdata1.txt|text/plain|6"
70string(%d) "foo.txt|application/octet-stream|6"
71string(%d) "foo.txt|text/plain|6"
72string(%d) "text/plain"
73string(%d) "%s/curl_testdata1.txt"
74string(%d) "curl_testdata1.txt|text/plain|6"
75string(%d) "foo.txt"
76string(%d) "foo.txt|application/octet-stream|6"
77curl_setopt(): Disabling safe uploads is no longer supported
78string(0) ""
79string(0) ""
80string(%d) "array(1) {
81  ["file"]=>
82  string(%d) "@%s/curl_testdata1.txt"
83}
84"
85