<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>图片上传</title>
<meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0">
</head>
<body>
<form action="../project.php" method="post" enctype="multipart/form-data">
选择上传的图片: <input type="file" name="file">
选择上传的图片: <input type="file" name="demo">
<br><br>
<input type="submit" value="上传">
</form>
</body>
</html>
<?php
header("Content-Type: text/html;charset=utf-8");
require './includes/connect.class.php';
$conn = Connect::getInstance();
if ($_FILES["file"]["error"] > 0) {
switch ($_FILES["file"]["error"]) {
case 1 :
echo "<script type='text/javascript'>alert('上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值<br>');history.back();</script>";
break;
case 2 :
echo "<script type='text/javascript'>alert('上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值');history.back();</script>";
break;
case 3 :
echo "<script type='text/javascript'>alert('文件只有部分被上传');history.back();</script>";
break;
case 4 :
echo "<script type='text/javascript'>alert('没有文件被上传');history.back();</script>";
break;
default :
echo "<script type='text/javascript'>alert('末知错误');history.back();</script>";
}
exit;
}
$maxsize = 10000000;
if ($_FILES["file"]["size"] > $maxsize) {
echo "<script type='text/javascript'>alert('上传的文件太大,不能超过{$maxsize}字节');history.back();</script>";
exit;
}
$allowtype = array("png", "gif", "jpg", "jpeg");
$arr = explode(".", $_FILES["file"]["name"]);
$hz = $arr[count($arr) - 1];
if (!in_array($hz, $allowtype)) {
echo "<script type='text/javascript'>alert('这是不允许的类型');history.back();</script>";
exit;
}
date_default_timezone_set('PRC');
$randname = date("Y") . date("m") . date("d") . date("H") . date("i") . date("s") . rand(100, 999) . "." . $hz;
$filepath = "upload/" . $randname;
echo $filepath;
if(move_uploaded_file($_FILES["file"]["tmp_name"],$filepath)){
echo '上传成功';
}else{
echo '上传失败';
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了