开发者

Download and save file to database using php

开发者 https://www.devze.com 2023-02-13 07:46 出处:网络
I\'m writing a program using php to download all picture with specified format and save them into my database (MySQL).

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开发者_StackOverflow 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);


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


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);


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()

0

精彩评论

暂无评论...
验证码 换一张
取 消