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

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)

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,

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

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

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.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

Explain the match expression (PHP 8 ) and how it differs from switch. Explain the match expression (PHP 8 ) and how it differs from switch. Apr 06, 2025 am 12:03 AM

In PHP8, match expressions are a new control structure that returns different results based on the value of the expression. 1) It is similar to a switch statement, but returns a value instead of an execution statement block. 2) The match expression is strictly compared (===), which improves security. 3) It avoids possible break omissions in switch statements and enhances the simplicity and readability of the code.

See all articles