批改状态:合格
老师批语:

<!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="content-type" content="text/html;charset=utf-8" /> <title> 上传你的文件 </title> </head> <form enctype="multipart/form-data" method="post" action="uploadprocess.php"> <table> <tr> <td align="center" colspan="2"> <font style="font-size:40px;font-family: 华文彩云;"> 文件上传 </font> </td> </tr> <tr> <td> 请填写用户名: </td> <td> <input type="text" name="username" /> </td> </tr> <tr> <td> 请简单介绍该文件 </td> <td> <textarea name="fileintro" rows="10" cols="80"> </textarea> </td> </tr> <tr> <td> 请选择你要上传文件: </td> <td> <input type="file" name="myfile" /> </td> </tr> <tr> <td> <input type="submit" value="上传文件" /> </td> <td> </td> </tr> </table> </form> </html>
点击 "运行实例" 按钮查看在线实例
<?php
//1.接收提交文件的用户
$username=$_POST['username'];
$fileintro=$_POST['fileintro'];
//我们这里需要使用到 $_FILES
/*echo "<pre>";
print_r($_FILES);
echo "</pre>";*/
//其实我们在上传文件时,点击上传后,数据由http协议先发送到apache服务器那边,这里apache服务器已经将上传的文件存放到了服务器下的C:\windows\Temp目录下了。这时我们只需转存到我们需要存放的目录即可。
//php中自身对上传的文件大小存在限制默认为2M
//获取文件的大小
$file_size=$_FILES['myfile']['size'];
if($file_size>2*1024*1024) {
echo "文件过大,不能上传大于2M的文件";
exit();
}
$file_type=$_FILES['myfile']['type'];
// echo $file_type;
if($file_type!="image/jpeg" && $file_type!='image/pjpeg' && $file_type!='image/png') {
echo "文件类型只能为jpg格式";
exit();
}
//判断是否上传成功(是否使用post方式上传)
if(is_uploaded_file($_FILES['myfile']['tmp_name'])) {
//把文件转存到你希望的目录(不要使用copy函数)
$uploaded_file=$_FILES['myfile']['tmp_name'];
//我们给每个用户动态的创建一个文件夹
$user_path=$_SERVER['DOCUMENT_ROOT']."/0419/".$username;
// echo "$user_path";
//判断该用户文件夹是否已经有这个文件夹
if(!file_exists($user_path)) {
mkdir($user_path);
}
//$move_to_file=$user_path."/".$_FILES['myfile']['name'];
$file_true_name=$_FILES['myfile']['name'];
$move_to_file=$user_path."/".time().rand(1,1000).substr($file_true_name,strrpos($file_true_name,"."));
// echo "$uploaded_file $move_to_file";
if(move_uploaded_file($uploaded_file,iconv("utf-8","gb2312",$move_to_file))) {
echo $_FILES['myfile']['name']."上传成功";
} else {
echo "上传失败";
}
} else {
echo "上传失败";
}
?>点击 "运行实例" 按钮查看在线实例
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号