Home > Backend Development > PHP Tutorial > File upload image in php

File upload image in php

迷茫
Release: 2023-03-06 22:20:01
Original
1652 people have browsed it
要实现文件上传图片我们需要写出两个php文件,第一个php文件我们需要写出一个文件上传的页面,第二个php文件我们写出实现图片上传的功能
Copy after login
<!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="upload.php" method="post" enctype="multipart/form-data">
   <input type="file" name="file" />
    <input type="submit" value="上传" />
</form>
</body>
</html>
Copy after login
<?php
var_dump($_FILES["file"]);

// 限制文件的类型
// 限制文件的大小
// 防止文件名重复
   
//     一.防止文件名重复
//    1.修改文件名
//    流水号,时间戳+随机数+用户名

//    2.建文件夹
//    upload/20170317/lch/shangchuan/11.jpg

//    3.保存文件

if($_FILES["file"]["error"])
{
   echo $_FILES["file"]["error"];
}
else
{
   //没有出错
   
    //加限制条件
   if(($_FILES["file"]["type"]=="image/png" || $_FILES["file"]["type"]=="image/jpeg") && $_FILES["file"]["size"]<1024000)
   {
      
      //防止文件名重复
      $filename = "./img/".time().$_FILES["file"]["name"];
      
    //转码
      $filename = iconv("UTF-8","gb2312",$filename);
      
      
    if(file_exists($filename))
      {
         echo "该文件已存在";
      }
      else
      {
         //保存文件
         move_uploaded_file($_FILES["file"]["tmp_name"],$filename);
      }
   }
   else
   {
      echo "文件类型不对";
   }
}
Copy after login

Related articles:

The mobile terminal implements the file upload function through HTML5

JavaScript Advanced (9) JS implements local file upload to Alibaba Cloud Server

Detailed introduction to file upload of HTML5 application

The above is the detailed content of File upload image in php. 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