從文件夾中隨機獲取一張圖片上傳到附件并刪除
本方案采用的是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、制作轉移函數
function tc_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;