在没有HTML表单的情况下从curl获取$ _FILES

时间:2015-05-01 19:33:42

标签: php curl

如果有人要通过表单上传图片:

<form method="post" accept="image/*" enctype="multipart/form-data">
    <input size="28" name="image_upload" type="file">
    <input type="submit" value="Submit">
</form>

在PHP中,您可以通过image_upload变量获取上传的$_FILES

if(isset($_FILES['image_upload']))
     var_dump($_FILES['image_upload'])

您可以继续使用它,例如显示tmp_namemove_uploaded_file

如果他们要使用CURL,我是如何允许某人上传图片的,但是没有表单?

   $POST_DATA = array(
    'ImageData' => file_get_contents('image.jpg'),
    );
    $handle = curl_init($url);
    curl_setopt($handle, CURLOPT_URL, 'http://example.com/?image');
    curl_setopt($handle, CURLOPT_POSTFIELDS, 'image={$POST_DATA['ImageData']}');
    curl_setopt($handle, CURLOPT_POST, 1);
    curl_close($handle);

然后在我收到卷曲请求的页面中......

if(isset($_REQUEST['image']))
    var_dump($_FILES); // won't work because i can't get $_FILES from $_POST['image']
var_dump($_POST['image']) // will only show the image data.. i want it to show what the $_FILES variable would normally show.

1 个答案:

答案 0 :(得分:1)

这在php文档中得到了解答......示例#2(http://uk.php.net/manual/en/function.curl-setopt.php

<?php

/* http://localhost/upload.php:
print_r($_POST);
print_r($_FILES);
*/

$ch = curl_init();

$data = array('name' => 'Foo', 'file' => '@/home/user/test.png');

curl_setopt($ch, CURLOPT_URL, 'http://localhost/upload.php');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

curl_exec($ch);
?>