您好,欢迎光临! 推荐您使用Chrome浏览器访问本站。

PHP上传文件 保存到文件夹或数据库

写到数据库:
$filepath = $_FILES[“uploadfile”][“name”];
$formdata=$_FILES[“uploadfile”][“tmp_name”];
$filedata = addslashes(fread(fopen($formdata,”r”),filesize($formdata)));
$newid=TSMLDB::getInstance()->insert_attachmnt_all($id, $filepath,$filedata);
写到文件:
move_uploaded_file($formdata, iconv(“utf-8”, “gb2312”, “uploads/”.$filepath));

两种取值方式:
$_POST 与 php://input可以取到值,$HTTP_RAW_POST_DATA 为空
$_POST 以关联数组方式组织提交的数据,并对此进行编码处理,如urldecode,甚至编码转换。
php://input 可通过输入流以文件读取方式取得未经处理的POST原始数据

php://input 允许读取 POST 的原始数据。和 $HTTP_RAW_POST_DATA 比起来,它给内存带来的压力较
小,并且不需要任何特殊的 php.ini 设置。php://input 不能用于 enctype=”multipart/form-data”

网摘小例:
判断上传文件的文件类型多种实例代码
$array = array(‘jpg’,’gif’,’png’,’jpeg’);
$picImg =’/upfile/test.jpg’;
$img = strtolower($picImg);
//获取文件件扩展名方法一
$ext = substr($img,strrpos($img,’.’)+1);//这里是读取文件扩展名的代码
//获取文件件扩展名方法二
$ext = end(explode(‘.’,$img));
//获取文件件扩展名方法三 这是应该是最安全的了,就是用php $_FILES[‘type’]
$ext = $_FILES[‘file’][‘type’];
//获取文件件扩展名方法四
$ext = getimagesize($img);//这个函数返回一个数组
if( !in_array( $ext,$array ) )
{
exit(‘缩略图地址错误,请重新上传!’);
}
else
{
echo(‘你上传的文件类型不允许’);
exit;
}
/*
函数解析:
array 数组这个不说了
strtolower 把字符大字转换成小写
substr 字符截取,对中文处理不友好。
strrpos 判断字符出现在指定字符串中的位置
explode 分割函数,返回结果为数组
end 读取数据最后一个值
$_FILES 全局变量文件上传
getimagesize 获取图片的类型
in_array 判断当变量是否在数组中
exit 终止当前脚本运行

*/
更多参考:
http://php.chinaunix.net/manual/zh/features.file-upload.put-method.php
http://php.chinaunix.net/manual/zh/wrappers.php.php
http://php.chinaunix.net/manual/zh/features.file-upload.put-method.php
http://justcoding.iteye.com/blog/986293
http://wenku.baidu.com/view/94783946336c1eb91a375d15.html
http://www.frostsky.com/2012/03/php-input-2/

您可能也喜欢