


Today I will continue the series of php9 super global variables. Today I will talk about the usage of $_FILE.
First of all, similar to $_POST, etc., $_FILES is suitable for input tags of uploaded files. $_FIFLES is similar to a two-dimensional array, which can obtain and save various information about files uploaded by the client, as follows:
$_FILES['myFile']['name'] The original name of the client file.
$_FILES['myFile']['type'] The MIME type of the file, which requires the browser to provide support for this information, such as "image/gif".
$_FILES['myFile']['size'] The size of the uploaded file, in bytes.
$_FILES['myFile']['tmp_name'] The temporary file name stored on the server after the file is uploaded, usually the system default. It can be specified in upload_tmp_dir in php.ini, but setting it with the putenv() function has no effect.
$_FILES['myFile']['error'] The error code related to the file upload. ['error'] was added in PHP 4.2.0. The following is its description: (They become constants after PHP3.0)
Therefore, the above variables can be used to process files uploaded by users in the background. For example, if you can only upload pictures, you can limit them through $_FILES['myFile']['type'], and limit the size. Specify it through $_FILES['myFile']['size']. Next, I will use an example on w3school to explain the specific usage of $_FILES.
The main function of this demo is to upload image files and save them in the relevant directory on the server side.
The html code is as follows:
<html> <body> </body> </html>
The code is very simple. It just defines an upload control. Note that the value 'file' corresponding to this name is the basis for $_FILES to obtain the front-end file. If the file type is obtained in the background, write $_FILES['myFile']['type'] , indicating that the value corresponding to the name at the front desk must be 'myFile', otherwise an Invalid file error will be reported. Finally, take a look at the background code:
<?php $allowedExts = array("gif", "jpeg", "jpg", "png"); /* * explode(separator,string,limit) 参数 描述 separator 必需。规定在哪里分割字符串。 string 必需。要分割的字符串。 limit 可选。规定所返回的数组元素的最大数目。 */ $temp = explode(".", $_FILES["file"]["name"]); //end() 函数将数组内部指针指向最后一个元素,并返回该元素的值(如果成功)。 $extension = end($temp); if ((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/jpg") || ($_FILES["file"]["type"] == "image/pjpeg") || ($_FILES["file"]["type"] == "image/x-png") || ($_FILES["file"]["type"] == "image/png")) && ($_FILES["file"]["size"] < 200000) /* * in_array(value,array,type) * in_array() 函数在数组中搜索给定的值。 * 如果给定的值 value 存在于数组 array 中则返回 true。如果第三个参数设置为 true,函数只有在元素存在于数组中且数据类型与给定值相同时才返回 true。 如果没有在数组中找到参数,函数返回 false。 注释:如果 value 参数是字符串,且 type 参数设置为 true,则搜索区分大小写。 * * move_uploaded_file() 函数将上传的文件移动到新位置。 * move_uploaded_file(file,newloc) * 本函数检查并确保由 file 指定的文件是合法的上传文件(即通过 PHP 的 HTTP POST 上传机制所上传的)。如果文件合法,则将其移动为由 newloc 指定的文件。*/ && in_array($extension, $allowedExts)) { if ($_FILES["file"]["error"] > 0) { echo "Return Code: " . $_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 "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>"; if (file_exists("upload/" . $_FILES["file"]["name"])) { echo $_FILES["file"]["name"] . " already exists. "; } else { move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]); echo "Stored in: " . "upload/" . $_FILES["file"]["name"]; } } } else { echo "Invalid file"; } ?>
It mainly uses three main functions: explode(), end(), and in_array(). I have commented and explained their usage. They are all easy to understand. You see, how simple it is to upload a file using PHP. You need One thing to note is that after the file is uploaded, it is stored in the temporary directory by default. This directory can be accessed through $_FILES['myFile']['tmp_name']. At this time, you must remove it from the temporary directory. Delete or move it elsewhere, or if not, it will be deleted. That is, regardless of whether the upload is successful or not, the files in the temporary directory will definitely be deleted after the script is executed. Therefore, in this example, the move_uploaded_files() function is used to copy it to another location. At this time, the file upload process is completed.
Finally, if necessary, you can still download the source code, although it is very simple.

C++局部变量和全局变量的区别:可见性:局部变量仅限于定义函数,而全局变量在整个程序中可见。内存分配:局部变量在栈上分配,而全局变量在全局数据区分配。作用域:局部变量在函数内,而全局变量在整个程序中。初始化:局部变量在函数调用时初始化,而全局变量在程序启动时初始化。重新创建:局部变量在每次函数调用时重新创建,而全局变量仅在程序启动时创建。

JSP注释的分类及用法解析JSP注释分为两种:单行注释:以结尾,只能注释单行代码。多行注释:以/*开头,以*/结尾,可以注释多行代码。单行注释示例多行注释示例/**这是一段多行注释*可以注释多行代码*/JSP注释的用法JSP注释可以用来注释JSP代码,使其更易于阅

go语言没有静态全局变量,它使用了一种更为灵活的方式来处理全局变量的需求,全局变量通常是在包级别被声明,也就是在函数外部声明的变量,这些全局变量在整个包中均是可见的,可以在包中的任何函数中使用。

request的中文意思为“请求”,是php中的一个全局变量,是一个包含了“$_POST”、“$_GET”和“$_COOKIE”的数组。“$_REQUEST”变量可以获取POST或GET方式提交的数据、COOKIE信息。

c语言exit函数怎么用,需要具体代码示例在C语言中,我们常常需要在程序中提前终止程序的执行,或者在某个特定的条件下退出程序。C语言提供了exit()函数来实现这个功能。本文将介绍exit()函数的用法,并提供相应的代码示例。exit()函数是C语言中的标准库函数,它包含在头文件中。它的作用是终止程序的执行,并且可以带一个整型

Python函数介绍:abs函数的用法和示例一、abs函数的用法介绍在Python中,abs函数是一个内置函数,用于计算给定数值的绝对值。它可以接受一个数字参数,并返回该数字的绝对值。abs函数的基本语法如下:abs(x)其中,x是要计算绝对值的数值参数,可以是整数或浮点数。二、abs函数的示例下面我们将通过一些具体的示例来展示abs函数的用法:示例1:计算

Python函数介绍:isinstance函数的用法和示例Python是一门功能强大的编程语言,提供了许多内置函数,使得编程变得更加方便和高效。其中一个非常有用的内置函数是isinstance()函数。本文将介绍isinstance函数的用法和示例,并提供具体的代码示例。isinstance()函数用于判断一个对象是否是指定的类或类型的实例。该函数的语法如下

随着JavaScript的流行,越来越多的网站和应用程序都依赖于JavaScript。然而,JavaScript中全局变量的使用可能存在安全问题。在此文中,我将介绍如何在JavaScript中实现全局变量的安全性。避免使用全局变量最好的方法是避免使用全局变量。在JavaScript中,所有变量都默认为全局变量,除非它们在函数中声明。因此,应尽可能使用局部变量


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

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 Chinese version
Chinese version, very easy to use

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Linux new version
SublimeText3 Linux latest version