I have a site where all the images are currently in a directory on another server (I did it at the time to conserve bandwidth), and am now looking to write a script that will import them from there directly into a database on a dedicated server as I'm getting rid of the other. I'm looking to write a script to load them from that server via the URL, and then insert it into my MYSQL database as a longblob file? I'm just trying to find an efficient way to make the move, and thought that this would be the best way to handle it.
Here's what I have:
Code:
<?php
$photo = "http://www.example.com/images/picture.jpg";
$image = file_get_contents($photo, FILE_BINARY);
$dbname = "database";
$connection = @mysql_connect("localhost", "user", "pass") or die("Couldn't Connect.");
$db = @mysql_select_db($dbname, $connection) or die("Couldn't Select Database.");
$table = "images";
$query = sprintf("INSERT INTO $table ('image') VALUES ('%s')",
mysql_real_escape_string($image, $connection));
mysql_query($query, $connection);
?>
I can't get it to load the image, and then insert it into the LONGBLOB cell. I can't find another example of this online or anything similar, and the file_get_contents reference on php.net obviously doesn't explain this. So thank you for your help so far, and I'm sure this is probably easier than I'm making it out to be. The problem is that there is a row being inserted, but the blob has 0 Bytes. So is the above the correct function to load the image before it's inserted, or is the problem that I'm using the wrong syntax for this function?
Thank you in advance for your assistance. - Ian
|