簡體   English   中英

圖像上傳使用php - curl

[英]Image Upload Using php - curl

我們正在努力使用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);
?>

你可以在這里獲得更多關於卷曲的信息

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

?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM