使用 php 上传图像-curl

发布于 2024-08-10 23:10:44 字数 53 浏览 3 评论 0原文

我们正在努力使用 php-curl 自动上传图像。请告诉我是否有任何方法可以做到同样的事情。

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我的黑色迷你裙 2024-08-17 23:10:44

您可以在此处了解有关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);
?>

the basic idea

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

you can have more info about curl here.

泪眸﹌ 2024-08-17 23:10:44
<?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);

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

?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文