我是一名PHP萌新,今天给大家分享PHP如何实现图片上传及储存,如果着急直接跳到最后,那废话不多说,直接开始。
实现功能:对图片进行筛选,只储存后缀为:gif,jpg,jpeg,png的图片,并将大小限制在1GB以下,改名为日期,储存在picture文件夹下。
1.筛选图片:
$bbb= substr(strrchr($_FILES["file"]["name"],"."),1,100000);
if ((($bbb == "gif")
|| ($bbb == "JPG")
|| ($bbb == "jpeg")
|| ($bbb == "jpg")
|| ($bbb == "png")
|| ($bbb == "pjpeg"))
&& ($_FILES["file"]["size"] < 1000000000))
2.改名为日期:
$_FILES["file"]["name"]= time().".".$bbb;
3.输出文件参数并储存:
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "文件名称: " . $_FILES["file"]["name"] . "<br />";
echo "文件类型: " . $bbb . "<br />";
if ($_FILES["file"]["size"]/1048576 < 1){
if($_FILES["file"]["size"]/1024 < 1){
$a=floor($_FILES["file"]["size"]*100)/100;
echo "大小: " . $a . "B" . "<br>";
}
else {
$a=floor($_FILES["file"]["size"]/10)/100;
echo "大小: " . $a . "KB" . "<br>";
}
}
else {
$a=floor($_FILES["file"]["size"]/10485)/100;
echo "大小: " . $a. "MB" . "<br>";
}
if (file_exists("picture/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . "文件名重复!请改名.";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"picture/" . $_FILES["file"]["name"]);
echo "图片保存在: " . "picture/" . $_FILES["file"]["name"]."<br>";
echo "<img src='". "picture/" . $_FILES["file"]["name"]."' style='width:60vw;'>";
}
}
}
else
{
echo "Type: " . $bbb . "<br />";
echo "请上传正确格式的照片";
}
成品:
1.上传.html
<html>
<head>
<title>demo</title>
<meta charset="utf-8">
<style>
#ssd{
position: fixed;
top: 0;
left: 0;
width: 100vw;
height: 70vh;
display: none;
z-index: 1;
}
</style>
<script src="ceju.js" defer></script>
</head>
<body>
<div class="bg-img"></div>
<form target="hhh" action="图片保存.php" method="post" enctype="multipart/form-data" style="text-align: center;">
<label for="file" id="fileout"><h2>选择图片</h2></label>
<div id="outspanss">
<input type="file" name="file" id="file" accept="image/*"/>
</div>
<br />
<br>
<input type="submit" name="submit" value="上传" onclick="ccccc()" id="shangchuang"/>
</form>
<a href="https://chuyisanban.rth.app">返回</a>
<iframe id="ssd" name="hhh"></iframe>
</body>
<script>
function ccccc(){
var iframe=document.getElementById("ssd");
iframe.style.display="block";
}
</script>
</html>
2.图片保存.php
<?php
$bbb= substr(strrchr($_FILES["file"]["name"],"."),1,100000);
if ((($bbb == "gif")
|| ($bbb == "JPG")
|| ($bbb == "jpeg")
|| ($bbb == "jpg")
|| ($bbb == "png")
|| ($bbb == "pjpeg"))
&& ($_FILES["file"]["size"] < 1000000000))
{
$_FILES["file"]["name"]= time().".".$bbb;
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "文件名称: " . $_FILES["file"]["name"] . "<br />";
echo "文件类型: " . $bbb . "<br />";
if ($_FILES["file"]["size"]/1048576 < 1){
if($_FILES["file"]["size"]/1024 < 1){
$a=floor($_FILES["file"]["size"]*100)/100;
echo "大小: " . $a . "B" . "<br>";
}
else {
$a=floor($_FILES["file"]["size"]/10)/100;
echo "大小: " . $a . "KB" . "<br>";
}
}
else {
$a=floor($_FILES["file"]["size"]/10485)/100;
echo "大小: " . $a. "MB" . "<br>";
}
if (file_exists("picture/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . "文件名重复!请改名.";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"picture/" . $_FILES["file"]["name"]);
echo "图片保存在: " . "picture/" . $_FILES["file"]["name"]."<br>";
echo "<img src='". "picture/" . $_FILES["file"]["name"]."' style='width:60vw;'>";
}
}
}
else
{
echo "Type: " . $bbb . "<br />";
echo "请上传正确格式的照片";
}
?>
|