日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

基于PHP的定时交作业系统

發布時間:2023/12/18 php 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 基于PHP的定时交作业系统 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

前言

????最近有個需求,就是宿舍的學委要去飛去銀川參加程序設計國際區域賽了。由于比賽期間不能帶電腦,身為宿友的我看他因收作業沒時間收而感到有一點點同情,肯定不是因為他要給我帶特產我才寫的!于是在兩天的有空的時間余下給他寫了個交作業系統。PHP實現在指定時間打包文件夾以及將其作為附件發送到指定郵箱,此項目技術棧沒有用任何架構。純屬基礎。

  • 文章目錄

    • 前言
      • 實現的功能
      • 重要功能的實現方法
          • 發送郵件
          • 時間精確判斷
          • 壓縮文件
          • 任務執行文件
          • 文件夾操作
      • 實現原理
      • 準備工作
      • 步驟
      • 實戰: 效果截圖
      • 演示視頻:
      • 總結
            • 偶爾遇到的坑
            • 感慨

實現的功能

????同學可以上傳作業文件,后臺接收發起者指定類型和大小范圍內的文件并將其放在生成的作業鏈接【這里采用md5校驗】文件夾下,發起者可以設置交作業的開始時間和結束時間,作業標題和格式,各種消息通知,設置是否時間結束自動發送還是認為發送,下載打包文件,可以自由添加學生名單,時間一結束將系統自動打包作業文件夾并命名好指定名字將其發送到指定目標郵件。

重要功能的實現方法

  • 發送郵件
  • 時間精確判斷
  • 壓縮文件
  • 執行任務文件
  • 文件以及文件夾操作
  • 公眾號登入,提交多次

發送郵件

1.打開qq郵箱。點擊設置【這里我使用的是qq郵箱,你也可以使用163】

2.點擊賬戶

3.點擊開啟smtp服務
4.獲取授權碼、【沒有這個系統沒有權限使用你的郵箱發送郵件】

5.重點
你需要了解到 php最多人用的發送email的sdk是什么。并不是自帶的核心email函數。而是PHPMailer這個神奇的功能api包。點擊到Github下載
使用方法:
將 PHPMailer-master項目文件夾放至指定位置,然后通過引用的方式進行調用
這里我放在了項目根目錄下。

代碼:

<?phperror_reporting(0);if(!$_POST['email']){ //發送目標郵箱echo('未接收到email');return ;}if(!$_POST['code']){//前后端驗證echo('沒有驗證碼');return ;}use PHPMailer\PHPMailer\PHPMailer;use PHPMailer\PHPMailer\Exception;require '../PHPMailer-master/src/Exception.php'; //目錄,我放到根目錄,所以要退回前一級目錄 ../require '../PHPMailer-master/src/PHPMailer.php';require '../PHPMailer-master/src/SMTP.php';$mail = new PHPMailer(true); //學過java的就知道,這是干啥new一個對象,然后下面可以調用對象的方法try {//服務器配置$mail->CharSet ="UTF-8"; //設定郵件編碼,一般是UTF-8$mail->SMTPDebug = 0; $mail->isSMTP(); // 使用SMTP 如果沒有開啟,那么會發送失敗$mail->Host = 'smtp.qq.com'; // SMTP服務器 我在這里實驗的是smtp.qq.com//你也可以 查找自己對應的郵件服務器。 win+R 輸入cmd 找到對應ip$mail->SMTPAuth = true; $mail->Username = '12345678910@qq.com'; // SMTP 用戶名 即郵箱的用戶名 這里可以用數據庫查詢是否用自己的郵箱去發送給老師$mail->Password = '這里是上面步驟獲取的授權碼'; // SMTP 密碼 $mail->SMTPSecure = 'ssl'; $mail->Port = 465; $mail->setFrom('12345678901@qq.com', '發起者名稱'); //發件人 這里可以用數據庫查詢是否用自己的郵箱去發送給老師$mail->addAddress($_POST['email'],"用戶".$_POST['email']); // 收件人//發送附件 這里發送作業$mail->addAttachment('../xy.zip'); // 添加附件$mail->Subject = 'CharlinHeng【'.$_POST['code'].'】' . date("Y-m-d H:i:s",time());$text="這里放置 未交學生名單 "; //這里可以定義 未交學生名單,計算學生名單的 算法下面給出。$mail->Body = '<p>'.$text.'</p>' . date('Y-m-d H:i:s');$mail->AltBody = $text;$mail->send();echo '郵件發送成功';} catch (Exception $e) {echo '郵件發送失敗: ', $mail->ErrorInfo; }

時間精確判斷

????很簡單的算法,對于執行時間判斷條件,需要用當前時間的毫秒數,從格林威治時間到現在的毫秒數。去比較交作業開始時間和交作業結束時間,去減,如果當前時間減去開始時間大于0,那么已經開始,計算當前時間減去結束時間,如果大于0,那么已結束,否則取差的絕對值,然后將毫秒轉換成普通用戶看的標準的日期時間格式,距離結束還有XX。如果當前時間減去開始時間小于0,那么沒有開始,取差的絕對值,后將毫秒轉換成普通用戶看的標準的日期時間格式,距離開始還有XX。

核心計算函數:strtotime()//轉化為格林威治毫秒

代碼

$statue = 0;//$data[6]就是 開始時間或者結束時間.可以換$T = strtotime($data[6]);$all = floor((time()-$T));if($all>0){$statue = 0;echo('已結束!');}else{$T = strtotime($data[5]);$all = floor((time()-$T));if($all>0){echo('已經開始!剩余時間:');$statue = 1;$T = strtotime($data[6]);$all = floor(($T-time())); }else{$statue = 0;echo "未開始:倒計時";}$all = abs($all);// $dS = $all;// $dY = floor($all);// $dM = 0;$dD = floor($all/(3600*24));$all = $all-floor($all/60/60/24);$dH = floor($all/3600);$all = $all-floor($all/60/24);$dM = floor($all/60);$all = $all-floor($all/60);$dS = $all;//年月日時分秒////$all 得到的是多少分鐘以前 if ($dD>0){$finally_time = ($dD).'天';}else if($dH>0){$finally_time = $dH.'小時';}else if($dM>0){$finally_time = $dM.'分鐘';}else{$finally_time = $dS.'秒';}echo $finally_time;}

任務執行判斷也是同理,這里不做敘述。

壓縮文件

壓縮文件夾,打包成zip的方式,如果下面的步驟你做完了,但是還是不成功,你可以去找到php.ini文件,開啟支持擴展方式,即可以在文尾加上一句話。

extension=php_zip.dll

核心代碼:

error_reporting(0);ob_start();if(!$_GET['md5']){echo "沒有定義目錄";return ;}echo "打包中...<br>";$dataPath="receiveHomework/homeworks/".$_GET['md5']."/";//BUG$fileName = "17創新實驗班實驗1.zip";// 自定義打包 文件名命名 可以從數據庫讀取$fileName = iconv('utf-8','gb2312',$fileName);$fileName = $dataPath.$fileName;if(!file_exists($fileName)){//重新生成文件$zip = new ZipArchive();//php內置打包類if ($zip->open($fileName, ZIPARCHIVE::CREATE)!==TRUE) {exit('無法打開文件,或者文件創建失敗');}$datalist=list_dir($tmpPath);foreach($datalist as $val){if(file_exists($val)){$zip->addFile($val, str_replace($tmpPath, '', $val));}}$zip->close();}echo "<br>";//獲取上傳學生作業的文件列表$count = 0;function list_dir($dir){$result = array();if (is_dir($dir)){$file_dir = scandir($dir);foreach($file_dir as $file){if ($file == '.' || $file == '..'){continue;}elseif (is_dir($dir.$file)){$result = array_merge($result, list_dir($dir.$file.'/'));}else{array_push($result, $dir.$file);$count++;echo "正在打包第".$count." 份作業....<br>"; //這里,可能有多個作業,需要展示給前端頁面看具體打包過程。}}}return $result;}echo $_GET['md5']."<br>打包成功!";sleep(1);//打包后,發送郵件,匯報情況if($_GET['send']){header("location:send-time-Mail.php?x=".$_GET['x']);return ;}header("location:downloadFiles.php?md5=".$_GET['md5']."&filename=".$urlpara);

任務執行文件

這里用啥方法都行,本人在這里定義用了XMLHttpRequest對象,方便使用FormData。

$(document).ready(function(){});function judge(){if(window.XMLHttpRequest){var request = new XMLHttpRequest();request.onreadystatechange=function(){if(request.readyState==4&&request.status==200){$('#div').prop('scrollTop',document.getElementById("div").scrollHeight);document.write(request.responseText);document.clear;if(request.responseText.indexOf("成功")!=-1){//notices sender sned ok//pirnt all each type data}}};request.open("GET",'handle_system_auto.php',true);request.send();}}window.setInterval(function (){judge();},8000);judge();

文件夾操作

1.php移動指定文件夾下的tmp臨時文件

$success = move_uploaded_file($file_tmp,$path.$name);

2.刪除指定目錄下的文件

unlink("./".$path.$name_edata);

3.生成空文件夾

$dir = iconv("UTF-8", "GBK", $path2); if (!file_exists($dir)){mkdir ($dir,0777,true);//個人感覺挺多函數都跟系統命令差不多語法。echo "新建文件夾成功!";}

4.獲取前端js FormData的post過來的file

$file = $_FILES['file'];$fileName = $_FILES['file']['name'];$file_tmp = $_FILES['file']['tmp_name'];

實現原理

  • 首先,需要先定義好,交作業開始和結束時間,格式,標題,說明,將這些存到數據庫指定的表,并隨機生成一個md5作業鏈接。
  • 交作業的同學只能交一次,即含有該名字的文件只能上傳一次,交了的同學對應表中有記錄。
  • 判斷類型,后臺只接收數據庫存的發起者設置的接收的文件類型和大小范圍。
  • 將上傳的文件存到對應作業鏈接文件夾,如果文件夾不存在,則創建一個。
  • 寫一個定時執行任務html文件和檢測執行php文件,檢查數據庫字段 狀態為0,即沒有完成的作業的結束時間,如果時間已到,而且發起者規定了自動發送,那么尋找目標郵箱,對文件進行打包,作為附件,發送。否則提醒發起者,作業已結束。
  • 發送之后郵箱通知發起者結果。
  • 任務文件基本運行流程圖:
  • 執行文件符合時間和要求打包結果打印結果打包成功,不發送打包成功自動發送少于3次發送失敗,自環返回結果返回結果發送結果定時執行html文件php文件判斷打包目標文件夾提醒發起者發送目標郵件

    準備工作

  • 郵件發送者的郵箱需要開啟smtp/pop3服務和獲得授權碼。
  • 服務器或者云主機一臺。【用自己電腦也可,但是就是需要一天到晚掛瀏覽器運行】
  • 注:服務器和云主機方面,如果想了解如何快速獲得一臺免費云主機免費服務器教程,1小時內即可讓別人訪問你的網站。可以留個言,本人會另外寫一篇關于1小時即可擁有自己的網站博客文章,免備案系列。

    步驟

    1.根據需求,設計作業的表。

    作業表 對應的SQL語句如下

    CREATE TABLE `homeWork` ( `x` int(11) NOT NULL AUTO_INCREMENT , `userid` int(11) NULL DEFAULT NULL , `path` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL , `title` text(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL , `format_` tinytext CHARACTER SET utf8 COLLATE utf8_general_ci NULL , `starttime` tinytext CHARACTER SET utf8 COLLATE utf8_general_ci NULL , `endtime` tinytext CHARACTER SET utf8 COLLATE utf8_general_ci NULL , `auto_sendemail` text CHARACTER SET utf8 COLLATE utf8_general_ci NULL , `email` tinytext CHARACTER SET utf8 COLLATE utf8_general_ci NULL , `use_you_email` text CHARACTER SET utf8 COLLATE utf8_general_ci NULL , `youemail` tinytext CHARACTER SET utf8 COLLATE utf8_general_ci NULL , `studentnum` int NULL DEFAULT NULL , `noticesyou` text CHARACTER SET utf8 COLLATE utf8_general_ci NULL , `student` text CHARACTER SET utf8 COLLATE utf8_general_ci NULL , `buildtime` char(25) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL , `statue` int(11) NULL DEFAULT NULL , PRIMARY KEY (`x`) ) ENGINE=InnoDB DEFAULT CHARACTER SET=utf8 COLLATE=utf8_general_ci AUTO_INCREMENT=29 ROW_FORMAT=DYNAMIC ;

    對于交了作業的同學,還需要記錄到另外一個表里。

    CREATE TABLE `receiveStudentFile` ( `x` int(11) NOT NULL AUTO_INCREMENT , `md5FileName` char(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL , `studentName` text CHARACTER SET utf8 COLLATE utf8_general_ci NULL , `filename` text CHARACTER SET utf8 COLLATE utf8_general_ci NULL , `uploadcount` int(11) NULL DEFAULT NULL , `uploadtime` char(25) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL , `statue` int(11) NULL DEFAULT NULL , PRIMARY KEY (`x`) ) ENGINE=InnoDB DEFAULT CHARACTER SET=utf8 COLLATE=utf8_general_ci AUTO_INCREMENT=64 ROW_FORMAT=DYNAMIC ;

    2.設計前端頁面
    ? ? ? ? 發起者的 設計新建作業、查看我的收作業、作業管理、交作業頁面。這里比較簡單,但比較花費時間,不做敘述。
    3.功能編寫
    ? ? ? ?對于交作業的homework頁面,需要加載作業詳情。同時還應前后端判斷作業在改時間是否可上傳。 注:[考慮到開發只有2天以及使用用戶基數,所以沒采用前后端分離,采用前后端內嵌式]。
    交作業homework頁面的代碼如下:

    <!doctype html> <html> <head> <meta charset="utf-8"> <title>作業</title> <link rel="stylesheet" type="text/css" href="../css/tooltip_css/tooltip.css"> <link rel="stylesheet" type="text/css" href="../css/homework.css"> <script src="../js/tooltipNeed/tooltip_1_js.js"></script> <script src="../js/tooltipNeed/tooltip_2_js.js"></script> </head> <?phperror_reporting(0);if(!$_GET['md5code']){echo('<a style="color:red;position:relative;top:20vw;margin:0 auto;justify-content: center;display:flex;">請輸入正確的網頁鏈接!</a>');return ;}if(!$_COOKIE['counts']){setcookie('counts',0,time()+3600*24*30); }$md5 = $_GET['md5code'];require_once('connect.php');$query_is_exist = "select * from homework where path='".$md5."'";$result = mysqli_query($connect_to_database,$query_is_exist);$this_result = mysqli_fetch_array($result);if($this_result[0]==""){echo('<a style="color:red;position:relative;top:20vw;margin:0 auto;justify-content: center;display:flex;">該作業詳情不存在!</a>');return ;}else{$data = $this_result;}?> <body><div class="main"><div class="nav"><ul class="inn"><li></li><li style="font-size: 11px;"><?php $q = "select count(*) from userreceive where md5='".$_GET['md5code']."'";$dataw = mysqli_query($connect_to_database,$q);$q = mysqli_fetch_array($dataw)[0];?>需交人數:<font style="color: brown;"><?php echo($data[11]); ?></font></li><li>已交<font style="color: green;"><?php echo $q; ?> </font></li><li>未交<font style="color: red;"> <?php echo($data[11]-$q); ?> </font></li><li><a class="aa">按時間排序</a></li><li><a class="aa" title="如果發起的作業沒有設置學生名單,那么將不可用" onClick="get_student_homework()">未交學生名單</a></li></ul></div><div class="left"><table class="left_table"><tr class="trw" style="font-weight: bolder;"><td colspan="2"></td><td colspan="3" align="center">已交同學姓名</td><td colspan="8" align="center">提交文件名稱</td><td colspan="3" align="center">提交時間</td><td colspan="2" align="center">提交次數</td> <td colspan="2"></td> </tr><?phprequire_once('connect.php');$q = "select * from userreceive where md5='".$_GET['md5code']."'";$dataw = mysqli_query($connect_to_database,$q);$i = 0;while($every = mysqli_fetch_array($dataw)){if($i%2){echo('<tr class="trw"><td colspan="2"></td><td colspan="3" align="center">'.$every[2].'</td><td colspan="8" align="center">'.$every[3].'</td><td colspan="3" align="center">'.$every[5].'</td><td colspan="2" align="center">'.$every[4].'</td> <td colspan="2"></td> </tr>');}else{echo('<tr><td colspan="2"></td><td colspan="3" align="center">'.$every[2].'</td><td colspan="8" align="center">'.$every[3].'</td><td colspan="3" align="center">'.$every[5].'</td><td colspan="2" align="center">'.$every[4].'</td> <td colspan="2"></td> </tr>');}$i++;}if($i==0){echo('<tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr><td colspan="20" align="center" style="color:gray;">暫時還沒有同學提交,快來搶沙發吧!</td></tr>');}?><tr> <!-- trw--><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr></table></div><div class="right"><table class="right_table"><tr><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr><tr><td colspan="10"><h3 align="center" style="margin: 0;"><?php echo($data[3]); ?></h3></td></tr><tr></tr><tr><td colspan="2" style="color: gray;font-size: 10px;" align="right">格式要求:</td><td colspan="8"><h6 style="margin: 0;color: red;font-size: 13px;"><?php echo($data[4]); ?></h6></td></tr><tr><td colspan="2" align="right">發起人:</td><td colspan="8"><font class="text"><?php$id = $data[1];$quer = "select * from moreuserdata where userid=".$id."";$result = mysqli_query($connect_to_database,$quer);$result = mysqli_fetch_array($result)[3];if($result=="-"){$quer = "select * from users where userid=".$id."";$result = mysqli_query($connect_to_database,$quer);$result = mysqli_fetch_array($result)[1];echo $result;}else{echo($result);} ?></font></td></tr><tr></tr><tr><td colspan="10" align="center"><img src="register_php/user/<?php echo($id); ?>/icon/A.jpg" style="width: 40px;height: 40px;margin: 0 auto;border-radius: 25px;" alt=""></td></tr><tr></tr><tr><td colspan="3">作業開始時間:</td><td colspan="7"><font class="text"><?php echo($data[5]) ?></font></td></tr><tr><td colspan="3">作業結束時間</td><td colspan="7"><font class="text"><?php echo($data[6]) ?></font></td></tr><tr></tr><tr><td colspan="10"><?phpif($data[7]=="false"){echo('備注:發起者未采用準時郵件發送系統。');}else{echo('備注:發起者采用了準時郵件發送系統,超過時間系統自動打包作業郵件發送給老師郵箱。');}?></td></tr><tr></tr><tr><td colspan="10" align="center"><font style="color: green;font-size: 20px;font-weight: bolder;"><?php//計算時間·段 $statue = 0;$T = strtotime($data[6]);$all = floor((time()-$T));if($all>0){$statue = 0;echo('已結束!');}else{$T = strtotime($data[5]);$all = floor((time()-$T));if($all>0){echo('已經開始!剩余時間:');$statue = 1;$T = strtotime($data[6]);$all = floor(($T-time())); }else{$statue = 0;echo "未開始:倒計時";}$all = abs($all);// $dS = $all;// $dY = floor($all);// $dM = 0;$dD = floor($all/(3600*24));$all = $all-floor($all/60/60/24);$dH = floor($all/3600);$all = $all-floor($all/60/24);$dM = floor($all/60);$all = $all-floor($all/60);$dS = $all;//年月日時分秒////$all 得到的是多少分鐘以前 if ($dD>0){$finally_time = ($dD).'天';}else if($dH>0){$finally_time = $dH.'小時';}else if($dM>0){$finally_time = $dM.'分鐘';}else{$finally_time = $dS.'秒';}echo $finally_time;}?></font></td></tr></table><hr style="background:linear-gradient(to right,rgba(0,0,0,0.3),rgba(255,0,0,0.3));color: transparent;width: 90%;height: 0.1px;"><input type="text" id="md5value" value="<?php echo $_GET['md5code']; ?>" hidden=""><input type="file" hidden="" id="bufile" onChange="loadfile(this)" accept="application/"><table class="right_table"><tr><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr><tr><td colspan="10" align="center">已提交次數:<?php echo $_COOKIE['counts']; ?></td></tr><tr><td colspan="10" align="center"><button class="sure_b" <?php if($statue==0){echo(' title="當前狀態不能上傳作業!"');}else{echo(' onClick="start()"');} ?> >點擊提交</button></td></tr><tr></tr><tr><td colspan="10" align="center">提示:如果未設置準時發送郵件,可以聯系發送者進行設置延期提交。否則規定時間外不得提交作業!;<br><font style="color: red;font-weight: bolder;">當前設置了只有一次提交機會,請謹慎提交</font></td></tr></table></div><div class="nav"></div></div><div class="tip" id="tips"><div style="height: 10px;"></div><a id="fff" style="justify-content: center;display: flex;margin: 0 auto;">sdasdas</a></div><div style="width: 1000px;height: 30px; margin: 0 auto;position: relative;top: 100px;font-size: 10px;color: gray;"><a style="justify-content: center;display: flex;margin: 0 auto;">CharLinHeng all Reserved .</a></div> </body> </html> <script>$(document).tooltip();var times = 100;var ani;var successa= 1;function start(){$("#bufile").click();}function loadfile(boj){var file = boj.files[0];if(file.name.split(".")[(file.name.split(".").length-1)]!="docx"&&file.name.split(".")[(file.name.split(".").length-1)]!="doc"&&file.name.split(".")[(file.name.split(".").length-1)]!="jpg"&&file.name.split(".")[(file.name.split(".").length-1)]!="png"&&file.name.split(".")[(file.name.split(".").length-1)]!="pdf"&&file.name.split(".")[(file.name.split(".").length-1)]!="exe"){$("#fff").text("不支持的格式!");times = 2;$("#tips").css("display","block");ani = window.setInterval(function(){countds()},1000);return ;}//var datas = new FormData();datas.append("md5",$("#md5value").val());datas.append("file",file);$("#fff").text("上傳中....");$("#tips").css("display","block");if(window.XMLHttpRequest){var request = new XMLHttpRequest();request.onreadystatechange=function(){if(request.readyState==4&&request.status==200){$("#fff").text( request.responseText);times = 3;if(request.responseText.indexOf("成功")!=-1){successa = 0;}$("#tips").css("display","block");ani = window.setInterval(function(){countds()},1000);}};request.upload.onprogress = function (evt) {if (evt.lengthComputable) {var percentComplete = Math.round(evt.loaded * 100 / evt.total);$("#tips").css("display","block");$("#fff").text("上傳進度:"+percentComplete+"%");}};request.open("POST","acceptHomework.php",true);request.send(datas);}}function countds(){times--;if(times<=0){$("#tips").css("display","none");clearInterval(ani);if(successa!=1){window.location.href=window.location.href;}return ;}}function get_student_homework(){window.open("noload.php?md5="+$("#md5value").val());} </script>

    新建作業文件的代碼:

    <?phperror_reporting(0);require_once('connect.php');if($_POST['title']&&$_COOKIE['userid']){$size = rand(1000,2000);$size = md5($size);$query = "insert into homework(`userid`, `path`, `title`, `format_`, `starttime`, `endtime`, `auto_sendemail`, `email`, `use_you_email`, `youemail`, `studentnum`, `noticesyou`, `student`, `buildtime`, `statue`) values(".$_COOKIE['userid'].",'".$size."','".$_POST['title']."','".$_POST['format']."','".$_POST['starttime']."','".$_POST['endtime']."','".$_POST['auto_send']."','".$_POST['auto_send_email']."','".$_POST['use_my']."','".$_POST['use_my_1']."',".$_POST['num'].",'".$_POST['notices']."','".$_POST['content']."','".date("Y-m-d H:i:s",time())."',0)";$result = mysqli_query($connect_to_database,$query);if($result){echo('新建成功!您這次交作業的鏈接,請保管好!| http://www.erremall.top/neusoftforum/php/homework.php?md5code='.$size);//需要建立文件夾$dir = iconv("UTF-8", "GBK", "receiveHomework/homeworks/".$size);if (!file_exists($dir)){mkdir ($dir,0777,true);}else{echo("建立失敗,已存在相同類型MD5,請重新提交");}}else{echo('服務器繁忙!');}} ?>

    后臺處理文件acceptHomework.php的代碼如下:

    <?phperror_reporting(0);require_once('connect.php');header("Content-Type: text/html;charset=utf-8");if(!$_POST['md5']){echo "文件太大!文件最多不能超過200M";return ;}$md5 = $_POST['md5'];$_COOKIE['counts']++;$query_is_exist = "select * from homework where path='".$md5."'";$result = mysqli_query($connect_to_database,$query_is_exist);$this_result = mysqli_fetch_array($result);$T = strtotime($this_result[6]);$all = floor((time()-$T));if($all>0){echo "已結束";return ;}else{$T = strtotime($this_result[5]);$all = floor((time()-$T));if($all<0){echo "未開始";return ;}}$file = $_FILES['file'];$fileName = $_FILES['file']['name'];$file_tmp = $_FILES['file']['tmp_name'];$path = "receiveHomework/homeworks/".$md5."/";$path2 = "receiveHomework/homeworks/".$md5;$dir = iconv("UTF-8", "GBK", $path2);if (!file_exists($dir)){mkdir ($dir,0777,true);echo "新建文件夾成功!";}$path = "receiveHomework/homeworks/".$md5."/";$name = iconv('utf-8','gb2312',$fileName);//查找是否有這個同學,是否提交過,如果是,那么覆蓋$datanames = explode("\n",$this_result[13]);$name_ ="";$has_recode = false;$repeat = 0;for($i = 0;$i<count($datanames);$i++){if(strstr(trim($fileName),trim($datanames[$i]))===false){} else{$has_recode = true;$repeat = 1;$name_ = $datanames[$i];break;} // echo $datanames;}//repeat if($repeat==1){$query = "select x from userreceive where studentName='".$name_."' and md5='".$md5."'";$query = mysqli_query($connect_to_database,$query);$query = mysqli_fetch_array($query);if($query[0]!=""){echo "為防止其他同學惡意篡改,只有一次提交機會!";return ;}}if(strlen($this_result[13])>2){$query_has_sub ="select studentName,filename from userreceive where md5='".$md5."' and studentName='".$name_."'";$quee = mysqli_query($connect_to_database,$query_has_sub);$edata = mysqli_fetch_array($quee);if($edata[0]!=""){$repeat = 1;}else{$repeat = 0;}}else{$repeat = 0;}if($repeat==1){$name_edata = iconv('utf-8','gb2312',$edata[1]);unlink("./".$path.$name_edata);echo "更新文件成功!";}//查找 文件名有沒有出現在數據庫里,有,則覆蓋$sele_repeat_file = "select filename,x from userreceive where filename='".$fileName."'";$Q = mysqli_query($connect_to_database,$sele_repeat_file);$repeat_same_file = 0;$eeeeee = mysqli_fetch_array($Q);$repeat_file_id = $eeeeee[1];$eeeeee = $eeeeee[0];if($eeeeee[0]!=""){echo "已覆蓋文件";$name_edata = iconv('utf-8','gb2312',$fileName);unlink("./".$path.$name_edata);$repeat_same_file = 1;}//$success = move_uploaded_file($file_tmp,$path.$name);if($success){//新增 用戶信息//先搜索學生名字$names = "-";if(strlen($this_result[13])<2){echo "該作業沒有定義名單";}else{//有$dataname = explode("\n",$this_result[13]);$is_has = false;$names = "";for($i = 0;$i<count($dataname);$i++){if(strstr(trim($fileName),trim($dataname[$i]))===false){} else{$is_has = true;$names = $dataname[$i];break;}}if($is_has){if($repeat!=1)echo $names." 同學,提交成功啦!";}else{echo "該同學沒有找到,請注意格式或者聯系發起者更換名單!上傳的文件已刪除";unlink("./".$path.$name);return ;}}$timesq = $_COOKIE['counts'];if($timesq==0){$timesq = 1;}if($timesq==1){$has_fix = 0;}else{$has_fix = 1;}if($repeat==1||$repeat_same_file==1){if($repeat_same_file==1){$insert_data = "update userreceive set filename='".$fileName."',uploadtime='".Date("Y-m-d H:i:s",time())."',statue=1,uploadcount=".$timesq." where md5='".$md5."' and x=".$repeat_file_id; }else{$insert_data = "update userreceive set filename='".$fileName."',uploadtime='".Date("Y-m-d H:i:s",time())."',statue=1,uploadcount=".$timesq." where md5='".$md5."' and studentName='".$names."'"; }}else{$insert_data = "insert into userreceive( `md5`, `studentName`, `filename`, `uploadcount`, `uploadtime`, `statue`) values('".$md5."','".$names."','".$fileName."',".$timesq.",'".Date("Y-m-d H:i:s",time())."',".$has_fix.")"; }$result = mysqli_query($connect_to_database,$insert_data);if($result){if($repeat!=1){echo('上傳成功!'); }else{echo('更新成功!'); }}else{echo ("上傳失敗");}}else{echo '上傳失敗!請重試,可能是網絡的原因.';} ?>

    定時執行任務html文件:

    <!doctype html> <html> <head> <meta charset="utf-8"> <script src="../js/jquery_need.js"></script> <title>自動發送系統</title> </head><body id="div"><div id="tip" style="width: 1000px;height: 500px;overflow-y: auto;margin: 0 auto;position: relative;top: 10vw;justify-content: center;display: flex;margin: 0 auto;word-break: break-all;word-wrap: break-word;">提示</div></body> </html> <script>$(document).ready(function(){});function judge(){if(window.XMLHttpRequest){var request = new XMLHttpRequest();request.onreadystatechange=function(){if(request.readyState==4&&request.status==200){ // $('#div').prop('scrollTop',document.getElementById("div").scrollHeight);document.write(request.responseText);document.clear;if(request.responseText.indexOf("success")!=-1){//notices sender sned ok// print relative data... For example, email send Result.}}};request.open("GET",'handle_system_auto.php',true);request.send();}}window.setInterval(function (){judge();},8000); //you can adjust exec time 。你可以改變執行時間judge(); </script>

    后臺定時處理php文件:

    <?php//及時判斷。error_reporting(0);require_once('connect.php') ;//計算時間·段 $query_has_send = "select x,title,path,endtime from homework where statue=0";$query_is_exist = mysqli_query($connect_to_database,$query_has_send);while($every_homework = mysqli_fetch_array($query_is_exist)){$curr = strtotime($every_homework[3]);$rest = floor(time()-$curr);if($rest>=0){echo "編號:".$every_homework[0].",".$every_homework[1]." <a style='color:green;'>該作業可以打包發送了。</a><br>";//然后發送郵件給他 send mail for he//壓縮 run zip files.~header("location:zip_files.php?md5=".$every_homework[2]."&send=1&x=".$every_homework[0]);//發送郵件//郵件通知//更改結果//break;}else{echo $every_homework[1]." 時候未到,剩余:";$all = abs($rest);$dD = floor($all/(3600*24));$all = $all-floor($all/60/60/24);$dH = floor($all/3600);$all = $all-floor($all/60/24);$dM = floor($all/60);$all = $all-floor($all/60);$dS = $all;//年月日時分秒if ($dD>0){$finally_time = ($dD).'天';}else if($dH>0){$finally_time = $dH.'小時';}else if($dM>0){$finally_time = $dM.'分鐘';}else{$finally_time = $dS.'秒';}echo $finally_time."<br>";}} ?>

    后臺管理:

    <?phperror_reporting(0); // class Handle{ // function Handle(){ // // // } // // }if(!$_GET['id']&&!$_GET['stopid']&&!$_GET['lateDate']&&!$_GET['sender']&&!$_GET['goaler']&&!$_GET['zip']){echo "抱歉,請求參數出現錯誤,請重新嘗試!";return ;}require_once('connect.php');if($_GET['id']){ //刪除 $id_md5_file = $_GET['id'];$name = "select filename,md5 from userreceive where x=".$id_md5_file;$name_query = mysqli_query($connect_to_database,$name);$name_set = mysqli_fetch_array($name_query);$name_set_data = $name_set[0];$md5data = $name_set[1];$query_update = "delete from userreceive where x = ".$id_md5_file;$end_result = mysqli_query($connect_to_database,$query_update);if($end_result){echo "刪除成功!";$path = "receiveHomework/homeworks/".$md5data."/";$name_edata = iconv('utf-8','gb2312',$name_set_data);unlink("./".$path.$name_edata);}else{echo "刪除失敗,請重新提交!";} }if($_GET['stopid']){//截止$Dtime = date("Y-m-d H:i:s",time());$name = "update homework set endtime='".$Dtime."' where x=".$_GET['stopid'];$name_query = mysqli_query($connect_to_database,$name);if($name_query){echo "截止成功!即將發送郵箱";}else{echo "截止失敗,服務器繁忙";}}if($_GET['lateDate']){//延期日期}if($_GET['sender']){//修改發送者的郵箱$name = "update homework set use_you_email='true',youemail='".$_GET['sender']."' where x=".$_GET['ttt'];$name_query = mysqli_query($connect_to_database,$name);if($name_query){echo "修改發送者郵箱成功!";}else{echo "修改失敗,服務器繁忙";}}if($_GET['goaler']){$name = "update homework set email='".$_GET['goaler']."' where x=".$_GET['ttt'];$name_query = mysqli_query($connect_to_database,$name);if($name_query){echo "修改接受者郵箱成功!";}else{echo "修改失敗,服務器繁忙";}//修改接受者的郵箱}if($_GET['zip']){$namewww = "17軟件工程創新實驗班實驗1.zip"; //可以修改命名格式$name = "select path from homework where x=".$_GET['x'];$name_query = mysqli_query($connect_to_database,$name);$name_set = mysqli_fetch_array($name_query); echo $name_set[0]."|".$namewww;} ?>

    實戰: 效果截圖

    要實現這個功能,需要要一定的所有情況處理。需要以下全部文件:

    1.新建作業:

    新建成功

    后臺管理:

    交作業界面:

    后臺接收的文件:

    11點30分準時發送打包好的壓縮文件郵件,并且將沒有提交學生的名單羅列出來:

    演示視頻:

    上傳作業:
    匹配同學(不在作業列表的同學無法交作業,這里發起者可后臺設定指定交作業同學)

    判斷交作業是否含有指定同學名字

    判斷格式;(格式不合格學委可能會聯系你,至于為什么不寫個自動生成作業格式算法?因為我不想讓同學變懶。。。)

    交了之后可以下載(放心,絕對私密。后臺會生成臨時文件夾并在你下載之后秒刪除。服務器各級目錄權限也是配置好的。并且設置cookie和session雙重防護。不會讓你的文件泄露)和預覽


    截止時間后,不能再提交。(后臺前端會雙重判斷驗證時間范圍有效性。所以卡頁面的時間卡法是沒用的,而且時間一到,系統會自動壓縮文件將它發送到指定老師的郵箱哦。你是沒機會交的。。只能補交了)


    后臺查看交作業的同學,可以進行各種操作(作業叫錯了。只能叫學委在后臺刪除你的作業,重交。或者公眾號登入,綁定微信號后,可以提交多次呦)

    公眾號登入!(中間時間是在找手機= =)

    需要項目文件的感興趣的朋友,可以留言,會發過去。
    項目部署步驟:
    1.新建數據庫homeworkSet
    2.注入項目中的sql文件
    3.打開即可

    總結

    偶爾遇到的坑

    1.使用語法對php進行文件刪除或者把tmp文件復制的過程中,需要將傳過來的文件名進行轉換,不然會亂碼。
    方法:

    $fileName = iconv('utf-8','gb2312',$fileName);
    感慨

    總的來說,人是不斷進步的。多多運用所學知識去解決一些生活中的問題,才能將知識發揮用武之地。

    總結

    以上是生活随笔為你收集整理的基于PHP的定时交作业系统的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。