patsfans Posted February 24, 2009 Share Posted February 24, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/146699-solved-image-import-into-database/ Share on other sites More sharing options...
cooldude832 Posted February 24, 2009 Share Posted February 24, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/146699-solved-image-import-into-database/#findComment-770184 Share on other sites More sharing options...
patsfans Posted February 24, 2009 Author Share Posted February 24, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/146699-solved-image-import-into-database/#findComment-770190 Share on other sites More sharing options...
premiso Posted February 24, 2009 Share Posted February 24, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/146699-solved-image-import-into-database/#findComment-770194 Share on other sites More sharing options...
cooldude832 Posted February 24, 2009 Share Posted February 24, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/146699-solved-image-import-into-database/#findComment-770195 Share on other sites More sharing options...
patsfans Posted February 24, 2009 Author Share Posted February 24, 2009 I tried a basic script a little while ago, but it's returning a broken image: with no value. This came after noticing no blob was going into the database: <? $photo = file_get_contents('http://www.mysite/images/myimage.jpg'); header('Content-type: image/jpeg;'); echo "<IMG SRC=\"$photo\">"; ?> What am I missing? - Ian Quote Link to comment https://forums.phpfreaks.com/topic/146699-solved-image-import-into-database/#findComment-770508 Share on other sites More sharing options...
cooldude832 Posted February 24, 2009 Share Posted February 24, 2009 cause you can't do it that way you are telling it to find the image source in the image files content. read what file_get_contents does Quote Link to comment https://forums.phpfreaks.com/topic/146699-solved-image-import-into-database/#findComment-770515 Share on other sites More sharing options...
patsfans Posted February 25, 2009 Author Share Posted February 25, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/146699-solved-image-import-into-database/#findComment-771043 Share on other sites More sharing options...
patsfans Posted February 25, 2009 Author Share Posted February 25, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/146699-solved-image-import-into-database/#findComment-771145 Share on other sites More sharing options...
patsfans Posted February 25, 2009 Author Share Posted February 25, 2009 I just heard from someone else that I need to escape the binary data in the query string; use mysql_real_escape_string(), but I'm not having any luck Quote Link to comment https://forums.phpfreaks.com/topic/146699-solved-image-import-into-database/#findComment-771290 Share on other sites More sharing options...
patsfans Posted February 27, 2009 Author Share Posted February 27, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/146699-solved-image-import-into-database/#findComment-772585 Share on other sites More sharing options...
patsfans Posted February 27, 2009 Author Share Posted February 27, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/146699-solved-image-import-into-database/#findComment-772827 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.