Home  >  Article  >  Backend Development  >  How to receive file upload in PHP

How to receive file upload in PHP

(*-*)浩
(*-*)浩Original
2019-09-30 11:11:3610657browse

In PHP, there is a predefined variable $_files that is specially used to store files uploaded by users.

How to receive file upload in PHP

"upload_file.php" The file contains the code for uploading files: (recommended learning: PHP Video Tutorial)

<?php
if ($_FILES["file"]["error"] > 0)
  {
  echo "Error: " . $_FILES["file"]["error"] . "<br />";
  }
else
  {
  echo "Upload: " . $_FILES["file"]["name"] . "<br />";
  echo "Type: " . $_FILES["file"]["type"] . "<br />";
  echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
  echo "Stored in: " . $_FILES["file"]["tmp_name"];
  }
?>

By using PHP’s global array $_FILES, you can upload files from the client computer to the remote server.

The first parameter is the input name of the form, and the second subscript can be "name", "type", "size", "tmp_name" or "error". Like this:

$_FILES["file"]["name"] - The name of the uploaded file

$_FILES["file"]["type"] - The uploaded file Type

$_FILES["file"]["size"] - The size of the uploaded file, in bytes

$_FILES["file"]["tmp_name"] - The name of a temporary copy of the file stored on the server

$_FILES["file"]["error"] - error code caused by a file upload

This is a very simple file upload Way. For security reasons, you should add restrictions on who has permission to upload files.

The above is the detailed content of How to receive file upload in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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