Jump to content

[SOLVED] Image Import Into Database


patsfans

Recommended Posts

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.  Is there a script or code that will 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.

 

Thanks in advance for your help! - Ian

Link to comment
Share on other sites

yeah just write one.

 

 

Use glob to get all the file names in the folder

 

use a foreach for each file name

 

in the foreach loop use file_get_contents to read the image and then insert said data into the database

 

That's what I was thinking, but don't the images need to be loaded into a temp folder first before they're inserted? - Ian

Link to comment
Share on other sites

That's what I was thinking, but don't the images need to be loaded into a temp folder first before they're inserted? - Ian

 

If they are uploaded, they are already stored in a temp folder. If they are on the server, no. You can just read them straight from where they are.

Link to comment
Share on other sites

Not if the remote server is accessible by this server you can use file_get_contents on an image because even if its remote it will still push the content untainted (unlike a processed document like a php document)

 

php.net/file_get_contents

 

 

read that see what it returns

 

 

No need to temp because they are already stored, temping is only important if you are doing a file upload via a POST form

Link to comment
Share on other sites

I looked into this further, but am still having an issue. 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.

Link to comment
Share on other sites

I looked into this further, but am still having an issue.   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 I'm trying to do is run the script where it imports the images into a longblob cell and then I'll be deleting the images off the other server.  Hopefully this makes sense. - Ian

Link to comment
Share on other sites

Well, it appears I've figured it out (at least the problem anyway).  If the URL is not on the local server, it won't load the image (it loaded it via a URL that was to a file on the same domain).  Is there a different command I need to use, or a function in .htaccess to tell the server to load remote files?

Link to comment
Share on other sites

Thanks to cooldude832 who helped me troubleshoot this.  I have a friend at the server company and come to find out it was a firewall issue.  The added the domain to the firewall white list and it worked perfectly.

 

Thank you to everyone for taking the time to try and help, I appreciate it.  And I hope you all have a good weekend! - Ian

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.