PHP上传文件示例程序代码(适合初学者)
创建一个文件上传表单,允许用户从表单上传文件是非常有用的,请看下面这个上传文件的HTML 表单,代码如下:
< html > < body > < form action = "upload_file.php" method = "post" enctype = "multipart/form-data" > < label for = "attach_file" > Filename: </ label > < input type = "file" name = "attach_file" id = "attach_file" /> < br /> < input type = "submit" name = "submit" value = "Submit" /> </ form > </ body > </ html >请留意如下有关此表单的信息:
<form> 标签的 enctype 属性规定了在提交表单时要使用哪种内容类型,在表单需要二进制数据时,比如文件内容,请使用 "multipart/form-data".
<input> 标签的 type="file" 属性规定了应该把输入作为文件来处理,举例来说,当在浏览器中预览时,会看到输入框旁边有一个浏览按钮.
注释:允许用户上传文件是一个巨大的安全风险,请仅仅允许可信的用户执行文件上传操作.
创建上传脚本,"upload_file.php" 文件含有供上传文件的代码:
<?php if ( $_FILES [ "attach_file" ][ "error" ] > 0) { echo "Error: " . $_FILES [ "attach_file" ][ "error" ] . "<br />" ; } else { echo "Upload: " . $_FILES [ "attach_file" ][ "name" ] . "<br />" ; echo "Type: " . $_FILES [ "attach_file" ][ "type" ] . "<br />" ; echo "Size: " . ( $_FILES [ "attach_file" ][ "size" ] / 1024) . " Kb<br />" ; echo "Stored in: " . $_FILES [ "attach_file" ][ "tmp_name" ]; } ?>通过使用 PHP 的全局数组 $_FILES,你可以从客户计算机向远程服务器上传文件.
第一个参数是表单的 input name(表单file组件的name值),第二个下标可以是 "name","type", "size", "tmp_name" 或 "error",就像这样:
$_FILES["attach_file"]["name"] - 被上传文件的名称
$_FILES["attach_file"]["type"] - 被上传文件的类型
$_FILES["attach_file"]["size"] - 被上传文件的大小,以字节(b)为单位
$_FILES["attach_file"]["tmp_name"] - 存储在服务器的文件的临时副本的名称
$_FILES["attach_file"]["error"] - 由文件上传导致的错误代码
这是一种非常简单文件上传方式,基于安全方面的考虑,您应当增加有关什么用户有权上传文件的限制.
上传限制: 在这个脚本中,我们增加了对文件上传的限制,用户只能上传 .gif 或 .jpeg 文件,文件大小必须小于 20 kb,代码如下:
<?php if ((( $_FILES [ "attach_file" ][ "type" ] == "image/gif" ) || ( $_FILES [ "attach_file" ][ "type" ] == "image/jpeg" ) || ( $_FILES [ "attach_file" ][ "type" ] == "image/pjpeg" )) && ( $_FILES [ "attach_file" ][ "size" ] < 20000)) { if ( $_FILES [ "attach_file" ][ "error" ] > 0) { echo "Error: " . $_FILES [ "attach_file" ][ "error" ] . "<br />" ; } else { echo "Upload: " . $_FILES [ "attach_file" ][ "name" ] . "<br />" ; echo "Type: " . $_FILES [ "attach_file" ][ "type" ] . "<br />" ; echo "Size: " . ( $_FILES [ "attach_file" ][ "size" ] / 1024) . " Kb<br />" ; echo "Stored in: " . $_FILES [ "attach_file" ][ "tmp_name" ]; } } else { echo "Invalid file" ; } ?>注释: 对于 IE,识别 jpg 文件的类型必须是 pjpeg,对于 FireFox,必须是 jpeg.
保存被上传的文件
上面的例子在服务器的 PHP 临时文件夹创建了一个被上传文件的临时副本,这个临时的复制文件会在脚本结束时消失,要保存被上传的文件,我们需要把它拷贝到另外的位置,代码如下:
<?php if ((( $_FILES [ "attach_file" ][ "type" ] == "image/gif" ) || ( $_FILES [ "attach_file" ][ "type" ] == "image/jpeg" ) || ( $_FILES [ "attach_file" ][ "type" ] == "image/pjpeg" )) && ( $_FILES [ "attach_file" ][ "size" ] < 20000)) { if ( $_FILES [ "attach_file" ][ "error" ] > 0) { echo "Return Code: " . $_FILES [ "attach_file" ][ "error" ] . "<br />" ; } else { echo "Upload: " . $_FILES [ "attach_file" ][ "name" ] . "<br />" ; echo "Type: " . $_FILES [ "attach_file" ][ "type" ] . "<br />" ; echo "Size: " . ( $_FILES [ "attach_file" ][ "size" ] / 1024) . " Kb<br />" ; echo "Temp file: " . $_FILES [ "attach_file" ][ "tmp_name" ] . "<br />" ; if ( file_exists ( "upload/" . $_FILES [ "attach_file" ][ "name" ])) { echo $_FILES [ "attach_file" ][ "name" ] . " already exists. " ; } else { move_uploaded_file( $_FILES [ "attach_file" ][ "tmp_name" ], "upload/" . $_FILES [ "attach_file" ][ "name" ]); echo "Stored in: " . "upload/" . $_FILES [ "attach_file" ][ "name" ]; } } } else { echo "Invalid file" ; } ?>上面的脚本检测了是否已存在此文件,如果不存在,则把文件拷贝到指定的文件夹,注意:这个例子把文件保存到了名为 "upload" 的新文件夹.
查看更多关于PHP上传文件示例程序代码(适合初学者) - php上传下的详细内容...
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://www.haodehen.cn/did29375