PHP些个代码把数据库存储的点播视频和阿里云OSS文件批量下载到本机

最近有个业务需求,需要备份数据库的视频文件,写了这个代码,用5T的硬盘拷贝数据库的点播视频文件,保持原先OSS存储的目录结构,跑个本机程序备份到本机(用TP写的PHP代码配置的路径为: http://localhost:8181/api/crontab.video/downloadVod),本机自动执行刷新,跑了2天终于把2T多的文件下载完毕了 ,以下核心代码仅供参考。

 /** 保存点播视频本地
  *  /api/crontab.video/downloadVod
  */

 function downloadVod(){
     set_time_limit(0);
     echo date('Y-m-d H:i:s').'
'; $where = ['cate_id'=>'0','is_audit'=>['in',[0,1]],'ctime'=>['lt',gmtime()-60]]; $video = Model('video')->where($where)->field('video_id,photo,video_url,vod_url')->order('video_id asc')->limit(2)->select(); $path = "./Uploads/video"; $path = "H:/YTS"; if(!is_dir($path)){ @mkdir($path, 0777,true); echo 'ok'; } $new_file = ''; foreach($video as $key=>$val){ $photo = explode('|',$val['photo']); $vod_url = $val['vod_url']; $video_url = $val['video_url']; if ( $vod_url != '') { $vod_url = str_replace('https', 'http', $vod_url); $url = $this->fopen_url($vod_url); $videoPath = parse_url($video_url)['path']; $new_file = $path . dirname($videoPath) . "/"; if (!file_exists($new_file)) { //检查是否有该文件夹,如果没有就创建,并给予最高权限 @mkdir($new_file, 0777, true); } $new_file = $new_file . basename($video_url); //getFileExt($vod_url) file_put_contents($new_file, $url); } $ret = Model('video')->where(array('video_id'=>$val['video_id']))->update(['cate_id'=>1]); if($ret === false){ return '下载保存失败.'; } } echo '

downloadCoverDone:'.$new_file.' | '.M()->getLastSql(); echo '

你可能感兴趣的:(THINKPHP,PHP,php,阿里云,数据库)