Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用curl和php强制使用空的$\u文件元素?_Php_File_Curl_Cookies_Upload - Fatal编程技术网

如何使用curl和php强制使用空的$\u文件元素?

如何使用curl和php强制使用空的$\u文件元素?,php,file,curl,cookies,upload,Php,File,Curl,Cookies,Upload,我有一个非常复杂的任务,我需要帮助。也许有人能帮忙找到丢失的线索 我需要请求一个带有curl的php表单页面并发送cookies。所有这些都很好,而且很有效。但是表单页面搜索$u文件['picture'],却找不到它 我以前用一个简单的空“picture”名称和“picture”=>“@”尝试过它 我可以从桌面发送一个真实的文件名并创建一个CURL文件对象,但我不想添加文件 如果我有一个空的上传表单,它应该是相同的 我想得到: $_FILE = Array ( [bild] =>

我有一个非常复杂的任务,我需要帮助。也许有人能帮忙找到丢失的线索

我需要请求一个带有curl的php表单页面并发送cookies。所有这些都很好,而且很有效。但是表单页面搜索$u文件['picture'],却找不到它

我以前用一个简单的空“picture”名称和“picture”=>“@”尝试过它

我可以从桌面发送一个真实的文件名并创建一个CURL文件对象,但我不想添加文件

如果我有一个空的上传表单,它应该是相同的

我想得到:

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

)
如果我请求PHP页面,如何使用curl强制执行此操作

我用这种方法试过了,效果很好。但我不想发送文件,我只会强制文件数组传递表单

有什么想法吗? 提前感谢您的帮助

这是当前/最后一个状态:


您可以通过手动构建原始post数据来实现这一点。这里有一个例子

$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);
这导致了一系列的问题

--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--
然后是服务器端,这里是输出

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

)
--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--
$_POST    Array
(
    [name] => Foo
)
$_FILES    Array
(
    [file] => Array
    (
        [name] => empty.txt
        [type] => text/plain
        [tmp_name] => /tmp/phpdTOn9n
        [error] => 0
        [size] => 13
    )

)