Home Backend Development PHP Problem How to use files in php

How to use files in php

May 25, 2023 am 10:39 AM
php

How to use files in php: 1. "$_FILES['myFile']['name']" displays the original name of the client file; 2. "$_FILES['myFile']['type ']" MIME type of the file; 3. The size of the uploaded file in "$_FILES['myFile']['size']"; 4. The temporary file stored in "$_FILES['myFile']['tmp_name']" How to use names, etc.

How to use files in php

Operating system for this tutorial: Windows 10 system, PHP version 8.1.3, Dell G3 computer.

Common $_FILES system function usage in the PHP programming language are:

  1. $_FILES['myFile']['name'] Displays the original name of the client file.

  2. $_FILES['myFile']['type'] The MIME type of the file, such as "image/gif".

  3. $_FILES['myFile']['size'] The size of the uploaded file, in bytes.

  4. $_FILES['myFile']['tmp_name'] The name of the temporary file stored, usually the system default.

  5. $_FILES['myFile']['error'] The error code related to file upload.

The following are the meanings of different codes:

  • 0; The file was uploaded successfully.

  • 1; The file size exceeds the size set by the system in php.ini.

  • 2; The file size is exceeded, the value specified by the MAX_FILE_SIZE option.

  • 3; Only part of the file was uploaded.

  • 4; No files were uploaded.

  • 5; The uploaded file size is 0.

Example:

<html>
<body>
<form action="upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file">filename:</label>
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="submit" />
</form>
</body>
</html
Copy after login

php processing code

This article will briefly introduce the Friends who need to know about various parameters of $_FILES, an important global variable when uploading files and images in PHP, can refer to it.

Processing of file upload form

The code is as follows:

<form action="post.php" enctype="multipart/form-data" method="post">
<input name="MAX_FILE_SIZE" type="hidden" value="1000" />
<input name="upfile" type="file" />
<input type="submit" value="上传文件" />
</form>
Copy after login

Usage of variables of files

$_FILES super Global variables are very special. They are the only two-dimensional arrays in the predefined super global array. Its role is to store various information related to uploaded files, which is crucial for files uploaded to the server through PHP scripts. There are a total of 5 items in this function:

1, $_FILES["userfile"]["error"]

$_FILES["userfile"]["error"] The array value provides Important information about the results of your upload attempt. There are 5 different return values ​​in total, one indicating a successful result and the other 4 indicating specific errors that occurred during the attempt. The names and names of the return values ​​will be introduced later.

2. The $_FILES["userfile"]["name"]

$_FILES["userfile"]["name"] variable specifies the file declared on the client machine The original name, including extensions. So if you browser a file called vacation.jpg and upload it via a form, the value of this variable will be vacation.png.

3. The $_FILES["userfile"]["size"]

$_FILES["userfile"]["size"] variable specifies the size of the file uploaded from the client. The unit is bytes. Therefore, in the example of the vacation.jpg file, this function may be assigned a value of 5253, which is approximately 5kb.

4, $_FILES["userfile"]["tmp_name"]

$_FILES The ["userfile"]["tmp_name"] variable specifies the temporary name given to the file after uploading to the server. This is the filename specified when stored in the temporary directory (specified by the PHP directive upload_tmp_dir).

5. The $_FILES["userfile"]["type"]

$_FILES["userfile"]["type"] variable specifies the mime type of the file uploaded from the client. Therefore, in the example of the vacation.jpg file, this variable would be assigned the value image/jpeg. If the upload is a PDF, the value is application/pdf. Because this variable can sometimes give unexpected results, it should be verified explicitly in the script.

$_FILES['myFile']['error'] Error code related to the file upload. ['error'] was added in PHP 4.2.0. The following is its description: (They became constants after PHP3.0)

UPLOAD_ERR_OK value: 0; No error occurred and the file was uploaded successfully.

UPLOAD_ERR_INI_SIZE value: 1; The uploaded file exceeds the value limited by the upload_max_filesize option in php.ini.

UPLOAD_ERR_FORM_SIZE value: 2; The size of the uploaded file exceeds the value specified by the MAX_FILE_SIZE option in the HTML form.

UPLOAD_ERR_PARTIAL value: 3; Only part of the file was uploaded.

UPLOAD_ERR_NO_FILE value: 4; No file was uploaded. Value: 5; The uploaded file size is 0.

Let’s look at a complete example

Simple file upload

##
<html>
<body>
<form action="upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file">filename:</label>
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="submit" />
</form>
</body>
</html>
php代码
 0)
{
echo "error: " . $_files["file"]["error"] . "
"; } else { echo "upload: " . $_files["file"]["name"] . "
"; echo "type: " . $_files["file"]["type"] . "
"; echo "size: " . ($_files["file"]["size"] / 1024) . " kb
"; echo "stored in: " . $_files["file"]["tmp_name"]; } } else { echo "invalid file"; } ?>
Copy after login
For IE, the type of jpg file recognized must be pjpeg, and for FireFox, it must be jpeg.

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

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

See all articles