Home php教程 php手册 php 上传功能实例代码

php 上传功能实例代码

Jun 13, 2016 pm 12:17 PM
enctype form php upload code Function copy Example form

1.上传表单 upload.html

复制代码 代码如下:




  



1.注意
这是一个标签,我们要实现文件的上传,必须指定为multipart/form-data,否则服务器将不知道要干什么。
2.值得注意的是文件upload.html中表单选项 MAX_FILE_SIZE 的隐藏值域,通过设置其Value(值)可以限制上载文件的大小。
3.MAX_FILE_SIZE 的值只是对浏览器的一个建议,实际上它可以被简单的绕过。因此不要把对浏览器的限制寄希望于该值。实际上,PHP 设置中的上传文件最大值,是不会失效的。但是最好还是在表单中加上 MAX_FILE_SIZE,因为它可以避免用户在花时间等待上传大文件之后才发现该文件太大了的麻烦。

PHP上传文件涉及到的参数

复制代码 代码如下:


$f=&$HTTP_POST_FILES['Myfile'];
$dest_dir='uploads';//设定上传目录
$dest=$dest_dir.'/'.date("ymd")."_".$f['name'];//设置文件名为日期加上文件名避免重复
$r=move_uploaded_file($f['tmp_name'],$dest);
chmod($dest, 0755);//设定上传的文件的属性


或者

======================================================================== 以上范例中 $_FILES 数组的内容如下所示。我们假设文件上传字段的名称为 userfile(名称可随意命名)

复制代码 代码如下:


$_FILES['userfile']['name'] 客户端机器文件的原名称。
$_FILES['userfile']['type'] 文件的 MIME 类型,需要浏览器提供该信息的支持,例如“image/gif”。
$_FILES['userfile']['size'] 已上传文件的大小,单位为字节。
$_FILES['userfile']['tmp_name'] 文件被上传后在服务端储存的临时文件名。
$_FILES['userfile']['error'] 和该文件上传相关的错误代码


值:0; 没有错误发生,文件上传成功。
值:1; 上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值。
值:2; 上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值。
值:3; 文件只有部分被上传。
值:4; 没有文件被上传。

PHP默认的上传限定是最大2M,想上传超过此设定的文件,需要调整PHP、apache等的一些参数. 下面,我们简要介绍一下PHP文件上传涉及到的一些参数:

file_uploads
是否允许通过HTTP上传文件的开关,默认为ON即是开

upload_tmp_dir
upload_tmp_dir用来说明PHP上传的文件放置的临时目录,要想上传文件,得保证服务器没有关闭临时文件和有对文件夹的写权限,如果未指定则PHP使用系统默认值

upload_max_filesize
允许上传文件大小的最大值,默认为2M

复制代码 代码如下:


define('MUILTI_FILE_UPLOAD', '10'); //最多10个文件同时上传
define('MAX_SIZE_FILE_UPLOAD', '500000' ); //文件大小不超过5MB
define('FILE_UPLOAD_DIR', 'd:/'); //上传文件的目录
//允许上传的文件名
$array_extention_interdite = array( '.php' , '.php3' , '.php4' , '.exe' , '.msi' , '.htaccess' , '.gz' ); //上传文件的扩展名

//显示信息的公共函数
function func_message($message='', $ok=''){
echo '

';
if($ok == true){
echo '' ;
}
if($ok == false){
echo '';
}
echo '
  '.$message.'
  '.$message.'
';
}
//处理表单提交
$action = (isset($_POST['action'])) ? $_POST['action'] :'' ;
$file = (isset($_POST['file'])) ? $_POST['file'] :'' ;
if($file != '') {
$file = $file.'/';
}
$message_true = '';
$message_false = '';

switch($action){
case 'upload' :
chmod(FILE_UPLOAD_DIR,0777);
for($nb = 1 ; $nb if( $_FILES['file_'.$nb]['size'] >= 10 ){
if ($_FILES['file_'.$nb]['size'] if (!in_array(ereg_replace('^[[:alnum:]]([-_.]?[[:alnum:]])*\.' ,'.', $_FILES['file_'.$nb]['name'] ) , $array_extention_interdite) ){
if($_POST['file_name_'.$nb] !=''){
$file_name_final = $_POST['file_name_'.$nb].$extension ;
}else {
$file_name_final = $_FILES['file_'.$nb]['name'] ;
}
//文件名的修改
$file_name_final = strtr($file_name_final, 'aaaaaa', 'AAAAAACEEEEIIIIOOOOOUUUUYaaaaaaceeeeiiiioooooouuuuyy');
$file_name_final = preg_replace('/([^.a-z0-1]+)/i', '_', $file_ name_final );

$_FILES['file_'.$nb]['name'] = $file_name_final;
move_uploaded_file( $_FILES['file_'.$nb]['tmp_name'] , FILE_UPLOAD_ DIR . $file . $file_name_final );

$message_true .= '已经上传文件 : '.$_FILES['file_'.$nb]['name'] .'
';
}else{
$message_false .= '文件上传失败 : '.$_FILES['file_'.$nb]['name'] .'
';
}
}else{
$message_false .= '文件尺寸超过'.MAX_SIZE_FILE_UPLOAD/1000 . 'KB : " '.$_FILES['file_'.$nb]['tmp_name'].'"
';}
}
}//end for
break;
}
?>


多文件上传








if($message_true != '') { func_message($message_true, true); }
if($message_false != ''){ func_message($message_false, false); }
?>

for($nb = 1 ; $nb ?>

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)

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

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

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

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