使用php上传文件
2009-12-11 11:44
337 查看
参考: http://www.phpchina.com/html/97/14397-4443.html
利用html提供的文件上传功能,php其实仅仅是把它保存在本地指定目录。
1、首先dump php 内置变量:
#var_dump($_FILES);
可以看到文件其实已经被临时存放在服务器了。
2、这时需要做的,就是把它用合适的名称保存到指定目录。
获取文件后缀,进行一些必要的检查。
生成一个唯一的文件名。
---------------------------------------------
# index.html
<form method="post" action="upload.php" enctype="multipart/form-data">
<table border=0 cellspacing=0 cellpadding=0 align=center width="100%">
<tr>
<td width=55 height=20 align="center"><input type="hidden" name="MAX_FILE_SIZE" value="2000000">文件: </TD>
<td height="16">
<input name="file" type="file" value="浏览" >
<input type="submit" value="上传" name="B1">
</td>
</tr>
</table>
</form>
# upload.php
<?php
//var_dump($_FILES);
$uploaddir = "./files/";//设置文件保存目录 注意包含/
$allow_types = array("txt");//设置允许上传文件的类型
// 返回函数
+ +-- 4 lines: function _return($ret_code, $ret_msg){------------------------------------------------------------------------------
+ +-- 3 lines: function _return_ok($ret_msg){--------------------------------------------------------------------------------------
+ +-- 3 lines: function _return_error($ret_msg){-----------------------------------------------------------------------------------
// 原始文件信息
$raw_file_name = trim($_FILES['file']['name']);
$raw_file_type = trim($_FILES['file']['type']);
$raw_file_tail = strtolower(substr(strrchr($raw_file_name, '.'), 1));
//判断文件类型
if(!in_array($raw_file_tail, $allow_types)){
$text = implode(",",$allow_types);
_return_error("allow type: $text");
}
if ('text/plain' != $raw_file_type){
_return_error("allow type: text/plain");
}
//生成目标文件的文件名
$upload_file_name = tempnam($uploaddir, $raw_file_name.".");
// 保存文件
if (move_uploaded_file($_FILES['file']['tmp_name'],$upload_file_name)){
_return_ok("ok");
} else {
_return_error("fail");
}
exit;
?>
利用html提供的文件上传功能,php其实仅仅是把它保存在本地指定目录。
1、首先dump php 内置变量:
#var_dump($_FILES);
array(1) { ["file"]=> array(5) { ["name"]=> string(8) "test.txt" ["type"]=> string(10) "text/plain" ["tmp_name"]=> string(18) "/var/tmp/phpUCf72l" ["error"]=> int(0) ["size"]=> int(589) } }
可以看到文件其实已经被临时存放在服务器了。
2、这时需要做的,就是把它用合适的名称保存到指定目录。
获取文件后缀,进行一些必要的检查。
生成一个唯一的文件名。
---------------------------------------------
# index.html
<form method="post" action="upload.php" enctype="multipart/form-data">
<table border=0 cellspacing=0 cellpadding=0 align=center width="100%">
<tr>
<td width=55 height=20 align="center"><input type="hidden" name="MAX_FILE_SIZE" value="2000000">文件: </TD>
<td height="16">
<input name="file" type="file" value="浏览" >
<input type="submit" value="上传" name="B1">
</td>
</tr>
</table>
</form>
# upload.php
<?php
//var_dump($_FILES);
$uploaddir = "./files/";//设置文件保存目录 注意包含/
$allow_types = array("txt");//设置允许上传文件的类型
// 返回函数
+ +-- 4 lines: function _return($ret_code, $ret_msg){------------------------------------------------------------------------------
+ +-- 3 lines: function _return_ok($ret_msg){--------------------------------------------------------------------------------------
+ +-- 3 lines: function _return_error($ret_msg){-----------------------------------------------------------------------------------
// 原始文件信息
$raw_file_name = trim($_FILES['file']['name']);
$raw_file_type = trim($_FILES['file']['type']);
$raw_file_tail = strtolower(substr(strrchr($raw_file_name, '.'), 1));
//判断文件类型
if(!in_array($raw_file_tail, $allow_types)){
$text = implode(",",$allow_types);
_return_error("allow type: $text");
}
if ('text/plain' != $raw_file_type){
_return_error("allow type: text/plain");
}
//生成目标文件的文件名
$upload_file_name = tempnam($uploaddir, $raw_file_name.".");
// 保存文件
if (move_uploaded_file($_FILES['file']['tmp_name'],$upload_file_name)){
_return_ok("ok");
} else {
_return_error("fail");
}
exit;
?>
相关文章推荐
- eweb使用WINDOWS的“备份”工具对网站内容自动备份editor for php任意文件上传漏洞
- php开发日记之使用ueditor上传文件至云存储
- 怎么样通过php使用html5实现多文件上传?(php多图上传)
- 使用php完成一个用户注册以及管理的demo(php实现单文件与多文件的上传)
- Android使用HttpClient实现文件上传到PHP服务器,并监控进度条
- PHP使用curl请求实现post方式上传图片文件功能示例
- PHP使用之上传文件到服务器
- PHP使用FTP函数备份/上传服务器文件
- FireFox下使用Javascript上传大文件(php版本)
- C# 使用HttpWebRequest通过PHP接口 上传文件
- php 使用html5实现多文件上传实例
- Android使用HttpClient实现文件上传到PHP服务器,并监控进度条
- php中上传图片文件,并且以上传时的时间戳命名文件,并将文件的路径存在session中以便使用。
- 使用ajaxfileupload.js实现ajax上传文件php版
- fastDFS中使用php上传文件 -- http上传与下载图片
- #舍得Share#php开发笔记-使用curl查看网页源码/自动跳转/显示头信息/显示通信过程/发送表单信息/文件上传/Referer字段/User Agent
- PHP使用原生函数实现文件上传及下载
- php使用ftp实现文件上传与下载功能
- php使用AjaxFileUpload上传图片文件实例
- PHP使用curl模拟post上传及接收文件的方法