-
Hi,
well, phpdoes not utomaticlly parse xml input.
You can do it in your script, and you can probably also use "startDir=mydir" in place of a real xml object
Musicman
-
Senior Member
I ended up just using a loadVar object to sendAndLoad the directory name to the php script...and return the XML (string) back to FLASH and created an XML object to handle the return.
however I stall cant get the exDir (exclusion directory) to work correctly..
PHP Code:
<?
$startDir=$_POST["startDir"];
//$startDir='.';
$exDir="images";
function scandir($dir) {
if($handle = opendir($dir)) {
while(($file = readdir($handle)) !== false){
if($file != '.' && $file != '..' && $file != $exDir){
if(is_dir("$dir/$file")){
print "<directory label='$file' parent='$dir/$file'>";
scandir("$dir/$file");
print "</directory>";
}else {
print "<file label='$file' path='$dir/$file' />";
}
}
}
closedir($handle);
}
}
scandir($startDir);
?>
update: wow.. I never hit the send button last night....and you already replied..
is there some way to maybe check: a.) if its a directory.. and then b.) if its empty? and if so.. then either DO NOT include it in the 'return'... or maybe we can change the label its given? like Directory='$file - Empty'
would I just add another else if between the If & else already there?
if(is_dir("$dir/$file")){
print "<directory label='$file' parent='$dir/$file'>";
scandir("$dir/$file");
print "</directory>";
}else if(is_dir("$dir/$file") == directory not empty){
print "<directory label='$file - Empty' parent='$dir/$file'>";
scandir("$dir/$file");
print "</directory>";
}else {
print "<file label='$file' path='$dir/$file' />";
}
Last edited by whispers; 01-28-2007 at 12:08 PM.
-
Hi,
you need to write
global $exDir;
inside the scandir function
Musicman
-
Senior Member
hi..what does that?? if you dont mind em asking??
doesnt declaring it liek I did already create the variable??
thanks
-
up to my .as in code
Hey Whispers. Here is a script I came across at the PHP site and modified to write xml. I set it up to write it but of course you can switch it out to be inline xml (depending on the use). I also set it up to write two flavors of xml (element and attribute). Right now it writes the filename and path and will snake right down through subdirectories of the path you specify. I tried to leave the original script with the dudes directives as much as possible (so you can tap what data you want using his comments as a guide) and simply added the ability to create xml.
PHP Code:
<?php
/* The below function will list all folders and files within a directory
It is a recursive function that uses a global array. The global array was the easiest
way for me to work with an array in a recursive function
*This function has no limit on the number of levels down you can search.
*The array structure was one that worked for me.
ARGUMENTS:
$startdir => specify the directory to start from; format: must end in a "/"
$searchSubdirs => True/false; True if you want to search subdirectories
$directoriesonly => True/false; True if you want to only return directories
$maxlevel => "all" or a number; specifes the number of directories down that you want to search
$level => integer; directory level that the function is currently searching
*/
function filelist ($startdir="./", $searchSubdirs=1, $directoriesonly=0, $maxlevel="all", $level=1) {
//list the directory/file names that you want to ignore
$ignoredDirectory[] = ".";
$ignoredDirectory[] = "..";
$ignoredDirectory[] = "_vti_cnf";
global $directorylist; //initialize global array
if (is_dir($startdir)) {
if ($dh = opendir($startdir)) {
while (($file = readdir($dh)) !== false) {
if (!(array_search($file,$ignoredDirectory) > -1)) {
if (filetype($startdir . $file) == "dir") {
//build your directory array however you choose;
//add other file details that you want.
$directorylist[$startdir . $file]['level'] = $level;
$directorylist[$startdir . $file]['dir'] = 1;
$directorylist[$startdir . $file]['name'] = $file;
$directorylist[$startdir . $file]['path'] = $startdir;
if ($searchSubdirs) {
if ((($maxlevel) == "all") or ($maxlevel > $level)) {
filelist($startdir . $file . "/", $searchSubdirs, $directoriesonly, $maxlevel, $level + 1);
}
}
} else {
if (!$directoriesonly) {
//if you want to include files; build your file array
//however you choose; add other file details that you want.
$directorylist[$startdir . $file]['level'] = $level;
$directorylist[$startdir . $file]['dir'] = 0;
$directorylist[$startdir . $file]['name'] = $file;
$directorylist[$startdir . $file]['path'] = $startdir;
}}}}
closedir($dh);
}}
return($directorylist);
}
$xml="<?xml version=\"1.0\"?>\n";
$xml .="<directories>\n";
$files = filelist("./",1,0);
foreach ($files as $list) {
$xml .="<myItem path=\"".$list['path']."\" file=\"".$list['name']."\"></myItem>\n";
//$xml .="<item>\n<path>". $list['path'] ."</path>\n<filename>". $list['name'] ."</filename>\n</item>";
}
$xml .="</directories>";
$file= fopen("dirlist.xml", "w");
fwrite($file, $xml);
echo "XML File Generated";
?>
That above writes an attribute style. If you want the element style xml comment out the line running now and uncomment the one underneath it in the final "foreach" loop. Hope it helps.
Last edited by Chris_Seahorn; 01-28-2007 at 03:37 PM.
-
Hi,
in phpscript variables are local to functions (if scandir calls scandir, the two $handles are different) unless you say otherwise.
In actionscript, variables are normally global, unless you use var keyword
Musicman
-
Senior Member
Ahh.. ok thanks..
Hey Chris- Thanks man... I'll take a look at your script.. although I think Im pretty much done with this 'adventure'...but I do find PHP VERY fun & useful. I really need to learn it.
But there "IS" one last thing...
here is what I have now.. works great.. its all working as it should.. I am just trying to 'manipulate' around the way the tree component handles XML..
PHP Code:
<?
$startDir=$_POST["startDir"];
//$startDir='.';
$exDir="images";
function scandir($dir) {
global $exDir;
if($handle = opendir($dir)) {
while(($file = readdir($handle)) !== false){
if($file != '.' && $file != '..' && $file != $exDir){
if(is_dir("$dir/$file")){
print "<directory label='$file' parent='$dir/$file'>";
scandir("$dir/$file");
print "</directory>";
}else {
print "<file label='$file' path='$dir/$file' />";
}
}
}
closedir($handle);
}
}
scandir($startDir);
?>
and I want to add an else if between the if & else statements already there (I think that would be the right place)....:S..lol
but basically I want to add another 'check' (sorry...I know I have been dragging this out)...
I want to add a 'check' to see if that in fact if it IS a directory..to see if it is BLANK/EMPTY..and if so..add a 'node' to it...(STRING)
if I have a blank folder called 'emptyDir' for example.....the PHP outputs it like this:
Code:
<directory label='emptyDir' parent='./emptyDir'></directory>
I would like it to come out like this:
Code:
<directory label='emptyDir' parent='./emptyDir'><empty label='EMPTY' /></directory>
so what would be the REAL PHP equivelant of this:
PHP Code:
if(is_dir("$dir/$file")){
if("$dir/$file" == EMPTY DIRECTORY){
print "<directory label='$file' parent='$dir/$file'>";
print "<empty label='EMPTY' />";
print "</directory>";
}else{
print "<directory label='$file' parent='$dir/$file'>";
scandir("$dir/$file");
print "</directory>";
}
}else {
print "<file label='$file' path='$dir/$file' />";
}
thanks.. I think that will wrap it up.... (then I have to look into skinning the tree & comboBox component.s. (YUK)
-
Hi,
there is no "empty dir" function, and if you write one yourself, it would work similar to scandir but not generate output, only return true/false
You could change scandir to return true if it encountered anything .... and output the empty tag if scandir returned false
Musicman
-
Senior Member
hi..
I didnt think there was an 'EMPTY DIR' function... just dont know HOW to do it.
Can you help a little more? how do I check for a blank/empty directory?????? or some hints? if you dont want to post the code??
Thanks
latest version:
PHP Code:
<?
$startDir=$_POST["startDir"];
//$startDir='.';
$exDir="images";
function scandir($dir) {
global $exDir;
if($handle = opendir($dir)) {
while(($file = readdir($handle)) !== false){
if($file != '.' && $file != '..' && $file != $exDir){
if(is_dir("$dir/$file")){
print "<directory label='$file' parent='$dir/$file'>";
scandir("$dir/$file");
print "</directory>";
}else {
print "<file label='$file' path='$dir/$file' />";
}
}
}
closedir($handle);
}
}
scandir($startDir);
?>
Also... I was wondering what other data could I grab from the files as I scanning the directories?? can I grab file size as well?? and pass that back as an attribute too??
Last edited by whispers; 01-28-2007 at 07:46 PM.
-
Code:
<?
$startDir=$_POST["startDir"];
//$startDir='.';
$exDir="images";
function scandir($dir) {
$found = 0;
global $exDir;
if($handle = opendir($dir)) {
while(($file = readdir($handle)) !== false){
if($file != '.' && $file != '..' && $file != $exDir){
$found++;
if(is_dir("$dir/$file")){
print "<directory label='$file' parent='$dir/$file'>";
if(!scandir("$dir/$file")) print "<empty />";
print "</directory>";
}else {
print "<file label='$file' path='$dir/$file' />";
}
}
}
closedir($handle);
}
return $found;
}
scandir($startDir);
?>
Musicman
-
Senior Member
Thank you...
I understand this part. (I guess I didnt know it would have been that easy)
PHP Code:
if(!scandir("$dir/$file")) print "<empty label='EMPTY' />";
can you explain what the $found; var is for??
Thanks..
-
Hi,
$found just counts the number of entries (apart from ., .., and excluded ones) in a directory - so the result will be 0 or non-zero.
You could as well use $found = false; and $found = true; inside the function and use the same if(...) code to indicate empty folder
Musicman
-
Senior Member
I was wondering what other data could I grab from the files as Im scanning the directories?? can I grab file size as well?? and pass that back as an attribute too??
Is there a list of stuff I can grab (access) for EACH file/item (as long as its NOT a dir)??
Such as dimensions, date, file size? etc?
or would that mean loading this file names into an array or something and then running through it each time to get my 'property' and add it into the attribute of the 'string/xml'?
Am I making sense? sometimes I bable!
-
Hi,
you can get file size and date. For images, you can also get image size
Musicman
-
Senior Member
hahahaha.. thanks.. but how?
how about any of the other questions? what are the function/method names? so I can try to search.
thanks.
-
filesize, filetime, imagesize....
if the names are not exactly right, they are sufficiently close for the online manual to suggest the correct ones....
Musicman
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|