Re: duplicate image
| From: | Ashley Sheridan | Date: | Sun, 15 Feb 2015 10:42:31 +0000 |
| Subject: | Re: duplicate image | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-324455@lists.php.net to get a copy of this message | ||
On 15 February 2015 10:24:23 GMT+00:00, hadi <almarzuki2011@hotmail.com> wrote:
>Hi,
>
>I have script which download rssfeed from the internet. But
>unfortunately it
>keep downloading duplicate image to the database.
>
>Here is my script
>
><?php
>require 'database.php';
>
>$url = "http://www.albaldnews.com/rss.php?cat=24";
>$rss = simplexml_load_file($url);
>
>if($rss)
>{
>echo '<h1>'.$rss->channel->title.'</h1>';
>echo '<li>'.$rss->channel->pubDate.'</li>';
>$items = $rss->channel->item;
>foreach($items as $item)
>{
>
>$title = $item->title;
>$link = $item->link;
>$published_on = $item->pubDate;
>$description = $item->description;
>$category = $item->category;
>$guid = $item->guid;
>$enclosure = $item->enclosure[0]['url'];
>
>
>$ch = curl_init ("$item->enclosure");
>curl_setopt($ch, CURLOPT_HEADER, 0);
>curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
>curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
>$rawdata=curl_exec ($ch);
>curl_close ($ch);
>
>
>mysqli_real_escape_string($conn,$item->title);
>mysqli_real_escape_string($conn,$item->link);
>mysqli_real_escape_string($conn,$item->pubDate);
>mysqli_real_escape_string($conn,$item->description);
>mysqli_real_escape_string($conn,$item->category);
>$img=mysqli_real_escape_string($conn,$rawdata);
>
>$sql = "INSERT INTO feedtable
>(title,link,pubdate,description,category,image)VALUES
>('$item->title','$item->link','$item->pubDate','$item->description','$item->
>category','$img')";
>$result = mysqli_query($conn, $sql);
>
>if ($conn->query($sql) === TRUE) {
> echo "New record created successfully\n";
>}
>else
>{
> echo "Error:\n " . $sql . "<br>" . $conn->error;
>}
>
>}
>}
>
>?>
You need to either set a unique index on that field in the DB and deal with the warning (either at
an error level or with an SQL construct like INSERT ... ON DUPLICATE KEY UPDATE) or query that table
first to see if what you're inputting is unique.
As you're doing this with binary data, I would recommended generating a hash of the image and
comparing that, as MySQL might have problems doing comparisons of such large objects, and it
won't be quick.
Thanks,
Ash