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

PHP file upload main code explanation_php basics

May 16, 2016 am 09:00 AM
php file upload

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 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
4 weeks 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 file upload tutorial: How to upload files using HTML forms PHP file upload tutorial: How to upload files using HTML forms Jun 11, 2023 am 08:10 AM

PHP file upload tutorial: How to use HTML forms to upload files In the process of website development, the file upload function is a very common requirement. As a popular server scripting language, PHP can implement the file upload function very well. This article will introduce in detail how to use HTML forms to complete file uploads. 1. HTML form First, we need to use an HTML form to create a file upload page. In the HTML form, the enctype attribute needs to be set to "multipart/form-

How to handle file upload in PHP? How to handle file upload in PHP? May 11, 2023 pm 10:31 PM

With the continuous development of Internet technology, the file upload function has become an essential part of many websites. In the PHP language, we can handle file uploads through some class libraries and functions. This article will focus on the file upload processing method in PHP. 1. Form settings In the HTML form, we need to set the enctype attribute to "multipart/form-data" to support file upload. The code is as follows: &lt;formaction="upload.

PHP file upload security guide: How to use the $_FILES array to obtain uploaded file information PHP file upload security guide: How to use the $_FILES array to obtain uploaded file information Jul 30, 2023 pm 06:53 PM

PHP file upload security guide: How to use the $_FILES array to obtain uploaded file information Summary: File upload is one of the common functions in web development. However, incorrect file upload implementation can lead to security vulnerabilities, bringing potential risks to the application. This article will introduce how to use PHP's $_FILES array to safely obtain uploaded file information, and combine it with some code examples to help readers better understand. To set appropriate file upload limits in PHP, we can use the php.ini file to

Tips for handling PHP file upload encoding errors and generating corresponding error prompts Tips for handling PHP file upload encoding errors and generating corresponding error prompts Aug 06, 2023 am 09:51 AM

Tips for handling PHP file upload encoding errors and generating corresponding error prompts. File upload is a very common requirement when developing web applications. When processing PHP file uploads, we often encounter encoding errors. This article will introduce some techniques for handling PHP file upload encoding errors and generating corresponding error prompts. In PHP, file uploads are accessed through the $_FILES global variable. You can obtain the name, size, temporary file path and other information of the uploaded file through $_FILES

How to write a simple file upload and download function via PHP How to write a simple file upload and download function via PHP Sep 24, 2023 am 08:12 AM

How to write a simple file upload and download function through PHP. With the development of Internet technology, file upload and download functions have become one of the necessary functions for many websites. By writing a simple file upload and download function, users can easily upload and download files and improve user experience. This article will introduce how to use PHP to write a simple file upload and download function, and provide specific code examples. 1. Implementation of the file upload function. Create a form. First, create a form on the HTML page for users to upload files.

How to use PHP to develop file upload and download modules in CMS How to use PHP to develop file upload and download modules in CMS Jun 21, 2023 pm 12:13 PM

With the continuous development of the Internet, more and more websites require file upload and download functions. As an open source server-side scripting language, PHP has a wide range of application scenarios and industry recognition. CMS (Content Management System) is one of our common website types. This article will discuss how to use PHP to develop the file upload and download modules in CMS. 1. File upload module 1. Basic principle of uploading files The basic principle of file upload is to transfer files from the client

PHP file upload methods and summary of frequently asked questions PHP file upload methods and summary of frequently asked questions Jun 08, 2023 pm 09:27 PM

Summary of PHP file upload methods and frequently asked questions File upload is one of the common functions in web development and can be used for users to upload avatars, files, etc. PHP provides a convenient file upload processing method. This article will introduce the PHP file upload method and a summary of common problems in detail. 1. PHP file upload method HTML form To implement file upload, you need to use an HTML form, in which the enctype attribute needs to be set to "multipart/form-data", so that the browser

How to use PHP to implement a simple online file upload and download system How to use PHP to implement a simple online file upload and download system Sep 24, 2023 pm 03:13 PM

How to use PHP to implement a simple online file upload and download system requires specific code examples. In the Internet age, file transfer and sharing are very common needs. Whether you are an individual or a business, you need to handle uploading and downloading files conveniently and quickly. As a powerful server-side scripting language, PHP provides a wealth of functions and tools to easily implement file upload and download functions. This article will introduce how to use PHP to implement a simple online file upload and download system, and provide detailed code examples.

See all articles