Home >Backend Development >PHP Tutorial >phpmaster | Uploading Files with PHP
PHP file upload: build a safe and reliable upload system
Online album pictures, email attachments, and batch processing application data files have one thing in common: they all need to upload files to the Internet through the user's web browser. The file upload feature is an important part of many websites and web applications that are used every day. This article will show you how to add file upload support to your website using PHP.
Key Points
<form></form>
element must use the POST method and set the enctype property to multipart/form-data. $_FILES
. The move_uploaded_file()
function moves the uploaded file from a temporary location to a permanent location and performs additional checks to make sure the file is indeed uploaded via HTTP POST request. Prerequisites
Processing file upload is not difficult, but there are some small details that must be correct, otherwise the upload will fail. First, you need to make sure that PHP is configured to allow uploads. Check your php.ini file and verify that the file_uploads directive is set to On.
<code>file_uploads = On</code>
Uploaded files are first stored in a temporary directory (don't worry... your PHP script can then move files to a more permanent location afterward). By default, the initial location is the system's default temporary directory. You can specify different directories using the upload_tmp_dir directive in php.ini. In any case, you should verify that the PHP process has the appropriate permissions to write to the directory you are using.
<code>upload_tmp_dir = "/tmp" tboronczyk@zarkov:~$ ls -l / | grep tmp drwxrwxrwt 13 root root 40960 2011-08-31 00:50 tmp</code>
After confirming that the configuration allows the server to accept uploaded files, you can focus on HTML details. Like most other server-side interactions from HTML, uploading files uses forms. It is very important that your <form></form>
element must use the POST method and set the enctype property to multipart/form-data.
The above is the detailed content of phpmaster | Uploading Files with PHP. For more information, please follow other related articles on the PHP Chinese website!