从文件夹中随机获取一张图片上传到附件并删除
本方案采用的是php语言,操作步骤如下:
1、从文件夹中随机抽取一张图片
$url=$zbp->path."imgs/"; $img_array=glob($url."*.jpg",GLOB_BRACE); $num=array_rand($img_array); $filename=$img_array[$num]; echo$filename;
2、制作转移函数
functiontc_imgssize_getSaveFileDir($times,$fileName)
{
global$zbp;
$fileDirPath=$zbp->usersdir.'upload/'.date('Y',$times).'/'.date('m',$times);
if(!file_exists($fileDirPath)){
@mkdir($fileDirPath,0755,true);
}
return$fileDirPath."/".$fileName;
}3、上传到附件
$va1=str_replace($url,"",$fileName);
$vc=explode('.',$va1);
$suffix=$vc[1];
$size=filesize($fileName);
$times=time();//固定时间
$fileName=date("YmdHis",$times).$times.rand(10000,99999).'.'.$suffix;
$fileSavePath=tc_imgssize_getSaveFileDir($times,$fileName);
$upload=new\Upload();
$upload->Name=$fileName;
$upload->SourceName=$fileName;
$upload->MimeType="image/".$suffix;
$upload->Size=$size;
$upload->AuthorID=1;
rename($va,$fileSavePath);
$filepath=$fileSavePath;
$upload->Save();4、获取新的图片地址
$imgurl=$zbp->host.'zb_users/upload/'.date('Y',$times).'/'.date('m',$times)."/".$fileName;
echo$imgurl;



