file_get_contents采集加入库

来源:互联网 发布:制作二维动漫软件 编辑:程序博客网 时间:2024/05/17 06:50

//demo.php

<?php
header('content-type:text/html;charset=utf8');
//设置路径
$url="http://www.sina.com.cn/";
//echo $url;die;
//获取文件路径
$str=file_get_contents($url);
//echo $str;die;
//正则表达式匹配
$reg='#<div class="ft-list">.*</div>#isU';
preg_match($reg,$str,$re);
//print_r($re);exit;
$reg1='#<img src="(.*)" data-src="(.*)" width=".*" height=".*" alt="(.*)">#isU';
preg_match_all($reg1,$re[0],$res);
print_r($res);exit;
//连接数据库
mysql_connect('127.0.0.1','root','root');
//选择数据库
mysql_select_db('caiji');
//设置字符集
mysql_query('set names utf8');
//把获取到的图片保存到当前文件夹下
foreach($res[2] as $v){
    //获取图片
    $pathinfo=file_get_contents($v);
    //返回文件路径信息
    $data=pathinfo($v);
    //获取文件名
    $url="./images/".time().rand(1,10000).'.'.$data['extension'];
    $sql="insert into images VALUES (null,'$url')";
    $res=mysql_query($sql);
    //把文件放入指定的文件夹内
    file_put_contents($url,$pathinfo);
}
if($res){
    echo 1;
}
else{
    echo 0;
}


0 0
原创粉丝点击