Simple file upload to local file for PHP development (1)

In this section, we use PHP code to upload files to a local folder and display them on the current page.

According to the idea mentioned in the previous section, we first create a simple form for uploading files

<html>
<head>
  <meta charset="utf-8">
  <title>图片上传</title>
  <style type="text/css">
    <!--
    body
    {
      font-size: 16px;
    }
    input
    {
      background-color: #66CCFF;
      border: 1px inset #CCCCCC;
    }
    -->
  </style>
</head>
<body>
    <form enctype="multipart/form-data" method="post" name="upform">
      上传文件:
      <input name="upfile" type="file">
      <input type="submit" value="上传"><br>
      允许上传的文件类型为:
    </form>
    <br>图片预览:<br>
    <img src=""/>
</body>
</html>

Note here:

<form> The enctype attribute of the tag specifies in What content type to use when submitting the form. Use "multipart/form-data" when your form requires binary data, such as file content.

An image preview <img> is created at the bottom of the page to display the uploaded file.

The PHP code can later display the file name, size, length and width of the file saved in the local folder.


Secondly, we need to make some restrictions on uploaded files:

Types of uploaded files: $uptypes

<?php
    $uptypes=array(
      'image/jpg',
      'image/jpeg',
      'image/png',
      'image/gif',
      'image/bmp',
    );  //限制上传格式为:jpg, jpge, png, gif, bmp
?>

Alright Set the upload file size, upload file path, etc. Here we have added image watermark settings.

<?php
    $max_file_size=2000000;     //上传文件大小限制, 单位BYTE
    
    $destination_folder="uploadimg/"; //上传文件路径,默认本地路径
    
    $watermark=1;      //是否附加水印(1为加水印,其他为不加水印);
    
    $watertype=1;      //水印类型(1为文字,2为图片)
    
    $waterposition=1;     //水印位置(1为左下角,2为右下角,3为左上角,4为右上角,5为居中);
    
    $waterstring = "";  //水印字符串
    
    $waterimg="";    //水印图片
    
    $imgpreview=1;      //是否生成预览图(1为生成,其他为不生成);
    
    $imgpreviewsize=1/2;    //缩略图比例
?>


Continuing Learning
||
<html> <head> <meta charset="utf-8"> <title>图片上传</title> <style type="text/css"> <!-- body { font-size: 16px; } input { background-color: #66CCFF; border: 1px inset #CCCCCC; } --> </style> </head> <body> <form enctype="multipart/form-data" method="post" name="upform"> 上传文件:<br><br> <input name="upfile" type="file"> <input type="submit" value="上传"><br><br> 允许上传的文件类型为: </form> <br>图片预览:<br> <img src=""/> </body> </html>
submitReset Code