Skip to main content
Participating Frequently
August 6, 2009
Question

php array to flash array issues

  • August 6, 2009
  • 1 reply
  • 1657 views

Here's what I'm trying to do:

1. php gathers filenames from folder
2. flash loads the array from the php and creates a usable array out of it
3. I will then use the array to load the files.

I'm having a problem loading the php array. That's where I am stuck right now. I've tried various suggested solutions but I keep running into errors.

The error I'm getting right now is

ReferenceError: Error #1069: Property $stringall not found on String and there is no default value.
   at Appleby_portfolio_02_fla::MainTimeline/onFileLoaded()
   at flash.events::EventDispatcher/dispatchEventFunction()
   at flash.events::EventDispatcher/dispatchEvent()
   at flash.net::URLLoader/onComplete()


My php code is:
<?php
/**
* This funtion will take a pattern and a folder as the argument and go thru it(recursivly if needed)and return the list of
*               all files in that folder.
* Link             : http://www.bin-co.com/php/scripts/filesystem/ls/
* Arguments     :  $pattern - The pattern to look out for [OPTIONAL]
*                    $folder - The path of the directory of which's directory list you want [OPTIONAL]
*                    $recursivly - The funtion will traverse the folder tree recursivly if this is true. Defaults to false. [OPTIONAL]
*                    $options - An array of values 'return_files' or 'return_folders' or both
* Returns       : A flat list with the path of all the files(no folders) that matches the condition given.
*/
function ls($pattern="*", $folder="Photography/", $recursivly=false, $options=array('return_files','return_folders')) {
    if(
$folder) {
       
$current_folder = realpath('.');
        if(
in_array('quiet', $options)) { // If quiet is on, we will suppress the 'no such folder' error
           
if(!file_exists($folder)) return array();
        }
       
        if(!
chdir($folder)) return array();
    }
   
   
   
$get_files    = in_array('return_files', $options);
   
$get_folders= in_array('return_folders', $options);
   
$both = array();
   
$folders = array();
   
   
// Get the all files and folders in the given directory.
   
if($get_files) $both = glob($pattern, GLOB_BRACE + GLOB_MARK);
   
$folders = glob("*", GLOB_ONLYDIR + GLOB_MARK);
   
   
//If a pattern is specified, make sure even the folders match that pattern.
   
$matching_folders = array();
    if(
$pattern !== '*') $matching_folders = glob($pattern, GLOB_ONLYDIR + GLOB_MARK);
   
   
//Get just the files by removing the folders from the list of all files.
   
$all = array_values(array_diff($both,$folders));
       
    if(
$recursivly or $get_folders) {
        foreach (
$folders as $this_folder) {
            if(
$get_folders) {
               
//If a pattern is specified, make sure even the folders match that pattern.
               
if($pattern !== '*') {
                    if(
in_array($this_folder, $matching_folders)) array_push($all, $this_folder);
                }
                else
array_push($all, $this_folder);
            }
           
            if(
$recursivly) {
               
// Continue calling this function for all the folders
               
$deep_items = ls($pattern, $this_folder, $recursivly, $options); # :RECURSION:
               
foreach ($deep_items as $item) {
                   
array_push($all, $this_folder . $item);
                }
            }
        }
    }
   
    if(
$folder) chdir($current_folder);
      
$stringall = implode ('|', $all);

    return
$stringall;
}


The as3 code is:

import flash.display.Loader;
import flash.events.*;
import flash.net.*;


var myRequest:URLRequest;
var myLoader:URLLoader;


myRequest = new URLRequest("Photography2.php/");
myLoader = new URLLoader();
myLoader.dataFormat = "VARIABLES";
myLoader.addEventListener(Event.COMPLETE, onFileLoaded);
myLoader.addEventListener(IOErrorEvent.IO_ERROR, onLoadError);
myLoader.load(myRequest);


function onLoadError(e:IOErrorEvent):void
{
     trace(e.toString());
     myLoader.removeEventListener(Event.COMPLETE, onFileLoaded);
     myLoader.removeEventListener(IOErrorEvent.IO_ERROR, onLoadError);
}
function onFileLoaded(ev:Event):void
{
     myLoader.removeEventListener(Event.COMPLETE, onFileLoaded);
     myLoader.removeEventListener(IOErrorEvent.IO_ERROR, onLoadError);
var fileNames = ev.target.data.stringall.split("|"); //added in by me
trace(fileNames);
fileNames_txt = fileNames;
}

Any help you can provide would be very much appreciated. I've been working on this for a really long time and I'm getting super frustrated with it.

Thanks in advance.

This topic has been closed for replies.

1 reply

webqaflash
Inspiring
August 6, 2009

I don't know whether an array can be passed from PHP to Flash.

When I want to access a group of values from php I prefer using XML

In Php you can generate an XML and echo it

When I want to retrieve name of images

echo '<?xml version="1.0" encoding="UTF-8"?>
<slideshow>
    <image src="images/homeImg.jpg"  />
    <image src="images/mouse.jpg" />
    <image src="images/country.jpg" />
    <image src="images/rope.jpg" />
    <image src="images/flower.jpg" />
</slideshow>';

(This can be easily generated in php);

Then in flash

          var strPHPPath = "slideshow.php"


            // create new urlloader for xml file
          var  xmlLoader = new URLLoader();


            // add listener for complete event
            xmlLoader.addEventListener(Event.COMPLETE, onXMLLoadComplete);


            // load xml file
            xmlLoader.load(new URLRequest(strXMLPath));

      function onXMLLoadComplete(e:Event):void {
            // create new xml with the received data
           var  xmlSlideshow = new XML(e.target.data);
            // get total slide count
            trace(xmlSlideshow..image.length());//get the length of data

          xmlSlideshow..image[1].@src)//the first name of image

        }

Hope this will help

Participating Frequently
August 6, 2009

Hey thanks for the reply. I'll give that a try and let you know how it goes. *fingers crossed*