代码如下,求大神指点:
$allowext=array(‘jpeg’,’pjpeg’,’jpg’,’png’,’gif’,’zip’);
$ext=pathinfo($fileinfo[‘name’],pathinfo_extension);
if(!in_array($ext, $allowext)){
exit(‘非法文件类型’);
}
php.ini中:extension=php_zip.dll
问题在哪呢???
回复讨论(解决方案)
参考 http://wenrunchang123.iteye.com/blog/1472511
楼主完成了么?最近也有个这个需求,不熟悉php,楼主有demo么?跪求帮助啊
没看懂.是上传文件.不管txt,zip,jpg..还是解压zip上传.
前端上传
//fileupload.js
//@param options 一系列可选的key/value参数的集合:
//参数名 类型 说明
//url string 上传文件服务器端url
//file file 选择文件的表单元素.例如:document.getelementbyid(“file”); //
//begin function 开始回调函数.原型为:function(obj);
//end function 结束回调函数.原型为:function(obj);
//success function 成功回调函数.原型为:function(obj, data);
//error function 失败回调函数.原型为:function(obj);
//progress function 进度回调函数.原型为:function(obj, process, total);
//@returns object xhr或者iframe对象.支持函数abort();
function fileupload(options) {
if(!options || !options.url || !options.file)
return null;
if(window.formdata)
return fileupload_formdata(options); //xmlhttprequest level 2
else
return fileupload_iframe(options); //post form to iframe
function fileupload_formdata(options) {
var xhr = window.xmlhttprequest ? new window.xmlhttprequest() : window.activexobject ? new window.activexobject(“microsoft.xmlhttp”) : null;
if(!xhr)
return null;
options.type = “xhr”;
xhr.options = options;
//xhr.abort = function() {} //abort is xmlhttprequest’s native function
//xhr.onreadystatechange = function(e) {
// if(xhr.readystate == 4 && xhr.status == 200) {}
//};
xhr.onloadstart = function(e) {
if(this.options.begin)
this.options.begin(this);
}
xhr.onloadend = function(e) {
if(this.options.end)
this.options.end(this);
}
xhr.onload = function(e) {
try {
var data = json.parse(this.responsetext ? this.responsetext : “[]”);
if(this.options.success)
this.options.success(this, data);
}
catch(e) {
if(this.options.error)
this.options.error(this);
}
}
xhr.onerror = function(e) {
if(this.options.error)
this.options.error(this);
}
if(xhr.upload) {
xhr.upload.parent = xhr;
xhr.upload.onprogress = function(e) {
if(this.parent.options.progress && e.lengthcomputable)
this.parent.options.progress(this.parent, e.loaded, e.total);
}
}
var data = new formdata();
data.append(“action_type”, “formdata”);
for(i = 0; i < options.file.files.length; i++)
data.append("files[]", options.file.files[i]);
xhr.open("post", options.url);
xhr.send(data);
return xhr;
}
function fileupload_iframe(options) {
var time = new date().gettime();
//
var iframeid = "iframe_" + time;
var iframe = document.createelement('iframe');
iframe.options = options;
iframe.setattribute("name", iframeid);
iframe.setattribute("id", iframeid);
iframe.setattribute("style", "position:absolute; top:-10000px; left:-10000px; display:none");
document.body.appendchild(iframe);
//
var formid = "form_" + time;
var form = document.createelement('form');
form.setattribute("name", formid);
form.setattribute("id", formid);
form.setattribute("action", options.url);
form.setattribute("target", iframeid);
form.setattribute("method", "post");
form.setattribute("enctype", "multipart/form-data");
form.setattribute("style", "position:absolute; top:-10000px; left:-10000px; display:none");
document.body.appendchild(form);
//file.files只能由ui操作赋值,代码不能直接赋值,克隆出来的是没有值的.折中的做法是,将克隆出来的替换原始的,而将原始的添加进表单里面去.
var file = options.file.clonenode();
options.file.name = "files[]";
options.file.parentnode.insertbefore(file, options.file);
form.appendchild(options.file);
options.type = "iframe";
iframe.options = options;
iframe.form = form;
iframe.abort = function() {
this.src = "about:blank";
}
iframe.onload = function(e) {
//this.contentwindow.document.xmldocument
//this.contentdocument.document.xmldocument
var innertext = null;
if(this.contentwindow && this.contentwindow.document && this.contentwindow.document.body && this.contentwindow.document.body.innertext) {
//xml = this.contentdocument.document.xmldocument;
innertext = this.contentwindow.document.body.innertext;
}
else if(this.contentdocument && this.contentdocument.document && this.contentdocument.document.body && this.contentdocument.document.body.innertext) {
//xml = this.contentdocument.document.xmldocument;
innertext = this.contentdocument.document.body.innertext;
}
try {
var data = json.parse(innertext ? innertext : "[]");
if(this.options.success)
this.options.success(this, data);
}
catch(e) {
if(this.options.error)
this.options.error(this);
}
if(this.options.end)
this.options.end(this);
this.form.parentnode.removechild(this.form);
this.parentnode.removechild(this);
}
if(options.begin)
options.begin(iframe);
form.submit();
return iframe;
}
}
var hd = null;
function upload() {
var file = document.getelementbyid("file");
hd = fileupload({
url: "server.php",
file: file,
begin: function(obj) { //the object maybe xhr or iframe, which includes property options. the options's fields are [url, file, begin, end, success, error, progress, type and so on].
console.log("begin");
},
end: function(obj) {
console.log("end");
},
success: function(obj, data) {
console.log("success: " + json.stringify(data));
},
error: function(obj) {
console.log("error");
},
progress: function(obj, process, total) {
console.log("progress: " + process + "/" + total);
var label = document.getelementbyid("progress");
label.innertext = math.round(total ? process / total * 100 : 0) + "%";
}
});
}
function abort() {
hd && hd.abort();
}
process
//uploadhandler.php
header('content-type: application/json; charset=utf-8'); //在slim框架内,这样设置无效.
header('access-control-allow-origin: *'); //允许跨域访问的域.可以是一个域的列表,也可以是通配符*.
header('access-control-allow-headers: content-type'); //允许自定义的头部.以逗号隔开.大小写不敏感.
header('access-control-max-age: 120'); //缓存此次请求的秒数.
//header('access-control-allow-credentials: true'); //是否允许请求带有验证信息.
//header('access-control-expose-headers: foobar'); //允许脚本访问的返回头.请求成功后,脚本可以在xmlhttprequest中访问这些头的信息.
//header('access-control-allow-methods: get,post,put,delete'); //允许使用的请求方法.以逗号隔开.
//**************************2.request header**************************
//以下请求头,不需要手动添加,一般由浏览器自动添加.
//header('access-control-request-headers: accept,origin,content-type');
//header('access-control-request-method: post');
class uploadhandler {
protected $m_callback = null;
public function __construct($callback) {
$this->m_callback = $callback;
switch($_server[“request_method”]) {
case “options”:
$this->options();
break;
case “head”:
$this->head();
break;
case “get”:
$this->get();
break;
case “post”:
$this->post();
break;
}
}
public function __destruct() {
}
protected function options() {
}
protected function head() {
}
protected function get() {
}
protected function post() {
if(!isset($_files[“files”]) || !isset($_files[“files”][“tmp_name”]))
exit(“[]”);
$files = array();
if(is_array($_files[“files”][“tmp_name”])) {
foreach($_files[“files”][“tmp_name”] as $i => $val) {
$tmp_name = $val;
$name = isset($_files[“files”][“name”][$i]) ? $_files[“files”][“name”][$i] : “”;
$type = isset($_files[“files”][“type”][$i]) ? $_files[“files”][“type”][$i] : “”;
$size = isset($_files[“files”][“size”][$i]) ? $_files[“files”][“size”][$i] : 0;
$error = isset($_files[“files”][“error”][$i]) ? $_files[“files”][“error”][$i] : 0;
$files[] = $this->handle_file_upload($tmp_name, $name, $type, $size, $error);
}
}
else {
$tmp_name = isset($_files[“files”][“tmp_name”]) ? $_files[“files”][“tmp_name”] : “”;
$name = isset($_files[“files”][“name”]) ? $_files[“files”][“name”] : “”;
$type = isset($_files[“files”][“type”]) ? $_files[“files”][“type”] : “”;
$size = isset($_files[“files”][“size”]) ? $_files[“files”][“size”] : 0;
$error = isset($_files[“files”][“error”]) ? $_files[“files”][“error”] : 0;
$files[] = $this->handle_file_upload($tmp_name, $name, $type, $size, $error);
}
//使json_encode不转义汉字(即避免出现类似”\u5c0f\u8c61″的unicode字符).详见:http://blog.csdn.net/qmhball/article/details/45690017
$str = json_encode($files, json_unescaped_unicode);
echo $str;
}
protected function handle_file_upload($tmp_name, $name, $type, $size, $error) {
$file = new \stdclass();
$file->tmp_name = $tmp_name;
$file->name = $name;
$file->type = $type;
$file->size = $size;
$file->error = $error;
if($this->m_callback) {
$func = $this->m_callback;
$func($file);
}
return $file;
}
}
//server.php
include_once __dir__ . “/uploadhandler.php”;
new uploadhandler(function ($file) {
$file->id = 200;
if(!file_exists(“images”)) {
@mkdir(“images”, 0777);
@chmod(“images”, 0777);
}
if(file_exists($file->tmp_name)) { //超出最大上传文件大小则上传不成功
$fn = md5_file($file->tmp_name) . “.” . pathinfo($file->name, pathinfo_extension);
move_uploaded_file($file->tmp_name, __dir__ . “/images/$fn”);
//move_uploaded_file($file->tmp_name, __dir__ . “/images/$file->name”);
}
});