


PHP file upload tutorial: How to upload files using HTML forms
Jun 11, 2023 am 08:10 AMPHP 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. The enctype attribute needs to be set to "multipart/form-data" in the HTML form to allow file upload. The specific code is as follows:
<form action="upload.php" method="post" enctype="multipart/form-data"> <input type="file" name="fileToUpload" id="fileToUpload"> <input type="submit" value="上傳文件" name="submit"> </form>
This form contains a file selection box and a submit button. The user can select the file to be uploaded, and then click the submit button to upload.
2. PHP file upload processing
Next, we need to create a PHP file to process file upload. The specific code is as follows:
<?php $target_dir = "uploads/"; $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]); $uploadOk = 1; $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION)); // 檢查文件是否是真正的文件類型 if(isset($_POST["submit"])) { $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]); if($check !== false) { echo "文件是一個(gè)" . $check["mime"] . "文件。"; $uploadOk = 1; } else { echo "文件不是一個(gè)圖片文件。"; $uploadOk = 0; } } // 檢查文件是否已經(jīng)存在 if (file_exists($target_file)) { echo "對(duì)不起,文件已存在。"; $uploadOk = 0; } // 檢查文件大小 if ($_FILES["fileToUpload"]["size"] > 500000) { echo "對(duì)不起,文件太大。"; $uploadOk = 0; } // 允許特定文件格式上傳 if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" ) { echo "對(duì)不起,只允許JPG, JPEG, PNG & GIF 的文件上傳。"; $uploadOk = 0; } // 如果文件上傳成功,將文件從臨時(shí)目錄移動(dòng)到上傳目錄 if ($uploadOk == 0) { echo "對(duì)不起,您的文件未上傳。"; } else { if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) { echo "文件" . basename( $_FILES["fileToUpload"]["name"]). "上傳成功。"; } else { echo "對(duì)不起,上傳過程中發(fā)生了錯(cuò)誤。"; } } ?>
In this PHP file, we first need to specify the target folder for file upload, here we specify it as the uploads folder. Next, we need to do some checking and verification of file uploads. For example, we need to check whether the file is a real file type, whether the file already exists, whether the file size is appropriate, etc. If the file passes all checks and validations, we can move the file from the temporary directory to the upload directory.
3. Notes
When using PHP to upload files, you need to pay attention to the following issues:
1. The server must have the target folder with write permission.
2.enctype attribute must be set to "multipart/form-data" so that file upload can be supported.
3. The type attribute of the input element must be set to "file" so that the file selection box can be created.
4.PHP only allows the maximum upload file size to be 2MB by default, which can be modified by modifying the upload_max_filesize and post_max_size parameters in the php.ini file.
5.PHP only allows uploading some common file types by default, such as JPG, PNG, GIF, etc. You can add other file types by modifying the upload_file_types parameter in the php.ini file.
4. Summary
File upload is a very common requirement in website development. This article introduces the methods of HTML form and PHP file upload processing, hoping to help readers better implement the file upload function. . When uploading files, you need to pay attention to security issues to avoid problems such as malicious file uploads and file injection. At the same time, it is also recommended that readers learn more about file uploading in order to better deal with various file uploading problems.
The above is the detailed content of PHP file upload tutorial: How to upload files using HTML forms. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

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

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

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Dewu APP is currently a very popular brand shopping software, but most users do not know how to use the functions in Dewu APP. The most detailed usage tutorial guide is compiled below. Next is the Dewuduo that the editor brings to users. A summary of function usage tutorials. Interested users can come and take a look! Tutorial on how to use Dewu [2024-03-20] How to use Dewu installment purchase [2024-03-20] How to obtain Dewu coupons [2024-03-20] How to find Dewu manual customer service [2024-03-20] How to check the pickup code of Dewu [2024-03-20] Where to find Dewu purchase [2024-03-20] How to open Dewu VIP [2024-03-20] How to apply for return or exchange of Dewu

After rain in summer, you can often see a beautiful and magical special weather scene - rainbow. This is also a rare scene that can be encountered in photography, and it is very photogenic. There are several conditions for a rainbow to appear: first, there are enough water droplets in the air, and second, the sun shines at a low angle. Therefore, it is easiest to see a rainbow in the afternoon after the rain has cleared up. However, the formation of a rainbow is greatly affected by weather, light and other conditions, so it generally only lasts for a short period of time, and the best viewing and shooting time is even shorter. So when you encounter a rainbow, how can you properly record it and photograph it with quality? 1. Look for rainbows. In addition to the conditions mentioned above, rainbows usually appear in the direction of sunlight, that is, if the sun shines from west to east, rainbows are more likely to appear in the east.

PhotoshopCS is the abbreviation of Photoshop Creative Suite. It is a software produced by Adobe and is widely used in graphic design and image processing. As a novice learning PS, let me explain to you today what software photoshopcs5 is and how to use photoshopcs5. 1. What software is photoshop cs5? Adobe Photoshop CS5 Extended is ideal for professionals in film, video and multimedia fields, graphic and web designers who use 3D and animation, and professionals in engineering and scientific fields. Render a 3D image and merge it into a 2D composite image. Edit videos easily

When using a computer to input English, sometimes we encounter the difference between full-width English letters and half-width English letters. Full-width English letters refer to the characters input by pressing the Shift key and the English letter key combination when the input method is Chinese mode. They occupy a full-width character width. Half-width English letters refer to characters input directly when the input method is English mode, and they occupy half a character width. In some cases, we may need to convert full-width English letters to half-width letters. Here is a simple tutorial: First, open a text editor or any

PHP Tutorial: How to Convert Int Type to String In PHP, converting integer data to string is a common operation. This tutorial will introduce how to use PHP's built-in functions to convert the int type to a string, while providing specific code examples. Use cast: In PHP, you can use cast to convert integer data into a string. This method is very simple. You only need to add (string) before the integer data to convert it into a string. Below is a simple sample code

1. First open WeChat. 2. Click [+] in the upper right corner. 3. Click the QR code to collect payment. 4. Click the three small dots in the upper right corner. 5. Click to close the voice reminder for payment arrival.

With the continuous development of smart phones, the functions of mobile phones have become more and more powerful, among which the function of taking long pictures has become one of the important functions used by many users in daily life. Long screenshots can help users save a long web page, conversation record or picture at one time for easy viewing and sharing. Among many mobile phone brands, Huawei mobile phones are also one of the brands highly respected by users, and their function of cropping long pictures is also highly praised. This article will introduce you to the correct method of taking long pictures on Huawei mobile phones, as well as some expert tips to help you make better use of Huawei mobile phones.

In daily shooting, many people encounter this situation: the photos on the camera seem to be exposed normally, but after exporting the photos, they find that their true form is far from the camera's rendering, and there is obviously an exposure problem. Affected by environmental light, screen brightness and other factors, this situation is relatively normal, but it also brings us a revelation: when looking at photos and analyzing photos, you must learn to read histograms. So, what is a histogram? Simply understood, a histogram is a display form of the brightness distribution of photo pixels: horizontally, the histogram can be roughly divided into three parts, the left side is the shadow area, the middle is the midtone area, and the right side is the highlight area; On the left is the dead black area in the shadows, while on the far right is the spilled area in the highlights. The vertical axis represents the specific distribution of pixels
