Extjs4创建简单的图片上传

先给出Extjs代码:

Ext.define('MyApp.view.ui.MyForm', {
extend: 'Ext.form.Panel',

height: 107,
width: 398,
bodyPadding: 10,
title: '上传图片',

initComponent: function() {
var me = this;
me.items = [
{
xtype: 'filefield',
name: 'file',
fieldLabel: '请选择图片',
anchor: '100%',
allowBlank:false,
regex: /((\.jpg)|(\.png)|(\.gif))$/i,
invalidText:'请选择正确的图片格式'
},
{
xtype: 'button',
text: '点击上传',
handler:function(btn){
var form=btn.up('form').getForm();
if(form.isValid())
{
form.submit({
url:'pro.php',
waitMsg:'正在上传中...',
success:function(grid,action){
Ext.Msg.alert('信息提示',action.result.msg);
},
failure:function(grid,action){
Ext.Msg.alert('信息提示',action.result.msg);
}

});
}
else
{
Ext.Msg.alert('信息提示','请选择正确的图片格式');
}
}

}
];
me.callParent(arguments);
}
});

Ext.onReady(function(){
Ext.create('MyApp.view.ui.MyForm',{

renderTo:Ext.get('main')
});
})

再给出服务器端代码(PHP):

<?php

if ($_FILES["file"]["error"] > 0)
{
echo "{
success:false,
msg:'上传出错,请重试'
}";
}
else
{
if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo "{
success:false,
msg:'文件已存在'
}
";
}
else
{
$now=time();//获取当前时间戳
$path_arr=pathinfo($_FILES["file"]["name"]);
$new_file_name=$now.'.'.$path_arr['extension'];
move_uploaded_file($_FILES["file"]["tmp_name"],"upload/".$new_file_name);
echo "{
success:true,
msg:'上传成功'
}";
}
}


?>


解释一下pathinfo()函数,用来获取一个文件名中的后缀名,不带点号。下面是API中给的例子:

<?php
$path_parts = pathinfo("/www/htdocs/index.html");
echo $path_parts["dirname"] . "\n";//输出/www/htdocs
echo $path_parts["basename"] . "\n";//输出index.html
echo $path_parts["extension"] . "\n";//输出html
?>



posted on 2012-03-28 21:16  crazymus  阅读(581)  评论(0编辑  收藏  举报

导航