php向服务器上传文件以及图片预览
程序员文章站
2022-05-09 11:22:07
...
在后台开发的过程中,经常需要满足应用有向服务器上传文件的功能:
1. 后台请求地址页面操作(也可以是当前页面):
<?php
header('content-type:text/html,charset=gbk');
?>
<?php
if(!empty($_FILES)){
$current_img = $_FILES['lgc_img'];
$http_path = dirname('http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']).'/images/'.$current_img['name'];
$file_dir = dirname(__FILE__).'/images/'.$current_img['name'];
echo $current_img['tmp_name'].'<br/>';
echo $file_dir;
$bool = @move_uploaded_file($current_img['tmp_name'],$file_dir);
}
?>
2. 前台表单页面:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<form action="" method="post" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="1000000" /> 上传文件:
<input type="file" name="lgc_img" />
<input type="submit" name="submit" value="提交" />
</form>
<?php
if(@$bool)
{
?>
<img src='<?php echo $http_path; ?>' width='10%' />
<?php
}
?>
</body>
</html>
* 此案例action="" , 说明请求的当前页面
* 总结:主要用到了 $_FILES 和 move_uploaded_file()
上一篇: mobile-detect 解析userAgent
下一篇: 心脏出血漏洞修复记录