Flex + PHP uploads images | Flex + PHPRealizationpictureuploading 1 Flex code
<br /> import flash.net.FileReference; <br /> import mx.controls.Alert; <br /> import mx.events.CloseEvent; <br /> import flash.events.*; <br /> private var file : FileReference; <br /> private var uploadURL : URLRequest; <br /> private function init() : void{ <br /> Security.allowDomain("*"); <br /> file = new FileReference(); <br /> file.addEventListener(ProgressEvent.PROGRESS, onProgress); <br /> file.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA,onUPLOAD_COMPLETE_DATA); <br /> file.addEventListener(Event.SELECT, onSelect); <br /> uploadURL = new URLRequest(); <br /> uploadURL.url = "http://localhost:80/ShowProduct-debug/uploadFile.php"; <br /> uploadURL.method = "post"; <br /> } <br /> private function upload() : void{ <br /> var imageTypes:FileFilter = new FileFilter("Images (*.jpg, *.jpeg, *.gif, *.png)", "*.jpg; *.jpeg; *.gif; *.png"); <br /> var allTypes:Array = new Array(imageTypes); <br /> file.browse(allTypes); <br /> } <br /> private function onSelect(e : Event) : void{ <br /> Alert.show("上传 " + file.name + " (共 "+Math.round(file.size)+" 字节)?", "确认上传",Alert.YES|Alert.NO,null,proceedWithUpload); <br /> } <br /> private function onProgress(e:ProgressEvent) : void{ <br /> lbProgress.text = " 已上传 " + e.bytesLoaded + " 字节,共 " + e.bytesTotal + " 字节"; <br /> } <br /> private function proceedWithUpload(e : CloseEvent) : void{ <br /> if (e.detail == Alert.YES){ <br /> file.upload(uploadURL); <br /> } <br /> } <br /> private function onUPLOAD_COMPLETE_DATA(evt:DataEvent):void <br /> { <br /> Alert.show(evt.data.toString()); <br /> } <br />
Note: uploadURL. url = "http: // localhost: 80/ShowProduct-debug/uploadFile. php "; the path must be correct; otherwise, Error #2044 is returned: unprocessed IOErrorEvent :. Text = Error #2038: file I/O Error.
2. php code
// The name attribute of the file Form passed by Flash is Filedata
$ FileName = $ _ FILES ["Filedata"] ["name"];
$ File = $ _ FILES ["Filedata"] ["tmp_name"];
$ Path = "uploadFiles /";
If (! Is_dir ("./uploadFiles ")){
@ Mkdir ("./uploadFiles ");
}
If (move_uploaded_file ($ file, $ path. $ fileName )){
Echo Success;
} Else {
Echo Fail;
}
?>
Note: The php file must be placed in the directory where Apache runs. for example, put the php file in the C: \ xampp \ htdocs \ ShowProduct-debug directory.