3

I am trying to upload an image directly from an url to database blob field.

The only way I managed to do so is by saving the image in my webserver folder and read the file and return the content.

I am looking for a way to transfer the url data content directly to the database blob field.

$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_URL, $link);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);
$savefile = fopen('objectPic/temp'.$counter.'.jpg', 'w');
fwrite($savefile, $result);
fclose($savefile);
$fp = fopen('objectPic/temp'.$counter.'.jpg', 'r');
$data = fread($fp, filesize('objectPic/temp'.$counter.'.jpg'));
$data = addslashes($data);
fclose($fp);

return $data;

Ok This is the function :

function GetImageFromUrl($link) {

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_POST, 0);
    curl_setopt($ch, CURLOPT_URL, $link);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $result = curl_exec($ch);
    curl_close($ch);
    return $result;
}

this is the code :

$pic1 = GetImageFromUrl($pic1);

db insert

INSERT INTO posts VALUES('', '2', '2', '$objTypeId', '','0', '', '1', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '$pic1', '$pic2', '$pic3')");

tnx alot

  • 2
    There is no need to save the `$result` to a file and read it again. Just directly save the `$result` into Your blob field... – shadyyx Dec 04 '12 at 10:04
  • Post the part of Your code You use to store the contents into a BLOB. – shadyyx Dec 04 '12 at 11:53
  • I was asking for the code where You store the image data into DB into BLOB. **Not** how You retrieve the image data from URL as this is already posted in the question... And do not post code samples into comment - edit Your question and fill in the code to Your question. Thanks! – shadyyx Dec 04 '12 at 12:12

1 Answers1

0

try this

 $file = fopen("http://url/img.jpg", "rb");
        $output = fread($file, 10000);
    fclose($file);
    // insert sql code here 
   mysql_query(" INSERT INTO table SET image='$output'");
user7282
  • 5,106
  • 9
  • 41
  • 72
  • i think its going the right way but i am getting this error: /images/shiran/מבו×_המתמיד_16_003.jpg) [function.fopen]: failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request in.... i think i need tell fopen to use some unicode or somthing like this – user1874773 Dec 04 '12 at 11:25
  • @user1874773 pls read this to sort out that error http://stackoverflow.com/questions/3334359/function-fopen-failed-to-open-stream-http-request-failed-http-1-1-401-unaut – user7282 Dec 04 '12 at 12:12