第八节 文件上传_PHP
7. 文件上传
你可以利用PHP实现文件的上传功能,注意客户端的浏览器应该是Netscape3以上或者IE3以上的版本。同时,因为本程序与你的PHP配置文件(PHP3为php3.ini,PHP4为php.in)设置有关。在执行该程序之前请先检查您的PHP配置文件有没有做好如下的设置:
将;upload_tmp_dir该行的注释符,即前面的分号“;”去掉,使该行在php.ini文档中起作用。upload_tmp_dir是用来定义上传文件存放的临时路径,在这里你还可以给其定义一个绝对路径,例如:upload_tmp_dir = d:upload 当然,此时你的d:upload目录必须有读写权限。
如果你在你的.php3程序里已经定义了上传的路径,此时上传文件的路径以.php3程序里定义的路径为基准。在下例中,receiver.php3文件就指定了用于存放上传文件的目录是:d:upload。
upload_max_filesize 是用来限制PHP处理的上载文件大小的最大值,以字节计算,缺省值为2097152= 2*1024*1024字节(2兆),你可以通过修改该缺省值来定义最大的上载文件大小。
修改后不要忘了重启Apache,IIS或PWS服务哦。
同时在PHP中,文件上载还有几点是值得注意的:
1. 在form表单中要将method属性设为post,enctype属性设为multipart/form-data;
2. 在form表单中可以加一个hidden类型的input框,其中名字为 MAX_FILE_SIZE的隐藏值域,通过设置其VALUE可以限制上载文件的大小。当然,这个值不可能超过PHP的配置文件(PHP3为php3.ini,PHP4为php.ini)中的upload_max_filesize,注意这个input框一定要放在所有file类型的input框前面,否则也是无效的哦;
3. 在PHP程序运行完后,上传文件被放在了临时目录下。如果上传文件没有被改名或移动,那么在请求的最后该文件将自动被从临时文件夹中删除,所以我们最好立即将新的上传文件上传移到一个永久目录下或更改其文件名。
首先我们需要一个上载文件的表单网页(upload.htm):
处理上载文件的PHP文件(receiver.php3)
function do_upload ()
{
global $uploadfile, $uploadfile_size;
global $local_file, $error_msg;
if ( $uploadfile == "none" )
{
$error_msg = "对不起,你没有选定任何文件上传!";
return;
}
if ( $uploadfile_size > 2000000 )
{
$error_msg = "对不起,你要上传的文件太大了!";
return;
}
$the_time = time ();
// 在这里指定你用来存放上传文件的目录,你需要对以下目录有写权限
// 同时,我们也可以给上传文件指定另外的目录,如:$upload_dir = "/local/uploads";
$upload_dir = "d:/upload";
$local_file = "$upload_dir/$the_time";
if ( file_exists ( '$local_file' ) )
{
$seq = 1;
while ( file_exists ( "$upload_dir/$the_time$seq" ) ) { $seq++; }
$local_file = "$upload_dir/$the_time$seq";
};
rename ( $uploadfile, $local_file );
display_page ();
}
function display_page ()
{
// 这里是你的页面内容
}
?>
if ( $error_msg ) { echo "$error_msg
"; }
if ( $sendit )
{
do_upload ();
echo "文件上载成功!";
}
elseif ( $cancelit )
{
header ( "Location: $some_other_script" );
echo "文件上载失败!";
exit;
}
else
{
some_other_func ();
}
?>

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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





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

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

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

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,

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

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

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