您好,欢迎访问一九零五行业门户网

php下传文件的5种方式

php上传文件的5种方式
作者:zccst
一、普通文件上传方式
关于上次文件,之前已经会的有两种方式,具体如下:
1,使用原生态的php上次文件。
(1)前端代码


filename:
(2)php代码(upload_file.php)

2,使用yii框架的cupload。
参见1:http://zccst.iteye.com/blog/1114948
参见2:http://zccst.iteye.com/blog/1271104
但是,两者面临的相同问题是提交后刷新页面。即非异步上次方式。
二、异步文件上传方式(3种方式)
现在需要异步上传方式,经过同学的帮助和查资料,得知php异步上传文件的有两种实现方式。
1,使用插件(如,jqueryupload)。
2,使用iframe上传,无需额外插件。
根据目前需求:尽量少使用插件。于是采用后者,即使用iframe异步上传文件
具体稍后补充
(1)前端html


导入文件:
function startupload() { var spanobj = document.getelementbyid(info); spanobj.innerhtml = 开始上传; document.getelementbyid(upform).sumbit();}//回调function stopupload(responsetext){ var spanobj = document.getelementbyid(info); spanobj.innerhtml = 上传成功; spanobj.innerhtml = responsetext;}
(2)服务器端代码
$file = $_files['myfile'];$filename = uploadfile($file);//$result = readfromfile(../upload/ . $filename);echo ;function uploadfile($file) { // 上传路径 $destinationpath = ../upload/; if (!file_exists($destinationpath)){ mkdir($destinationpath , 0777); } //重命名 $filename = date('ymdhis') . '_' . iconv('utf-8' , 'gb2312' , basename($file['name'])); if (move_uploaded_file($file['tmp_name'], $destinationpath . $filename)) { return iconv('gb2312' , 'utf-8' , $filename); } return '';}
//代码注释/*1,关于basename方法$path = /testweb/home.php;//显示带有文件扩展名的文件名echo basename($path);//显示不带有文件扩展名的文件名echo basename($path,.php);2,关于iconviconv('gb2312' , 'utf-8' , $filename);//将$filename从gb2312转为utf-8格式。注:该函数需要开启php.ini里面的php_iconv.dll3,关于$_files['myfile']$_files相当于一个二维数组,而$_files['myfile']相当于一个一维数组。所以可以$f = $_files['myfile'];echo $f['name'];如果直接访问该$_files['myfile'],则会报undefined index: myfile。此时加上if(!isset($_files['myfile'])){ die('上传文件不存在!');}*/
3,使用yii+iframe方式
参见:http://zccst.iteye.com/blog/1271091
附:在网上查到的相关资料如下
http://www.cnblogs.com/spemoon/archive/2011/01/07/1930221.html
http://wenku.baidu.com/view/80522df6ba0d4a7302763abb.html
http://www.jb51.net/article/28427.htm
http://apps.hi.baidu.com/share/detail/20419052
其它类似信息

推荐信息