This is the second post in a series of 3 on how to make a Flex uploader.
For the first post, go here.
For the third post, go here.
You can download the php file here.
You can download the flex project file here.
A demo of the file is here.
Setting up the PHP
Flex needs a server-based back-end to handle where files go on the server. The PHP file here is what it uses. First, we need to make sure that the DOCUMENT_ROOT global variable is set.
if ( ! isset($_SERVER['DOCUMENT_ROOT'] ) ) $_SERVER['DOCUMENT_ROOT'] = str_replace( '\\', '/', substr( $_SERVER['SCRIPT_FILENAME'], 0, 0-strlen($_SERVER['PHP_SELF']) ) );
We then handle whatever the REQUEST['action'] variable tells us to do - this is one of the variables that was set in Flex. The code below moves the temp file to a known location, and reports back any errors.
switch($_REQUEST['action']) {
case "upload":
$file_temp = $_FILES['file']['tmp_name'];
$file_name = $_FILES['file']['name'];
$file_path = $_SERVER['DOCUMENT_ROOT']. '/'. $_REQUEST['path'];
//move the temporary file to a known storage area
$filestatus = move_uploaded_file($file_temp,$file_path . $file_name);
if(!$filestatus) {
$success = "false";
array_push($errors,"Upload failed. Please try again.");
}
break;
Finally, the php returns any errors to flex:
return_result($success,$errors,$data);
function return_result($success,$errors,$data) {
echo("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
echo ("<results>");
echo ("<success>".$success."</success>");
echo ($data);
echo ("<error>".$errors."</error>");
echo ("</results>");
}
You also need to make sure that you allow PHP write access to the directory that you are trying to write to. I use Komodo to debug this PHP and I advise you set up a PHP debug environment also.
Related posts:
- Flex Uploader / Viewer Component Part 1 - Using the FileReference Class The aim of this series of 3 blogs is to...
- Flex Uploader / Viewer Component Part 3 - Tying It All Together This is the third post in a series of 3...
- Flex TitleBox Component For a recent project I was working on, I needed...
- How to Print a Component to Disk in Flex Here is an example of how to print a component...
- Flex AJAX Bridge I’m at MAX2007, sitting in Andre Dragomir’s session, Integrating Flex...
Related posts brought to you by Yet Another Related Posts Plugin.











|
Posted by charmer on September 29, 2009 at 6:44 pm
