PDA

View Full Version : FileReference, LoadVars, PHP and PHP SESSION


jeffnovis
04-21-2006, 09:05 PM
Hi,

Thanks for reading this. I'm new to ActionScript and PHP so I expect this will be something quite simple but I've been racking my brains for hours and I can't see the problem. I'm trying to upload a user-selected image file from a Flash movie and then display the image in a ScrollPane in the movie (actually I'm eventually going to create thumbnails for display so I'm just using small images to test).

The upload is done with the FileReference object (adapted from a script I found on the Web) and a simple PHP upload script which stores the name of uploaded image as $_SESSION['image']. This works fine.

In the (ActionScript)"onComplete" method for my upload, I've put the "load" method of a LoadVars object that tries to get the name of the uploaded image from a second PHP script which tries to access $_SESSION['image']. However, for some reason $_SESSION['image'] is not set. I've arranged for the second PHP script to pass $_SESSION['image'] to Flash if it is set, or a "No image" GIF if it's not.

Can anyone see why $_SESSION['image'] is not being saved to the PHP SESSION, or if it is, why the second PHP file (image_data.php) can't read it?

Heres the URL to try out: http://www.winkleweb.org/my_uploader/my_uploader.html

Here's the ActionScript file:

import mx.utils.Delegate;
import mx.controls.TextArea;
import mx.controls.ProgressBar;
import mx.controls.Button;
import mx.containers.ScrollPane;
import flash.net.FileReference;

var textArea:TextArea;
var progressBar:ProgressBar;
var browse_btn:Button;
var upload_btn:Button;
var imagePane:ScrollPane;
var fileRef:FileReference;
var uploadURL:String = "http://www.winkleweb.org/my_uploader/upload.php";
var imageData:LoadVars;

function onLoad():Void
{
textArea.fontSize = 15;
progressBar.mode = "manual";
progressBar.fontSize = 14;
progressBar.color = 0x848384;
progressBar.visible = false;
browse_btn.label = "Browse";
browse_btn.onPress = Delegate.create(this, this.browse);
upload_btn.label = "Upload";
upload_btn.onPress = Delegate.create(this, this.upload);
imagePane.contentPath = "";
fileRef = new FileReference();
fileRef.addListener(this);
}

imageData = new LoadVars();
imageData.onLoad = function()
{
insertImage();
};

insertImage = function()
{
imagePane.contentPath = imageData.scrollPaneImage;
};

function browse():Void
{
var success:Boolean;
success = this.fileRef.browse([{description: "Image Formats (*.jpg, *.jpeg)", extension: "*.jpg;*.jpeg", macType: "JPEG;jp2_"}]);
if(success == false)
{
textArea.text = "OS window failed to open";
}
}

function upload():Void
{
var success:Boolean;
success = this.fileRef.upload("http://www.winkleweb.org/my_uploader/upload.php");
if(success == false)
{
textArea.text = "upload process failed to start";
}
}

function onSelect(fileRef:FileReference):Void
{
textArea.text = fileRef.name + " (";
textArea.text += "size: " + fileRef.size + " kb)";
}

function onOpen(fileRef:FileReference):Void
{
textArea.text = "upload started";
}

function onProgress(fileRef:FileReference, loaded_num:Number, total_num:Number):Void
{
progressBar.visible = true;
progressBar.setProgress(loaded_num, total_num);
}

function onComplete(fileRef:FileReference):Void
{
progressBar.setProgress(100, 100);
progressBar.visible = false;
textArea.text = "upload successful";

imageData.load("http://www.winkleweb.org/my_uploader/image_data.php");
}

function onCancel():Void
{
textArea.text = "Cancelled";
}

function onHTTPError(fileRef:FileReference):Void
{
textArea.text = "onHTTPError: " + fileRef.name;
}

function onIOError(fileRef:FileReference):Void
{
textArea.text = "onIOError: " + fileRef.name;
}

function onSecurityError(fileRef:FileReference, error_str:String):Void
{
textArea.text = "onSecurityError: " + fileRef.name + " error : " + error_str;
}

Here's the upload.php file:

session_start();
$uploadDir = "uploads/";
$_SESSION['image'] = $_FILES['Filedata']['name'];
$uploadpath = $uploadDir . $_SESSION['image'];

function move_file(uploadedfile)
{
move_uploaded_file($_FILES['Filedata']['tmp_name'], uploadedfile);
}

move_file($uploadpath);

and here's the image_data.php file used to get the LoadVars data:

session_start();

if (isset($_SESSION['image']))
{
$user_image = "uploads/" . $_SESSION['image'];
}
else
{
$user_image = "uploads/" . "no_image.gif";
}

print("&scrollPaneImage=$user_image");

If you want to test this on your server you can download all the files from http://www.winkleweb.org/my_uploader/my_uploader.zip

Thanks again for having a look at this.

Cheers,

Jeff

loze
09-27-2006, 03:32 AM
did you ever solve this?
im having a similar problem.
it seems that session variables cannot be set or read in the upload.php file.
but only in firefox and safari, it seems to work in ie

CreativityInCode
12-29-2007, 04:19 AM
My assumption as to why it doesn't see the session var is that when you access the script from within the swf, it's running under a different session.

My workaround... pass the session ID to the script with FlashVars in the embed tag.

add a <param name="FlashVars" value="image=<?php echo $_SESSION['image']; ?>"> inside the project tag as well as attaching a GET-like url to the movie url in the embed tag... <embed src="mymovie.swf?image=<?php echo $_SESSION['image']; ?>">

You can only pass the variable when the swf loads, but for most uses, this is sufficient.

daveystew
07-11-2008, 02:28 PM
My assumption as to why it doesn't see the session var is that when you access the script from within the swf, it's running under a different session.

I can confirm this is true!

In Flash, I passed the external HTML page session ID in FlashVars, and in the my upload script (called by Flash), I logged all the data (including the session ID for that page) to a text file.

In the [upload.php] array, you can see the variables I logged for the upload script called by Flash.
In the [post] array, you can see the HTML page variables I passed along with the file upload.

The two session IDs are different! Very annoying that you can't use sessions, as you need to do extra checking on the server side.

Array
(
[upload.php] => Array
(
[time] => Fri 11 Jul 2008 - 02:07:42
[sessionid] => 5d6da173c5bace84aadec3a5ed1043c1 // different!
)

[post] => Array
(
[some_data] => Hello there
[passed_html_sessionid] => a6b723caa9f36edd440890f62b5bfbbb // different!
[Upload] => Submit Query
)

[files] => Array
(
[Filedata] => Array
(
[name] => hello.jpg
[type] => application/octet-stream
[tmp_name] => c:/wamp/tmp\php375.tmp
[error] => 0
[size] => 17004
)

)

)


Has anyone else seen this, or got any new ideas on it?

Cheers,
Dave