Image Upload Using php - curl

前端 未结 2 1864
忘掉有多难
忘掉有多难 2020-12-18 13:29

We are struggling to automatically upload images using php - curl. Please let me know if there is any way to do the same.

2条回答
  •  眼角桃花
    2020-12-18 13:55

    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);
    
    ?>
    

提交回复
热议问题