search
HomeBackend DevelopmentPHP ProblemHow 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

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>

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"; } ?>
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
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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version