PHP simple file upload

    Only a demo of simple file upload is given here. If the file is too large, you can consider using multi-part upload.

    HTML file

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <title>demo</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  </head>
  <body>
    <form action="upload.php" method="post" enctype="multipart/form-data">
      <input type="file" name="file" id="file" />
      <br />
      <input type="submit" name="submit" value="upload" />
    </form>
  </body>
</html>
    PHP backend

<?php
  //The key point is that because the video file is uploaded, the file size will exceed the default configuration of the server and php, and the configuration needs to be manually modified
  //php configuration file php.ini
  //file_uploads on The switch to allow file uploading via HTTP. Default is ON
  // upload_tmp_dir – the file is uploaded to the server where temporary files are stored, if not specified, the system default temporary folder will be used
  // upload_max_filesize 8m Wangwen business, that is, the maximum allowable upload file size. Default is 2M
  // post_max_size 8m refers to the maximum value that can be received through form POST to PHP, including all values ​​in the form. Default is 8M

  //If it is an nginx server, you also need to modify the nginx configuration file. Below is the modified configuration of my nginx with client_max_body_size 100m added;
  // server {
  //     listen       83;
  //     server_name localhost;
  //     root /usr/share/nginx/html;
  //     index index.php;
  //     client_max_body_size 100m;

  //     access_log /var/log/nginx/html-access.log;
  //     error_log  /var/log/nginx/html-error.log;

  //     location / {
  //         try_files $uri $uri/ /index.php?$args =404;
  //         #try_files $uri$args $uri$args/ index.php;
  //     }

  //     location ~ .*\.(php|php7.0)?$ {
  //         #fastcgi_pass   127.0.0.1:9000;
  //         fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
  //         include        fastcgi_params;
  //     }

  //     location ~ /\.(ht|svn|git) {
  //             deny all;
  //     }
  // }

  / / After the modification is required to restart php, and the server
  // sudo service php7.0-fpm restart (ubuntu下)
  // sudo service nginx restart(ubuntu下)


  //The directory where the current script file is located
  define('ROOT_DIR', dirname(__FILE__) . DIRECTORY_SEPARATOR);

  //The uploaded file data object file information is stored in this array
  $file = $_FILES["file"];

  //Format and size verification only accepts mp4, rmvb format file size limit is 100M If you need to add or change it, modify it here
  if (($file["type"] == "video/mp4" || $file["type"] == "video/rmvb") && $file["size"] < 100 * 1024 * 1024) {
    // Judge whether the upload was successful or not
    if ($file["error"] > 0) {
      echo "Error message: " . $file["error"] . "<br />";
    } else {
      // whether the file already exists
      if (file_exists("upload/" . $file["name"])) {
        echo $file["name"] . " already exists. ";
      } else {
        //Whether it is a file submitted through a form
        if (is_uploaded_file($file['tmp_name'])) {
          //Save path splicing requires the upload directory permission to be 777
          $save_path = ROOT_DIR . 'upload/' . basename($file['name']);

          //Use the function to save the uploaded temporary file to a custom directory
          if (move_uploaded_file($file['tmp_name'], $save_path)) {
            echo "Uploaded successfully, the file is saved in: " . $save_path;
          } else {
            echo 'File upload failed';
          }
        } else {
          echo 'file source error';
        }
      }
    }
  } else {
    echo "Invalid file, only supports 100M files in mp4, rmvb format";
  }
?>


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325827757&siteId=291194637