Upload file to Server via AS3 & PHP - MAC FF & SAFARI PROBLEM ONLY
Goal: User will be able to upload a file from their hard drive and that file will be saved on server. Eventually I’d like to add security and not allow overwriting of files on server but for now I can’t seem to get the basics to work on MAC FF & Safari
The “onComplete” and upload never fires off. I’d be happy to email anyone the files if you need them. Thanks for your help!
OS Browsers Results I’ve Tested On:
Windows XP:
FF: works
IE: works
Chrome: works
Mac:
FF: Doesn’t work
Safari: Doesn’t work
Opera: works
Chrome: works
AS3:
//imports
import flash.utils.ByteArray;
import flash.net.FileReference;
import flash.net.FileFilter;
import flash.events.Event;
/*******************************************************************************************/
// Website Form Inquiry
name_txt.text =
email_txt.text =
phone_txt.text =
company_txt.text =
info_txt.text = "";
phone_txt.restrict = "0-9";
// MODES
submit_btn.buttonMode = true;
//CLICKS
submit_btn.addEventListener(MouseEvent.CLICK, sCLICK );
// OVERS
submit_btn.addEventListener(MouseEvent.ROLL_OVER, sOVER );
// OUTS
submit_btn.addEventListener(MouseEvent.ROLL_OUT, sOUT );
// FUNCTIONS
function sCLICK (e:MouseEvent):void {
var snd:Sound = new Sound();
snd.load(new URLRequest("click.mp3"));
snd.play();
}
function sOVER (e:MouseEvent):void {
}
function sOUT (e:MouseEvent):void {
}
// Upload Files
/*******************************************************************************************************************************/
filename_txt.text =
progress_txt.text = "";
// Set the URL for the PHP uploader script
var URLrequest:URLRequest = new URLRequest("http://www.mywebsite.com/upload.php");
var fileRef:FileReference= new FileReference();
/// Listeners
fileRef.addEventListener(Event.COMPLETE, completeHandler);
fileRef.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA,uploadCompleteDataHandler);
fileRef.addEventListener(SecurityErrorEvent .SECURITY_ERROR,securityHandler);
browse_btn.addEventListener(MouseEvent.CLICK, openBrowseDialog);
submit_btn.addEventListener(MouseEvent.CLICK, uploadNow);
clear_btn.addEventListener(MouseEvent.CLICK, cancelUpload);
function openBrowseDialog(e:MouseEvent):void
{
fileRef.browse([new FileFilter(" submittable file types - .jpg, .gif, .png, .doc, .pdf", "*.jpg;*.gif;*.png;*.doc;*.pdf;")]);
//fileRef.browse([FileFilter.macType(" submittable file types - .jpg, .gif, .png, .doc, .pdf", "*.jpg;*.gif;*.png;*.doc;*.pdf;")]);
//fileRef.browse([new FileFilter("Images", "*.jpg;*.gif;*.png;")]);
fileRef.addEventListener(Event.SELECT, onFileSelected);
}
function onFileSelected(e:Event):void {
filename_txt.text = "" + fileRef.name;
var variables:URLVariables = new URLVariables();
variables.todayDate = new Date();
URLrequest.method = URLRequestMethod.POST;
URLrequest.data = variables;
}
function uploadNow (e:MouseEvent):void {
fileRef.addEventListener(ProgressEvent.PROGRESS, progressHandler);
function progressHandler(event:ProgressEvent):void {
var file:FileReference = FileReference(event.target);
var percent:Number = Math.floor((event.bytesLoaded * 100)/ event.bytesTotal );
progress_txt.text = percent+"%";
}
trace("uploading");
fileRef.upload(URLrequest);
}
function uploadCompleteDataHandler(event:DataEvent):void {
trace("uploadCompleteData: " + event);
}
function completeHandler(event:Event):void {
trace("completeHandler: " + event);
trace("completed");
doOnComplete();
}
function doOnComplete(){
progress_txt.text = "100% Finished Uploading";
completeFinished();
}
function completeFinished(){
// timer to clear "progress_txt"
var timer:Timer;
timer = new Timer(500);
timer.addEventListener(TimerEvent.TIMER, on_timer);
timer.start();
function on_timer(te:TimerEvent):void
{
if( timer.currentCount >= 5 )
{
progress_txt.text =
filename_txt.text = "";
timer.removeEventListener(TimerEvent.TIMER, on_timer);
}
}
}
function cancelUpload(e:MouseEvent):void {
filename_txt.text = "";
trace("File Browse Canceled");
fileRef.cancel();
}
function securityHandler(event:SecurityErrorEvent):void {
filename_txt.text = "security error";
trace ( "a security error occurred");
}
PHP
<?php
// Set local PHP vars from the POST vars sent from flash
$todayDate = $_POST['todayDate'];
$Name = $_POST['Name'];
$Email = $_POST['Email'];
$filename = $_FILES['Filedata']['name'];
$filetmpname = $_FILES['Filedata']['tmp_name'];
$fileType = $_FILES["Filedata"]["type"];
$fileSizeMB = ($_FILES["Filedata"]["size"] / 1024 / 1000);
// Place file on server, into the uploads folder
$ok = move_uploaded_file($_FILES['Filedata']['tmp_name'], "uploads/".$filename);
if ($ok)
{
// Write to Log File
$myFile = "logFile.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
$stringData = "
todayDate: $todayDate
Name: $Name
Email: $Email
ssid: $ssid
FileName: $filename
TmpName: $filetmpname
Type: $fileType
Size: $fileSizeMB MegaBytes";
fwrite($fh, $stringData);
fclose($fh);
// End log file edit
echo " ";
}
else
{
echo "success=no";
}
?>