Quote:
Originally Posted by geoblow And one more question,
I am working in flash action script. I wana do one flash uploader by using the server side script php. Can anyone give the code for that?
I can send the file binary data to php by using File reference object.
If u wants the flash code I can post that too.
Thanks |
Hey Geo... this is KBala, i am pretty good in flash action script and server side scripts. Actually u just need the php server side script to upload a file from flash, but here i try to explain about "File uploading from a flash client app to a web server". Eventhough you are well about flash part, i hope it will be useful for others.
Lets first see the Flash part(Action Script 3.0).
Create a new flash document and write the below script in frame 1.
Code:
var fileRef:FileReference = new FileReference();
here, we have created a new instance of FileReference Class, fileRef is the instance(object) name.
Code:
fileRef.addEventListener(Event.SELECT, selectHandler)
we have to add a event and a listener to the fileRef object. Here the event is "SELECT", listener is "selectHandler". The meaning of the above code is, when the SELECT event fires, call the selectHandler function
Code:
fileRef.addEventListener(Event.COMPLETE, completeHandler)
we can add another event to notify the upload complition
Code:
private function selectHandler(event:Event):void
{
var request:URLRequest = new URLRequest();
request.url = "http://somename.com/upload.php";
request.method = "POST";
fileRef.upload(request,"Filedata");
} consider, we have to upload a image file to the server. Here, the core functionality used to upload the selected file. As soon as SELECT event of the fileRef object fires, the above listener function will execute. Now the fileRef object contains the selected image file in the form of binary data.
[Note: Open a image file in a Notepad to see the binary data of the image. This binary data will be stored in the fileRef object as a stream of string]
Create a URLRequest object as above and make sure the request method should be "POST". And call the upload function of the fileRef object. This function has two params. one is for where to post the binary data, another is post variable name. You can see how this variable name will be used in the server side script.
Code:
private function completeHandler(event:Event):void
{
Alert.show("One file uploaded");
} This function will be executed when the binary data transfered over http from fileRef object to server.
Place a button control on the stage and name as "btn"
Code:
btn.addEventListener(MouseEvent.CLICK, clickHandler);
//listener function
private function clickHandler(event:MouseEvent):void
{
fileRef.browse();
} When you click the button, Browse dialog window will open to select the image file. The SELECT event will fire when the user click the "Open" button of the Browse window.
So far, the flash part will come to the end, let us see the server side script(php)
PHP Code:
<?php
if ($_FILES['Filedata']['tmp_name'] && $_FILES['Filedata']['name'])
{
$vSourceFile = $_FILES['Filedata']['tmp_name'];
$vImageName = stripslashes($_FILES['Filedata']['name']);
$vFoldername = 'UploadedFiles/';
if(!is_dir($vFoldername))
{
mkdir($vFoldername, 0777); //#-- Create a new session directory chmod($vFoldername, 0777); //#-- Set all the access permissions to the folder
}
if(copy($vSourceFile,$vFoldername.$vImageName))
$vStatus = 1;
else
$vStatus = 0;
}
else
{
$vStatus = 0;
}
echo '<?xml version="1.0" encoding="UTF-8"?>';
if($vStatus)
echo '<status>1</status>';
else
echo '<status>0</status>';
?>
See, how the "Filedata" variable is used.
$_FILES['Filedata']['tmp_name'] will return the binary data of the image.
$_FILES['Filedata']['name'] will return original name of the image file (with extension).
$_FILES['Filedata']['size'] will return size of the image file in bytes.
The image file is stored in the "UploadedFiles" folder. This will be created with full permission if it does not exsit.
By using "Copy" method, the file will be stored in the "UploadedFiles" folder as same file name. Finally the status will be return back to Flash client app as xml.
That's it Geo, i think you will get the exact one that you want. And if you have any doubt regarding this, dont hesitate, feel free to ask me.
Thanks
-kb