Importing Remote Images into DB

Discussion in 'PHP' started by Ian, Feb 25, 2009.

  1. #1
    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.

    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());	
    ?>
    Code (markup):
    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 a cell in my product 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
     
    Ian, Feb 25, 2009 IP
  2. yoavmatchulsky

    yoavmatchulsky Member

    Messages:
    57
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    48
    #2
    what kind of error do you get?

    - string is quoted in SQL using single quotes, not double
    - maybe your server doesn't allow PHP to use remote connections
    - maybe your database isn't set
    - maybe your table has a primary key which isn't auto increment (i.e. you have to fill that field when inserting a new row)
     
    yoavmatchulsky, Feb 25, 2009 IP
  3. Ian

    Ian Well-Known Member

    Messages:
    409
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    125
    #3
    No error. It's just not inserting the image. Just a blank entry in the image cell. The DB connection isn't remote, it's local. It's just not loading the image and then inserting it into the database. So I'm not sure if I used the file_get_contents function correctly.
     
    Ian, Feb 25, 2009 IP
  4. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #4
    May I ask why do it so complicated? Why not just copy the images from the old server to the new (in a directory) and add the file-url to the database? Saves lots of db-space, and pics usually does not load any faster from db than directly from server...
     
    PoPSiCLe, Feb 25, 2009 IP
  5. Ian

    Ian Well-Known Member

    Messages:
    409
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    125
    #5
    I'm doing it to more easily move off the other server instead of ftp'ing, but mostly to also help deal with bandwidth theft, as I don't want to have to get into a mass-rename of images and then changing all the code to reflect it. By doing this all I have to do is change the db connection and the cell of the image call. So far I haven't been able to figure this one out yet...I didn't think it would be so difficult :confused:
     
    Ian, Feb 27, 2009 IP
  6. Ian

    Ian Well-Known Member

    Messages:
    409
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    125
    #6
    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?
     
    Ian, Feb 27, 2009 IP
  7. Ian

    Ian Well-Known Member

    Messages:
    409
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    125
    #7
    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
     
    Ian, Feb 27, 2009 IP