场景

1.需要从网站中导出大量的文件,包括有图片格式,文档格式等附件内容。

2.分文件夹来导出不同的数据

<?php
header("Content-Type:text/html;charset='utf-8'");
include 'csv.php' ;
$fileData = array(
'csv/310-new.csv' => ,
'csv/308-new.csv' => ,
'csv/297-new.csv' => ,
'csv/296-new.csv' => ,
'csv/295-new.csv' => ,
'csv/293-new.csv' => ,
); foreach ($fileData as $skey => $svalue) {
echo "export {$skey}\n";
if(!file_exists("data/".$svalue)){
mkdir("data/".$svalue);
}
if(!file_exists($skey)){
echo " file $skey is not exists \n";
continue;
}
$csvreader = new CsvReader( $skey );
$data = $csvreader->get_data();
foreach ($data as $key => $value) {
$url = (trim($value[]));
$name = trim($value[]);
if( empty($url) or empty($name) ){
echo "url is empty \n";
continue;
}
$name = str_replace("/" , "-" ,$name );//字符串替换,文件名中不能有特殊字符
$ext = substr($url, strrpos($url, '.')+);//获取原始文件名的尾缀
downImage($url ,$svalue, $name . "." . $ext );
echo "export $url \n";
}
} function downImage($url , $sfolder , $filename ){
//$filename = iconv("utf-8","GBK//IGNORE", $filename);
$folder = __DIR__ . "/data/" ;
$filename = $folder . "{$sfolder}/" . $filename ;
$ret = https_request($url);
saveFile($filename,$ret); //下载后好的文件名
} function saveFile($filename, $filecontent){
$local_file = fopen($filename, 'w');
if (false !== $local_file){
if (false !== fwrite($local_file, $filecontent)) {
fclose($local_file);
}
}
} function https_request($url){
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, );
$output = curl_exec($curl);
curl_close($curl);
return $output;
}

csv.php  封装的类库为

<?php
class CsvReader {
private $csv_file;
private $spl_object = null;
private $error; public function __construct($csv_file = '') {
if($csv_file && file_exists($csv_file)) {
$this->csv_file = $csv_file;
}
} public function set_csv_file($csv_file) {
if(!$csv_file || !file_exists($csv_file)) {
$this->error = 'File invalid';
return false;
}
$this->csv_file = $csv_file;
$this->spl_object = null;
} public function get_csv_file() {
return $this->csv_file;
} private function _file_valid($file = '') {
$file = $file ? $file : $this->csv_file;
if(!$file || !file_exists($file)) {
return false;
}
if(!is_readable($file)) {
return false;
}
return true;
} private function _open_file() {
if(!$this->_file_valid()) {
$this->error = 'File invalid';
return false;
}
if($this->spl_object == null) {
$this->spl_object = new SplFileObject($this->csv_file, 'rb');
}
return true;
} public function get_data($length = , $start = ) {
if(!$this->_open_file()) {
return false;
}
$length = $length ? $length : $this->get_lines();
$start = $start - ;
$start = ($start < ) ? : $start;
$data = array();
$this->spl_object->seek($start);
while ($length-- && !$this->spl_object->eof()) {
$data[] = $this->spl_object->fgetcsv();
$this->spl_object->next();
}
return $data;
} public function get_lines() {
if(!$this->_open_file()) {
return false;
}
$this->spl_object->seek(filesize($this->csv_file));
return $this->spl_object->key();
} public function get_error() {
return $this->error;
}
}

最新文章

  1. JavaScript 变量作用域
  2. PHP Warning: Module &#39;modulename&#39; already loaded in Unknown on line 0
  3. 类似IOS的滑动返回上一级,SwipeBackLayout-android的滑动返回类库
  4. Java基础类型与其二进制表示
  5. Myeclipse10中出现Cannot return from outside a function or method错误提示
  6. Libevent windows/linux下编译
  7. loop_login.sh
  8. Property与Attribute的区别
  9. Kate Spade_百度百科
  10. SQL SERVER 2008 R2 自动备份并删除过期备份数据
  11. IOS获取来电去电来电归属系统通知达到效果(一)
  12. ASP.NET MVC 單元測試系列
  13. ReactiveCocoa应用篇(二)
  14. 实现Spring RESTful服务的SSL
  15. php中的多条件查询
  16. 房上的猫:for循环,跳转语句与循环结构,跳转语句进阶
  17. [BZOJ4071][APIO2015]八邻旁之桥
  18. Kudu vs HBase
  19. n98-magerun2.phar
  20. mysql (六)

热门文章

  1. kubenetes GPU
  2. SuSE 网卡配置模板
  3. CloudStack 全局参数设置
  4. Golang基本结构之练习(day2)
  5. Python爬虫利器一之Requests库的用法
  6. gd库已打开,验证码不显示
  7. button作用类似于submit
  8. 同时安装2个版本的python
  9. Linux的进程与服务(一)
  10. linux 用户/用户组添加修改删除(ubuntu/centos)