search
Homephp教程php手册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):


Upload Your File


ENCTYPE="multipart/form-data" METHOD=POST>
NAME="MAX_FILE_SIZE" VALUE="2000000">
NAME="uploadfile" SIZE="24" MAXLENGTH="80">



NAME="sendit">
NAME="cancelit">





处理上载文件的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 ()
{
// 这里是你的页面内容
}
?>


php3 Receiving Script



if ( $error_msg ) { echo "$error_msg

"; }
if ( $sendit )
{
do_upload ();
echo "文件上载成功!";
}
elseif ( $cancelit )
{
header ( "Location: $some_other_script" );
echo "文件上载失败!";
exit;
}
else
{
some_other_func ();
}
?>




Statement
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

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor