使用php-curl上传图像

使用php-curl上传图像,php,curl,image-uploading,Php,Curl,Image Uploading,我们正在努力使用php-curl自动上传图像。请告诉我是否有同样的方法。基本思路 <?php $ch = curl_init(); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_VERBOSE, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_USERAGENT, "M

我们正在努力使用php-curl自动上传图像。请告诉我是否有同样的方法。

基本思路

<?php
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_VERBOSE, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)");
    curl_setopt($ch, CURLOPT_URL, _VIRUS_SCAN_URL);
    curl_setopt($ch, CURLOPT_POST, true);
    // same as <input type="file" name="file_box">
    $post = array(
        "file_box"=>"@/path/to/myfile.jpg",
        "username"=>"foobar",
        "password"=>"secret",
        "submit"=>"submit"
    );
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post); 
    $response = curl_exec($ch);
?>


你可以有更多关于卷发的信息。

你好,拉格兹,谢谢你的回答。但是,我的代码中没有使用数组。我使用如下:$Post=Filefieldname=”“现在我不知道在这些“”中写什么以便上传图像。你应该使用一个数组,这就是curl_setopt的制作方式,如果你不使用数组,你必须自己对文件进行编码。为什么您无法使用数组?当我使用数组时,浏览字段不会自动填充,所以我使用了一行url作为$post=“field\u name=name&password=password&Filefieldname=???&button\u name=Continue”;所以我只是想知道是否可以用上传图片的东西来替换上面的问号。在文件的abs路径中,前导“@”是至关重要的。
<?php

/*
ini_set('display_errors',1);
error_reporting(E_ALL);
*/
include('_db.php');
include('_session.php');




$business_id = $session->business->id;
$error = "";
$output = "";

if ($_FILES["file"]["type"] == "image/gif" || $_FILES["file"]["type"] == "image/jpeg" || $_FILES["file"]["type"] == "image/pjpeg" || $_FILES["file"]["type"] == "image/png")
{
    if ($_FILES["file"]["error"] > 0)
    {
        $error = $_FILES["file"]["error"];
        echo "{error: '". $error ."', msg: ''}";
    }
    else
    {
        //set POST variables
        $url = 'http://img.mySite.com/';

        $fields = array(
                    //assign filetype the file extension
                    'filetype'=>substr(strrchr($_FILES["file"]["name"], '.'), 1),
                    //give the file id a unique id
                    'fileid'=>$business_id . ":" . date('YmdGisu') .":". $_FILES["file"]["name"],
                    //read image data into a string using file get contents
                    'content'=>file_get_contents($_FILES['file']['tmp_name'])
                );

        //open connection
        $ch = curl_init();

        //set the url, number of POST vars, POST data
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_VERBOSE, 0);
        curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)");
        curl_setopt($ch,CURLOPT_URL,$url);
        curl_setopt($ch,CURLOPT_POST,true);
        curl_setopt($ch,CURLOPT_POSTFIELDS,$fields);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);

        //execute post
        $output = curl_exec($ch);
        if($output == false)
            $error = "Fail.";
        echo "{error: '". $error ."', msg: '" . $output . "'}";

        //close connection
        curl_close($ch);
    }
  }
  else
  {
    $error = "Incorrect File Format.";
    echo "{error: '". $error ."', msg: ''}";
  }
mysql_close($link);

?>