PHP文件上传实现代码详解
PHP文件上传实现代码详解
上传文件功能由两个部分组成,HTML页面和PHP处理部分。
HTML页面主要是让用户选择所要上传的文件,php部分让我们可以把文件存储到服务器的指定目录。
一.HTML部分upload.html
<p><html> </p><p> <head> <meta content="text/html; charset=utf-8" http-equiv="Content-Type"> </head> </p><p><body> 上传Demo: </p><p><form action="upload.php" method="post" enctype="multipart/form-data"> </p><p><input type="file" name="img" /> </p><p><input type="submit" name="submit" value="上传" /> </p><p></form> </p><p></body></p><p></html></p>
说明:
1.Input标签中type="file",表明把输入作为文件来处理。
2.Enctype规定了在提交这个表单时要使用哪种内容类型。在表单需要二进制数据时,比如文件内容,请使用"multipart/form-data",如果要上传文件,这个属性是必要的。
二.php部分upload.php
<p><?php</p><p>$DST_DIR = '/data/upload/';if ($_FILES['img']['name'] != '') { </p><p>if ($_FILES['img']['error'] > 0) {</p><p> echo "上传失败";</p><p> } else { </p><p> if (move_uploaded_file($_FILES['img']['tmp_name'], $DST_DIR.$_FILES['img']['name'])) {</p><p> echo "上传成功"; </p><p> } else { </p><p> echo "上传失败"; </p><p> } }}else {</p><p> echo "请上传文件";</p><p>}</p>
PHP文件上传实现代码说明:
1. 全局变量$_FILE
此数组包含有所有上传的文件信息。
以我们假设文件上传字段的名称如上例所示,为 img。则
$_FILES['img']['name']
客户端上传的文件的原名称。
$_FILES['img']['type']
文件的 MIME 类型,如果浏览器提供此信息的话。一个例子是“image/gif”。不过此 MIME 类型在 PHP 端并不检查,因此不要想当然认为有这个值。$_FILES['img']['size']:已上传文件的大小,单位为字节。
$_FILES['img']['size']
已上传文件的大小,单位为字节。
$_FILES['img']['tmp_name']
文件被上传后在服务端储存的临时文件名。
$_FILES['img']['error']
和该文件上传相关的错误代码。
2. 关于错误码
$_FILES['img']['error']有以下几种类型
UPLOAD_ERR_OK
其值为 0,没有错误发生,文件上传成功。
UPLOAD_ERR_INI_SIZE
其值为 1,上传的文件超过了 php.ini 中 upload_max_filesize选项限制的值。
UPLOAD_ERR_FORM_SIZE
其值为 2,上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值。
UPLOAD_ERR_PARTIAL
其值为 3,文件只有部分被上传。
UPLOAD_ERR_NO_FILE
其值为 4,没有文件被上传。
UPLOAD_ERR_NO_TMP_DIR
其值为 6,找不到临时文件夹。PHP 4.3.10 和 PHP 5.0.3 引进。
UPLOAD_ERR_CANT_WRITE
其值为 7,文件写入失败。PHP 5.1.0 引进。
3.move_uploaded_file
文件被上传后,默认地会被储存到服务端的默认临时目录中(除非 php.ini 中的 upload_tmp_dir设置为其它的路径),文件名是随机的。如果该文件没有被移动到其它地方也没有被改名,则该文件将在表单请求结束时被删除。因此需要通过move_uploaded_file移动临时文件。
经实验copy也能完成move_uploaded_file的功能,为啥要用move_uploaded_file呢?
有说法是move_uploaded_file会对上传文件做一些检查,防止copy引起的一些安全漏洞。但具体copy会带来什么问题呢?
Anyway,既然php给了特定的函数,必然有一定道理,先这么用吧。
三.PHP文件上传安全检查
可以考虑通过$_FILES['img']['size']和$_FILES['img']['type']对上传的文件做一些安全检查,比如限定上传类型,上传文件的大小等。

Hot AI Tools

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

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

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

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



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

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 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). 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

How to use PHP to implement FTP file upload progress bar 1. Background introduction In website development, file upload is a common function. For the upload of large files, in order to improve the user experience, we often need to display an upload progress bar to the user to let the user know the file upload process. This article will introduce how to use PHP to implement the FTP file upload progress bar function. 2. The basic idea of implementing the progress bar of FTP file upload. The progress bar of FTP file upload is usually calculated by calculating the size of the uploaded file and the size of the uploaded file.

PHP file upload guide: How to use the move_uploaded_file function to handle uploaded files In developing web applications, file upload is a common requirement. PHP provides a convenient function move_uploaded_file() for processing uploaded files. This article will introduce you how to use this function to implement the file upload function. 1. Preparation Before starting, make sure that your PHP environment has been configured with file upload parameters. You can do this by opening php.in

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,

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.
