我想将图像文件添加到Codeigniter中的文件夹中。
我使用库DOMXpath从网站获取数据图像。我有功能:getNew()
:获取列表图像。
问题是:
我获得了图片成功的网址。
我想将此图像保存到服务器中的文件夹,其中包含文件夹的自定义名称。 不依赖URL是一个爬虫。
例:
之前:http://example.com/2017-3-22/picture.png
它应该使用如下名称保存到我的服务器中:
之后:http://my-website.com/upload/newimg/picture.png
我的代码:
function getNew(){
$xpath_thumb = //*[@id='news_home']/li/div/div[1]/a/img;
$cateWebsite = 'http://vnexpress.net/tin-tuc/thoi-su';
$doc = new DOMDocument();
$internalErrors = libxml_use_internal_errors(true);
$loadHtml = $doc->loadHTMLfile($cateWebsite);
if($loadHtml){
$xpath = new DOMXpath($doc);
$thumb = $xpath->query($xpath_thumb);
$result = array();
for ($i=0; $i < $title->length; $i++) {
$result[$i]['thumb']= $thumb[$i]->getAttribute('src');
}
}
}
由于
答案 0 :(得分:0)
您可以使用file_put_contents
和file_get_contents
保存:
function save($url) {
$nameArr = explode("/", $url);
$img = '/my/folder/'.$nameArr[count($nameArr) - 1];
file_put_contents($img, file_get_contents($url));
}
卷曲
function save($url) {
$nameArr = explode("/", $url);
$ch = curl_init($url);
$fp = fopen('/PATH/TO/YOUR_FOLDER/'.$nameArr[count($nameArr) - 1], 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
}