undone
undone

Reputation: 7888

Download and save file to database using php

I'm writing a program using php to download all picture with specified format and save them into my database (MySQL).

I'm using cURL but I can not get file (only send it to user browser).

Is there any another function or class that I can use to save file to database?

$ch=curl_init($_REQUEST["URL"]);
header("Content-type:" . curl_getinfo($ch,CURLINFO_CONTENT_TYPE));
$txt=curl_exec($ch);

Upvotes: 0

Views: 1373

Answers (3)

Zeta Two
Zeta Two

Reputation: 1806

You have to call curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE). This will make curl_exec return the file as astring instead of outputting it. This way you can save it to the database.

Read more: http://www.php.net/manual/en/function.curl-setopt.php

Upvotes: 2

TheDeveloper
TheDeveloper

Reputation: 422

Sounds like you need to use the option CURLOPT_RETURNTRANSFER:

 $ch=curl_init($_REQUEST["URL"]);
curl_setopt($ch, CURLOPT_RETURNSTRANSFER,true);
$txt=curl_exec($ch);

You could also use file_get_contents()

Upvotes: 1

AndreKR
AndreKR

Reputation: 33697

To get the downloaded file's content you need to set a curl option before you call curl_exec():

curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);

Upvotes: 2

Related Questions