Correction status:Uncorrected
Teacher's comments:
总结:没有听明白ajax如何回传数据,网上百度没有找到,一直郁闷中。
<!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>文件上传工具</title> <style type="text/css"> div{ width:360px; margin:auto; border:4px solid blue; border-radius:20px; } form{ width:360px; margin:10px auto; text-align:center; } h2{ text-align:center; } button{ width:80px; height:30px; background-color:green; color:white; border:none; cursor:pointer; } button:hover{ background-color:red; } </style> </head> <body> <div> <p><h2>文件上传工具</h2></p> <form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="POST" enctype="multipart/form-data"> <p><label for="file">文件名:</label><input type="file" name="file" id="file"></p> <p><button type="submit" name="submit">提交</button></p> </form> <?php // if (isset($_POST['submit'])) if($_SERVER['REQUEST_METHOD']=="POST") { if (!empty($_FILES['file']['tmp_name'])) { $allowedExts = array("gif", "jpeg", "jpg", "png"); $tmp = explode('.', $_FILES['file']['name']); $ext = end($tmp); if (in_array($ext, $allowedExts)) { if ($_FILES['file']['error'] > 0) { echo '<p>错误原因是:<strong>'; switch ($_FILES['file']['error']) { case 1: echo '文件超过了php.ini配置中设置的大小'; break; case 2: echo '文件超过了表单中常量设置的大小'; break; case 3: echo '仅有部分文件被上传'; break; case 4: echo '没有文件被上传'; break; case 6: echo '没有可用的临时文件夹'; break; case 7: echo '磁盘已满,写入失败'; break; case 8: echo '上传意外中止'; break; default: echo '系统未知错误'; break; } echo '</strong></p>'; } else { $dir = 'upload'; if (is_dir($dir)) { // echo "当前目录下,目录".$dir."存在"; date_default_timezone_set("PRC"); $newName=date('Y').date('m').date('d').date('H').date('i').'.'.$ext; if (move_uploaded_file($_FILES["file"]["tmp_name"], "upload/". $newName)) { echo "<script>alert('上传成功!')</script>"; if (file_exists($_FILES['file']['tmp_name']) && is_file($_FILES['file']['tmp_name'])) { unlink($_FILES['upload']['tmp_name']); } } } else { //无目录,立即创建 echo "<script>alert('当前目录不存在,已经建立完成!')</script>"; mkdir("upload"); } } } else { echo '文件格式不对!'; } } else { echo "<script>alert('请上传文件!')</script>"; } } ?> <script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script> <script type="text/javascript"> </script> </div> </body> </html>
点击 "运行实例" 按钮查看在线实例