1

I have tried to download images to a directory but now I want to save the images to the database using BLOB type by not using the File Chooser. I mean save the images direct from a URL:

Say:

http://someserver.com/images/imageone.gif

How can I save that directly to database? Do I have to download it first before saving to database? Please help me..

3
  • I think they deserve even votes :) Commented Jun 21, 2012 at 13:03
  • as stated above, i tried downloading the images to a directory which actually works and also tried to insert image to database using the file chooser in which you select the images from your local machine and submit (insert to database, also worked) I wanted to save it automatically straight from a URL..is it possible? Commented Jun 21, 2012 at 13:04
  • @silent_coder14, you mentioned that you succed to insert image to DB using file chooser, how did you do that?. I'm trying to insert image base64 url(which is realy large) using text type but it's not working. Can you help me? Commented Oct 3, 2017 at 8:45

2 Answers 2

4

Like you would save any other data.

$imageContents = file_get_contents('http://someserver.com/images/imageone.gif');
$imageContentsEscaped = mysql_real_escape_string($imageContents, $link);
mysql_query("INSERT INTO table_name (imageData) VALUES ('$imageContentsEscaped')", $link);

Make sure the column you want to save the image data to is set to a binary type such as BLOB.

Sign up to request clarification or add additional context in comments.

Comments

1
// download image to memory
$image = file_get_contents('http://example.com/my/image.jpg');

// open PDO connection
$db = ...;

// insert downloaded data to the DB
$sql = 'insert into my_table(filename, content) values (?,?)';
$stmt = $db->prepare($sql);
$stmt->bindParam(1, 'my/image.jpg');
$stmt->bindParam(2, $image, PDO::PARAM_LOB);
$stmt->execute();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.