完美的flex+php上传文件实例【转】

完美的flex+php上传文件实例【转】2009年05月06日 星期三 上午 03:39整理了一下,又写了一下注释。看看吧。
mail.mxml:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init();">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.managers.CursorManager;

private var urlRequset:URLRequest;
private var file:FileReference = new FileReference();//实例化一个文件类件
private var serverSide:String = "http://www.flash.com/uploads.php";//指明后台处理的文件

private function init():void{
localChoose.addEventListener(MouseEvent.CLICK,doSelect);
file.addEventListener(Event.SELECT,onSelect);
resetBtn.addEventListener(MouseEvent.CLICK,doReset);
submitBtn.addEventListener(MouseEvent.CLICK,doUpload);

}
public function uploadCompleteHandler(event:Event):void{//上传成功
localPath.text = event.target.name+"文件上传成功!";
Alert.show(localPath.text,"提示");
}

public function doSelect(evt:Event):void{//选取文件
file.browse();
}
public function onSelect(evt:Event):void{//选取文件完成
localPath.text=file.name;
}
public function doReset(evt:Event):void{//重置
localPath.text='';
}
public function doUpload(event:Event):void{//开始上传
if(localPath.text==''){
Alert.okLabel = "确 定";
Alert.show("请选择要上传的文件!","警告");
}else{
var request:URLRequest=new URLRequest(serverSide);
file.addEventListener(Event.COMPLETE, uploadCompleteHandler);
file.addEventListener(ProgressEvent.PROGRESS,processHandler);
localPath.text = "正在上传 " + file.name;
CursorManager.setBusyCursor();
file.upload(request);
}

}
public function processHandler(evt:ProgressEvent):void{//上传进度
if(evt.bytesLoaded == evt.bytesTotal)
{
CursorManager.removeBusyCursor();
}else
{
var proc:uint=evt.bytesLoaded/evt.bytesTotal*100;
localPath.text="已上传:"+proc;
}
}
]]>
</mx:Script>
<mx:Panel x="10" y="10" width="421" height="184" layout="absolute" title="FLEX上传工具DEMO" fontSize="12" fontWeight="bold">
<mx:Label x="37" y="31" text="上传文件:"/>
<mx:TextInput id="localPath" x="107" y="29" width="179.5" editable="false"/>
<mx:Button id="localChoose" x="312" y="29" label="选择"/>
<mx:Button id="submitBtn" x="107" y="79" label="开始上传" />
<mx:Button id="resetBtn" x="234.5" y="79" label="重填"/>
</mx:Panel>
</mx:Application>

uploads.php:
这里要注意了,还要在uploads.php文件的同级目录建一个uploads的文件夹。上传的文件就会上传到这个文件夹中。
<?php
$uploaddir = 'uploads/';
$filename = date("Ymdhis").rand(100,999);//上传改后文件名
$uploadfile = $uploaddir .$filename.substr($_FILES['Filedata']["name"],strrpos($_FILES['Filedata']["name"],"."));
$temploadfile = $_FILES['Filedata']['tmp_name'];
file_put_contents("catcah.txt",$_FILES['Filedata']['tmp_name']);
move_uploaded_file($temploadfile , $uploadfile);
?>
再看一下uploads那个文件夹是不是有你上传的文件了。

如果想把上传的文件入库的话也很简单,加上下面的代码就可以了。。。


# 定义数据库资料
Define('DATABASE_SERVER', 'localhost');
Define('DATABASE_USERNAME', 'root');
Define('DATABASE_PASSWORD', 'root');
Define('DATABASE_NAME', 'woaiphp');

# Connect to the database
$link = @mysql_connect(DATABASE_SERVER, DATABASE_USERNAME, DATABASE_PASSWORD);
mysql_select_db("test", $link);
mysql_query("SET NAMES utf8");//设置UTF8编码

# 添加数据
$sql = "INSERT INTO `woaiphp`.`test` (`id` ,`file`) VALUES ('','".$uploadfile."')";
mysql_query($sql);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值