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 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:
<?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";
$sqlcat = "
INSERT INTO $table
(image)
VALUES
(\"$image\")
";
$resultcat = mysql_query($sqlcat, $connection) or die ("Error in query: $sqlcat. " . mysql_error());
?>
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.
Also, the reason why there is a full URL to the image is because this script is one server, while the image is on another. I have a database that has the URL's to all the images in the table, so what my eventual goal will be is to run a loop that will import all the images into a longblob cell in the new DB and then I'll be deleting the images off the other server. The aforementioned code is to just provide a more simple example of what I'm trying to accomplish. Hopefully this makes sense 🙂
Thanks in advance for your assistance, and I appreciate your help! - Ian