Home > Backend Development > PHP Tutorial > PHP file upload function enables code sharing

PHP file upload function enables code sharing

巴扎黑
Release: 2023-03-15 14:04:02
Original
2929 people have browsed it

This article mainly introduces the example code of PHP to implement the file upload function. The code is simple and easy to understand, very good, and has reference value. Friends in need can refer to the following

Click to browse and upload the selected file Upload to the created images folder

The code is as follows:

1.wenjian.php


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<body>
<h1>上传文件</h1>
<form action="chuli.php" method="post" enctype="multipart/form-data">
请选择文件:<input type="file" name="file" /><input type="submit" value="上传" />
</form>
</body>
</html>
Copy after login

2.chuli.php


##

<?php
//取文件信息
$arr = $_FILES["file"];
//var_dump($arr);
//加限制条件
//1.文件类型
//2.文件大小
//3.保存的文件名不重复
if(($arr["type"]=="image/jpeg" || $arr["type"]=="image/png" ) && $arr["size"]<10241000 )
{
//临时文件的路径
$arr["tmp_name"];
//上传的文件存放的位置
//避免文件重复: 
//1.加时间戳.time()加用户名.$uid或者加.date(&#39;YmdHis&#39;)
//2.类似网盘,使用文件夹来防止重复
$filename = "./images/".date(&#39;YmdHis&#39;).$arr["name"];
//保存之前判断该文件是否存在
  if(file_exists($filename))
  {
    echo "该文件已存在";
  }
  else
  {
  //中文名的文件出现问题,所以需要转换编码格式
  $filename = iconv("UTF-8","gb2312",$filename);
  //移动临时文件到上传的文件存放的位置(核心代码)
  //括号里:1.临时文件的路径, 2.存放的路径
  move_uploaded_file($arr["tmp_name"],$filename);
  }
}
else
{
  echo "上传的文件大小或类型不符";
}
Copy after login

The above is the detailed content of PHP file upload function enables code sharing. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template