Saving a picture in my flash webcam snapshot application
I have made a flash webcam application to allow the user to take a photo of themselves using a webcam. I have successfully saved the photo in my localhost. However, I would like to set the names of those photos. Currently, the photos names are generated by
$uniqueStamp = date('U');
I also have a textfield in my flash app that allows inputs from user for the photo name. For example if the user write 'MYPHOTONAME' in the textfield, the photo which will be saved in my localhost will be named 'MYPHOTONAME' too. How do I do it in Flash and in开发者_如何学运维 PHP? Currently I'm having some problems because I cannot send the textfield string to PHP.
Here is my flash code to send the snapshot to PHP and let the PHP save it in my localhost:
imgBA = jpgEncoder.encode(imgBD1);
sendReq.data = imgBA;
sendLoader.load(sendReq);
Here is my PHP code:
if ( isset ( $GLOBALS["HTTP_RAW_POST_DATA"] )) {
$photo = $_POST['photo'];
$uniqueStamp = date('U');
$filename = $photo.$uniqueStamp.".jpg";
$fp = fopen( 'photos/'.$filename,"wb");
fwrite( $fp, $GLOBALS[ 'HTTP_RAW_POST_DATA' ] );
fclose( $fp );
echo 'filename=photos/'.$filename."&base=".$_SERVER["HTTP_HOST"].dirname($_SERVER["PHP_SELF"]);
}
As you can see, I tried to pass in $photo = $_POST['photo']; from flash to PHP however I do not know the exact code, can anyone help me?
You can use this class org.lzyy.util.UploaderPostHelper.getPostData() to emulate an HTTP upload (do same thing of FileReference but with generated data)
ActionScript side:
var urlRequest:URLRequest = new URLRequest();
urlRequest.url = uploadPath;
urlRequest.contentType = 'multipart/form-data; boundary=' + UploadPostHelper.getBoundary();
urlRequest.method = URLRequestMethod.POST;
urlRequest.data = UploadPostHelper.getPostData(file, parameters);
urlRequest.requestHeaders.push( new URLRequestHeader( 'Cache-Control', 'no-cache' ) );
var urlLoader:URLLoader = new URLLoader();
urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
//handler for succesful loading of request, ex: trace(urlLoader.data);
//Complete not mean image handling was a success
//Memory usage, type checking, security restriction etc. could produce errors
urlLoader.addEventListener(Event.COMPLETE, onComplete);
//handler for faulty loading of request
urlLoader.addEventListener(IOErrorEvent.IO_ERROR, onError);
urlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onError);
urlLoader.load(urlRequest);
And PHP side, a basic image upload handling:
<?php
if ((($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
?>
Only one thing you can't do, is listen upload progression.
- Handling file uploads
精彩评论