Table of Contents
php实现文件上传与下载(上)
Home php教程 php手册 php实现文件上传与下载(上)

php实现文件上传与下载(上)

Jun 13, 2016 am 08:52 AM
php generally superior upload download and Function Basic accomplish document yes of website

php实现文件上传与下载(上)

php实现文件的上传与下载是一个挺基本的功能,一般网站多多少少都会有这样的需求在内,当然不是说所有的文件都可以被上传,那这网络就太没有安全性可言了。因为接触php时间不长,今天写练练手,随笔也就是公开记录而已啦。

 

文件上传:也就是将客户端的文件上传到服务器端,在将服务器端的临时文件移动到指定目录即可。主要思维就是先定义获取$_FILES中的二维数组变量,可以每次都用二维数组获取,也可以将第一维的放入一个数组变量中,然后每次获取该数组变量中的值,简化代码量----> 判断错误号是否为0或者是UPLOAD_ERR_OK为真时表示没有错误,可以上传----->将服务器上的临时文件移动到我们的指定目录下,叫什么名字,移动成功返回true,用move_uploaded_file($临时文件名,$目的地)----->另外一种方式就是用copy($临时变量名,$目的地)函数;

 

$_FILES中保存这儿上传文件的信息,这是一个二维数组,第一维当然就是上传进来的文件的名称了,第二维就是该文件的相关信息了,上传之后就可以打印该数组中的参数进行查看---用print_r($_FILES),可以看到里边的内容有:

name:上传文件的名称;

type:上传文件的MIME类型;

tmp_name:上传到服务器上的临时文件名;//很重要的,因为以后在服务器上操纵的就是这个文件

size:上传文件的大小;

error:上传文件的错误号,成功为0,。

服务器端配置---php.ini:

 

file_uploads=on,支持http上传;

upload_tmp_dir=,临时文件保存的目录;

upload_max_filesize=  2M ;允许上传文件的大小;

max_file_uploads=20; 允许一次上传文件数目,默认为20;

max_execution_time=-1;设置脚本被解析终止之前允许的最大执行秒数,防止程序太差(例如死循环)而占尽服务器资源;

max_input_time=60;脚本解析输入数据允许的最大秒数;

max_input_nesting_level=64,设置输入变量的嵌套深度;

max_input_vars=60;接受多少输入的变量,减轻dos攻击的可能性,如果超过指定数量的变量,将会导致E_WARNING的产生,更多的输入变量将会从请求中截断;

memory_limit=128M。最大单线程的独立内存使用量,也就是一个web请求,给予线程的最大内存使用量的定义。

还有一些错误信息说明,基本都是英文的字面意思,找不见临时目录啊大小超过限制啊之类的共8中错误信息,分别对应数字0-8但是没有数字5,在代码里进行判定输出,按照一一对应的,可以用switch……case,共7个case(0表示成功就不用写了)来表示输出错误信息。在代码中进行判定,如果没错误输出的话,就进行上传操作。

 

客户端的一些配置---但是在客户端的配置很多时候并不能真正起到限制作用,真正想限制都是要配置在服务端的:

 

通过表单隐藏域限制上传文件的最大值    

通过accept属性限制上传文件类型  

在这个练手的过程中,我没有认识到一点,form表单最大上传限制(程序中的判定)和底层php的判定(php.ini中),因此如果我表单的最大上传限制为2M,而php.ini中的判定是10M,这样在我上传文件大小超过10M时肯定就直接提示代码错误,而当上传文件大小介于2M和10M之间时,程序可以执行,按照判定输出对应的错误信息。

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 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 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles