Home Backend Development PHP Tutorial Submit files in PHP

Submit files in PHP

May 24, 2023 am 08:51 AM
File Upload form processing php file operations

PHP is a server-side scripting language that can be used to process form data, generate dynamic pages, etc. One common application scenario is uploading files through forms. This article will introduce the file upload function in PHP, including the processing of upload forms, storage of uploaded files, limiting the type and size of uploaded files, etc.

1. Processing of upload forms

In HTML, you can create a form through the form tag, and use the type="file" attribute in the input tag to allow uploading files. In PHP, you can use the $_FILES global variable to obtain uploaded file information.

Suppose our form contains an input tag for uploading files and a submit button:



There are a few things to note here:

  1. The action attribute specifies the address for form submission, which is the processing script for uploading files. Here we use upload.php file.
  2. The method attribute specifies the form submission method, here the post method is used.
  3. The enctype attribute specifies the encoding method of form data. We need to use multipart/form-data to allow uploading files.

Next, handle the file upload request in the upload.php file.

2. Storage of uploaded files

First you need to determine whether the uploaded file exists. If it exists, you can obtain the relevant information of the uploaded file through the $_FILES array:

if( isset($_FILES["fileToUpload"])) {

$file_name = $_FILES["fileToUpload"]["name"];
$file_tmp = $_FILES["fileToUpload"]["tmp_name"];
$file_size = $_FILES["fileToUpload"]["size"];
Copy after login

}

Among them, the file name can be obtained through the $name attribute, the file size can be obtained through the $size attribute, and the temporary path of the file Can be obtained through the $tmp_name attribute.

Next, you need to move the uploaded file to the target folder. Here use the move_uploaded_file() function:

$target_dir = "uploads/";
$target_file = $target_dir. basename($file_name);
if(move_uploaded_file($file_tmp, $target_file)) {

echo "The file ". basename($file_name). " has been uploaded.";
Copy after login

} else{

echo "Sorry, there was an error uploading your file.";
Copy after login

}

In the above code, $target_dir is the storage directory of the uploaded file, where the basename() function is used to obtain the file name. Moving files uses the move_uploaded_file() function, where the first parameter is the temporary path of the file, and the second parameter is the target path. If the file is moved successfully, a successful upload message is returned to the user, otherwise a failed upload message is returned.

3. Limit the type and size of uploaded files

In order to ensure the security and performance of the uploading system, we need to limit uploaded files. This includes both file type and file size.

  1. File type restrictions

We use PHP's in_array() function to determine whether the type of uploaded file is legal. Specifically, you need to define an array of file types that are allowed to be uploaded, and then use the in_array() function to determine whether the uploaded file type is in the array of file types that are allowed to be uploaded.

$allowed_types = array('jpg', 'jpeg', 'png', 'gif');
$file_type = strtolower(pathinfo($file_name,PATHINFO_EXTENSION));
if( in_array($file_type, $allowed_types)){

// 合法文件类型
Copy after login

} else{

echo "Sorry, only JPG, JPEG, PNG, GIF files are allowed.";
Copy after login

}

In the above code, the pathinfo() function is used to obtain the file suffix name , the strtolower() function converts it to lowercase characters, and the in_array() function determines whether it is in the array of file types that are allowed to be uploaded.

  1. File size limit

We use PHP's file upload configuration to limit the size of uploaded files, which can be set in the php.ini file, for example:

post_max_size=8M
upload_max_filesize=2M

The post_max_size here represents the maximum length of POST data, and upload_max_filesize represents the maximum size of the uploaded file. It should be noted that if the file size does not meet the limit, the $_FILES array will not contain information about the uploaded file.

Summary:

Implementing the file upload function in PHP requires the following steps:

  1. Create a form containing the upload file input tag and submit button.
  2. In the processing script for uploading files, use the $_FILES global variable to obtain the uploaded file information.
  3. Move the uploaded files to the target folder.
  4. Enforce security and performance restrictions on uploaded files, including file type and file size.

Implementing the file upload function through the above method can provide our web applications with richer functions and better user experience.

The above is the detailed content of Submit 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

Video Face Swap

Video Face Swap

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

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)

Implement file upload and download in Workerman documents Implement file upload and download in Workerman documents Nov 08, 2023 pm 06:02 PM

To implement file upload and download in Workerman documents, specific code examples are required. Introduction: Workerman is a high-performance PHP asynchronous network communication framework that is simple, efficient, and easy to use. In actual development, file uploading and downloading are common functional requirements. This article will introduce how to use the Workerman framework to implement file uploading and downloading, and give specific code examples. 1. File upload: File upload refers to the operation of transferring files on the local computer to the server. The following is used

PHP form processing: form reset and data clearing PHP form processing: form reset and data clearing Aug 07, 2023 pm 03:05 PM

PHP form processing: form reset and data clearing In web development, forms are a very important part and are used to collect data entered by users. After the user submits the form, we usually process the form data and perform some necessary operations. However, in actual development, we often encounter situations where we need to reset the form or clear the form data. This article will introduce how to use PHP to implement form reset and data clearing functions, and provide corresponding code examples. Form reset First, we need to understand the concept of form reset. when user

PHP form processing: form data sorting and ranking PHP form processing: form data sorting and ranking Aug 09, 2023 pm 06:01 PM

PHP form processing: form data sorting and ranking In web development, forms are a common user input method. After we collect form data from users, we usually need to process and analyze the data. This article will introduce how to use PHP to sort and rank form data to better display and analyze user-submitted data. 1. Form data sorting When we collect form data submitted by users, we may find that the order of the data does not necessarily meet our requirements. For those that need to be displayed or divided according to specific rules

How to use Laravel to implement file upload and download functions How to use Laravel to implement file upload and download functions Nov 02, 2023 pm 04:36 PM

How to use Laravel to implement file upload and download functions Laravel is a popular PHP Web framework that provides a wealth of functions and tools to make developing Web applications easier and more efficient. One of the commonly used functions is file upload and download. This article will introduce how to use Laravel to implement file upload and download functions, and provide specific code examples. File upload File upload refers to uploading local files to the server for storage. In Laravel we can use file upload

How to use gRPC to implement file upload in Golang? How to use gRPC to implement file upload in Golang? Jun 03, 2024 pm 04:54 PM

How to implement file upload using gRPC? Create supporting service definitions, including request and response messages. On the client, the file to be uploaded is opened and split into chunks, then streamed to the server via a gRPC stream. On the server side, file chunks are received and stored into a file. The server sends a response after the file upload is completed to indicate whether the upload was successful.

How to solve Java file upload exception (FileUploadException) How to solve Java file upload exception (FileUploadException) Aug 18, 2023 pm 12:11 PM

How to solve Java file upload exception (FileUploadException). One problem that is often encountered in web development is FileUploadException (file upload exception). It may occur due to various reasons such as file size exceeding limit, file format mismatch, or incorrect server configuration. This article describes some ways to solve these problems and provides corresponding code examples. Limit the size of uploaded files In most scenarios, limit the file size

File Uploading and Processing in Laravel: Managing User Uploaded Files File Uploading and Processing in Laravel: Managing User Uploaded Files Aug 13, 2023 pm 06:45 PM

File Uploading and Processing in Laravel: Managing User Uploaded Files Introduction: File uploading is a very common functional requirement in modern web applications. In the Laravel framework, file uploading and processing becomes very simple and efficient. This article will introduce how to manage user-uploaded files in Laravel, including verification, storage, processing, and display of file uploads. 1. File upload File upload refers to uploading files from the client to the server. In Laravel, file uploads are very easy to handle. first,

Simplify file upload processing with Golang functions Simplify file upload processing with Golang functions May 02, 2024 pm 06:45 PM

Answer: Yes, Golang provides functions that simplify file upload processing. Details: The MultipartFile type provides access to file metadata and content. The FormFile function gets a specific file from the form request. The ParseForm and ParseMultipartForm functions are used to parse form data and multipart form data. Using these functions simplifies the file processing process and allows developers to focus on business logic.

See all articles