Home > Backend Development > PHP Tutorial > Detailed explanation of php function for uploading image files

Detailed explanation of php function for uploading image files

墨辰丷
Release: 2023-03-30 20:52:02
Original
1394 people have browsed it

This article mainly introduces the detailed explanation of PHP's function for uploading image files. Interested friends can refer to it. I hope it will be helpful to everyone.

In PHP program development, file upload is a very commonly used function and is also one of the necessary skills for PHP programmers. Fortunately, implementing the file upload function in PHP is much simpler than in languages ​​such as Java and C#. Below we combine specific code examples to introduce in detail how to implement file upload and multi-file upload functions through PHP.

The code is very simple, we won’t talk too much here, just provide the source code

<?php
// 注册表单的姓名
$name="";
$nameErr="";
if ($_SERVER["REQUEST_METHOD"]=="POST") {
  if (empty($_POST[&#39;name&#39;])) {
  }else{
    $name=$_POST[&#39;name&#39;];
    if (!preg_match("/^[a-zA-Z]*$/", $name)) {
      $nameErr="只允许字母和空格";
    }else{
      echo &#39;姓名&#39;.$name;
    }
  }
 
  // 文件上传
    if ((($_FILES["file"]["type"] == "image/gif")
    || ($_FILES["file"]["type"] == "image/jpeg")
    || ($_FILES["file"]["type"] == "image/pjpeg"))
    && ($_FILES["file"]["size"] < 2000000))
    {
 
      if ($_FILES["file"]["error"]>0) {
        echo "错误:".$_FILES["file"]["error"]."<br/>";
 
      }else{
        echo "upload:".$_FILES["file"]["name"]."<br/>";
        echo "type:".$_FILES["file"]["type"]."<br/>";
        echo "size:".$_FILES["file"]["size"]."<br/>";
        echo "stored in:".$_FILES["file"]["tmp_name"];
      }  
    }else{
      if (file_exists("weiwei/".$_FILES["file"]["name"])) {
        echo $_FILES["file"]["name"]."上传成功.";
      }else{
         move_uploaded_file($_FILES["file"]["tmp_name"],
       "weiwei/" . $_FILES["file"]["name"]);
       echo "Stored in: " . "weiwei/" . $_FILES["file"]["name"];
      }
 
  echo "上传成功";
 
}
 
}
   
?>
 
<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>注册表单</title>
</head>
<body>
  <form action="" method="post" enctype="multipart/form-data">
  姓名:<input type="text" name="name" value="">
   <span class="error">* <?php echo $nameErr;?></span>
  文件类型:<input type="file" name="file" id="file">
  <img src="<?php echo "weiwei/".$_FILES[&#39;file&#39;][&#39;name&#39;]?>" alt="">
 
 
  <input type="submit" name="sub" value="提交">
  </form>
</body>
</html>
Copy after login

Summary: The above is the entire content of this article, I hope it can be helpful to everyone learning helps.

Related recommendations:

PHP probability algorithm function

Types of PHP comparison operators

Word guessing game implemented in php

The above is the detailed content of Detailed explanation of php function for uploading image files. 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