HTML
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>文件上传</title> </head> <body> <form action="?" method="post" enctype="multipart/form-data"> <input type="file" name="file"> <input type="submit" name="submit" value="上传"> </form> </body> </html>
PHP
if (isset($_POST['submit'])) { // 获取当前日期 $date = date('Ymd',time()); // 获取文件扩展名 $type = $_FILES['file']['name']; $pos = substr($type,strrpos($type, '.')); // 如果文件夹upload不存在,则创建 if (!is_dir('upload/')) { mkdir('upload/'); } // 创建日期文件夹 if (!is_dir('upload/'.$date)) { mkdir('upload/'.$date); } // 上传之前路径 $tmpfile = $_FILES['file']['tmp_name']; // 上传之后路径 $desfile = 'upload/'.$date.'/'.md5(time()).$pos; if(move_uploaded_file($tmpfile, $desfile)) { echo '<script>alert("上传成功")</script>'; } else { echo '<script>alert("上传失败")</script>'; } }