search
Homephp教程php手册PHP file upload main code explanation_php basics

Copy code The code is as follows:

if($_FILES[ 'myfile']['name'] != '') {
if($_FILES['myfile']['error'] > 0) {
echo "Error status:" . $_FILES[' myfile']['error'];
} else {
move_uploaded_file($_FILES['myfile']['tmp_name'], "uploads/" . $FILES['myfile']['name'] );
echo "<script>alert(Upload successful!);</script>";
}
} else{
echo "<script>alert(Please upload the file!);</script>";
}
?>

Description:

Before explaining this code, we need to understand the following knowledge.

$_FILES['myfile']['name'] refers to the name of the uploaded file
$_FILES['myfile']['type'] refers to the type of the uploaded file
$_FILES[ 'myfile']['size'] refers to the size of the uploaded file, in bytes (B)
$_FILES['myfile']['tmp_name'] refers to the temporary copy of the uploaded file stored in the server The file name will be automatically destroyed after the file is moved to the specified directory.
$_FILES['myfile']["error"] refers to the status code of errors that may occur during file upload. The meaning of each status will be explained later.

Understanding this, let’s take a look at the code of upload.php.

First of all, myfile in $_FILES['myfile']['name'] refers to the name value of the file tag uploaded in the above HTML page. Based on this, we can know which input submitted the file we are processing. of.

Then let’s check whether $_FILES['myfile']['name'] is empty. Based on this, we can know whether the user has uploaded files and perform different operations.

If the file is uploaded and the status is 0, it means the upload is successful. We can use the move_uploaded_file method to store the uploaded file in the specified directory. The above example refers to moving the uploaded file to the uploads folder in the same directory. , this path is relative to the directory relative to the PHP file (upload.php).

For example, if we want to move the uploaded file to a folder called user one level above upload.php, we can write like this: move_uploaded_file ($_FILES['myfile']['tmp_name'], ". /user/". $FILES['myfile']['name']), this method is very convenient and flexible to use. In this way, a file is uploaded to the server, and you can open the directory in the server to view the file.

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 Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function