开发者

Using PHP to gather an image at a specified URL and storing it into a database

开发者 https://www.devze.com 2023-03-11 03:15 出处:网络
Generally, I am looking to input a URL and then import the image at that URL into a database. Here is some code that has me close, but alternatives are welcomed.

Generally, I am looking to input a URL and then import the image at that URL into a database.

Here is some code that has me close, but alternatives are welcomed.

If I try to store $image into a database as a BLOB, it presents an error.

<?php



class wSpider
{
    var $ch; /// going to used to hold our cURL instance
    var $html; /// used to hold resultant html data
    var $binary; /// used for binary transfers
    var $url; /// used to hold the url to be downloaded

    function wSpider()
    {
        $this->html = "http:/";
        $this->binary = 0;
        $this->url = "";
    }


    function fetc开发者_如何转开发hPage($url)
    {
        $this->url = $url;
        if (isset($this->url)) {
            $this->ch = curl_init(); /// open a cURL instance
            curl_setopt ($this->ch, CURLOPT_RETURNTRANSFER, 1); // tell cURL to return the data
            curl_setopt ($this->ch, CURLOPT_URL, $this->url); /// set the URL to download
            curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, true); /// Follow any redirects
            curl_setopt($this->ch, CURLOPT_BINARYTRANSFER, $this->binary); /// tells cURL if the data is binary data or not
            $this->html = curl_exec($this->ch); // pulls the webpage from the internet
            curl_close ($this->ch); /// closes the connection
        }
    }
}

$mySpider = new wSpider(); //// creates a new instance of the wSpider
$mySpider->binary = 1; /// turns on the binary transfer mode
$image = $mySpider->fetchPage("http://static.php.net/www.php.net/images/php.gif");

?>


You don't need all that stuff.

You can do: $image = file_get_contents($url);

And then pdo::prepare("INSERT INTO img (?, ?)");

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号