标签:set 图片 exist 文件大小 gif url write 保存文件 foreach
<?php
set_time_limit(0);
$file = fopen("index.csv",‘r‘);
$temp = [];
$i=0;
$firstsku=‘‘;
while ($data = fgetcsv($file)) {
// print_r($data
if ($i>2) {
if (empty($data[15])&&empty($data[16])&&empty($data[17])&&empty($data[18])) {
$firstsku = $data[0];
if (!is_dir($data[0])) {
mkdir($data[0]);
}
$arr = explode(‘/‘,$data[1]);
$end = end($arr);
$temp[$data[0]][$end] = $data[1];
}else{
$arr = explode(‘/‘,$data[1]);
$end = end($arr);
$temp[$firstsku][$end] = $data[1];
}
if (!empty($data[3])) {
# code...
$arr = explode(‘/‘,$data[3]);
$end = end($arr);
$temp[$firstsku][$end] = $data[3];
}
}
$i++;
}
// print_r($temp);die;
foreach ($temp as $key => $data) {
foreach ($data as $key1 => $value) {
if (!file_exists($key.‘/‘.$key1)) {
getImage($value,$key,$key1,$type=1);
}
}
// print_r($data);die;
/*if (!file_exists($key.‘/‘.$data)) {
getImage($data[1],$data[0],$end,$type=1);
}*/
/*// if ($key>2&&$key<20) {
if (empty($data[15])&&empty($data[16])&&empty($data[17])&&empty($data[18])) {
if (!is_dir($data[0])) {
mkdir($data[0]);
}
$arr = explode(‘/‘,$data[1]);
$end = end($arr);
if (!file_exists($data[0].‘/‘.$end)) {
getImage($data[1],$data[0],$end,$type=1);
}
}else{
}*/
// }
}
function getImage($url,$save_dir=‘‘,$filename=‘‘,$type=0){
if(trim($url)==‘‘){
return array(‘file_name‘=>‘‘,‘save_path‘=>‘‘,‘error‘=>1);
}
if(trim($save_dir)==‘‘){
$save_dir=‘./‘;
}
if(trim($filename)==‘‘){//保存文件名
$ext=strrchr($url,‘.‘);
if($ext!=‘.gif‘&&$ext!=‘.jpg‘){
return array(‘file_name‘=>‘‘,‘save_path‘=>‘‘,‘error‘=>3);
}
$filename=time().$ext;
}
if(0!==strrpos($save_dir,‘/‘)){
$save_dir.=‘/‘;
}
//创建保存目录
if(!file_exists($save_dir)&&!mkdir($save_dir,0777,true)){
return array(‘file_name‘=>‘‘,‘save_path‘=>‘‘,‘error‘=>5);
}
//获取远程文件所采用的方法
if($type){
$ch=curl_init();
$timeout=300;
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
$img=curl_exec($ch);
curl_close($ch);
}else{
ob_start();
readfile($url);
$img=ob_get_contents();
ob_end_clean();
}
//$size=strlen($img);
//文件大小
$fp2=@fopen($save_dir.$filename,‘a‘);
fwrite($fp2,$img);
fclose($fp2);
unset($img,$url);
return array(‘file_name‘=>$filename,‘save_path‘=>$save_dir.$filename,‘error‘=>0);
}
标签:set 图片 exist 文件大小 gif url write 保存文件 foreach
原文地址:https://www.cnblogs.com/zhaoxiangshang/p/10493807.html