[FlashCodersNY] query server directory AS2?
Cadete B7
cadete at bigote7.com
Thu Sep 4 13:22:07 PDT 2008
it ended up being super simple...
<?php
$path = "images";
$dir = opendir( $path );
$output = "<?xml version='1.0' encoding='UTF-8'?>\n";
$output .= "< imagelist >\n";
while ( ( $file = readdir($dir) ) !== false )
{
if ( substr($file, 0, 1) == '.' ) continue;
//echo "filename: " . $file . "<br />";
$dotpos = strrpos( $file, '.' );
if ( $dotpos )
{
$ext = substr( $file, $dotpos+1 );
if ( $ext === 'jpg' )
{
$output .= "\t<filename>" . $file . "</filename>\n";
//echo "filename: " . $file . "<br />";
}
}
}
$output .= "</imagelist>\n";
echo ( $output );
closedir( $dir );
?>
- Wilbur
On Aug 27, 2008, at 4:24 PM, Lisa Larson~Kelley wrote:
> hi Wilbur,
>
> I have a script I'm using for my app; though it grabs user/pass from
> flash, then queries a database and returns the results in XML, it
> should be helpful. Then, I've included the code to read the contents
> of a directory in PHP. Frankenstein those two together and you
> should be golden.
>
> Hopefully you can read it in-line below; if not, let me know and
> I'll send you the files.
>
> cheers
> // Lisa
>
>
> // Lisa Larson-Kelley
> http://www.flashconnections.com
> --------------------------------------------------
> Flash Video for Professionals
> Now shipping on Amazon!
> http://tinyurl.com/2pl7ro
> --------------------------------------------------
>
> //XML EXAMPLE
> <?PHP
> define("DB_HOST", "your_host");
> define("DB_USER", "user");
> define("DB_PASSWORD", "pass");
> define("DB_NAME", "db_name");
> function DBConnect() {
> mysql_connect(DB_HOST, DB_USER, DB_PASSWORD)
> OR die("FATAL ERROR: Can't open database connection!\n");
> mysql_select_db(DB_NAME)
> OR die("FATAL ERROR: Can't find the database!\n");
> }
> //connect it
> DBConnect();
> //assign data passed from Flash to variables
> $username = mysql_real_escape_string($_POST["username"]);
> $password = mysql_real_escape_string($_POST["password"]);
>
> //Set up to print XML
> $output = "<?xml version='1.0' encoding='UTF-8'?>";
>
> //Query the db to see if un/pw is valid
> $query = ("SELECT
> account_num AS account_num,
> first_name AS first_name,
> last_name AS last_name,
> Username AS username,
> language AS language,
> acct_status AS acct_status,
> first_login AS first_login,
> email AS email,
> site_name as site_name
> FROM accounts
> WHERE Username = '$username'
> AND Password = '$password'
> ");
>
> $result = mysql_fetch_array(mysql_query($query));
>
> //If this is an account owner
> if($result){
> //start outputting the XML
> $output .= "<loginsuccess>owner</loginsuccess>\n";
> $output .="<account_num>".$result[0]."</account_num>\n";
> $output .="<first_name>".$result[1]."</first_name>";
> $output .="<last_name>".$result[2]."</last_name>";
> $output .="<username>".$result[3]."</username>";
> $output .="<language>".$result[4]."</language>";
> $output .="<acct_status>".$result[5]."</acct_status>";
> $output .="<first_login>".$result[6]."</first_login>";
> $output .="<email>".$result[7]."</email>";
> $output .="<site_name>".$result[8]."</site_name>";
> }
> else{
> $guestquery = ("SELECT
> account_num AS account_num,
> guest_username AS guest_username
> FROM guests
> WHERE guest_username = '$username'
> AND guest_password = '$password'
> ");
> $guestresult = mysql_fetch_array(mysql_query($guestquery));
>
> if($guestresult){
> //start outputting the XML
> $output .= "<loginsuccess>guest</loginsuccess>\n";
> $output .= "<account_num>".$guestresult[0]."</account_num>";
> $output .= "<username>".$guestresult[1]."</username>";
> }
> else if(!$guestresult){
> $output .= "<loginsuccess>no</loginsuccess>\n";
> }
> }
>
> //output all the XML
> echo ($output);
>
> //close the connection
> mysql_close();
> ?>
>
> //DIRECTORY READER
> <html>
> <head>
> <title>Directory Listing</title>
> <style type="text/css">
> <!--
> h2 {
> font-family: Georgia, "Times New Roman", Times, serif;
> font-size: 18px;
> font-weight: bold;
> color: #666666;
> }
> body {
> font-family: Georgia, "Times New Roman", Times, serif;
> font-size: 12px;
> line-height: 15px;
> font-weight: normal;
> color: #333333;
> padding-left: 6px;
> }
> hr {
> color: #6699FF;
> background-color: #6699FF;
> }
> li {
> color: #6699FF;
> list-style-type: square;
> }
> -->
> </style>
> </head>
> <body>
>
> <h2>PAGE TITLE HERE</h2>
> <p>contents:</p>
>
> <?php
> // TASK: display a directory listing with thumbnails for images and
> human-readable filesizes
>
> // handy humansize function:
> // input is number of bytes, output is a "human-readable" filesize
> string
> function humansize($size) {
> // Setup some common file size measurements.
> $kb = 1024; // Kilobyte
> $mb = 1024 * $kb; // Megabyte
> $gb = 1024 * $mb; // Gigabyte
> $tb = 1024 * $gb; // Terabyte
>
> if($size < $kb) return $size."B";
> else if($size < $mb) return round($size/$kb,0)."KB";
> else if($size < $gb) return round($size/$mb,0)."MB";
> else if($size < $tb) return round($size/$gb,0)."GB";
> else return round($size/$tb,2)."TB";
> }
> // get local directory path
> $path= dirname($_SERVER['SCRIPT_FILENAME']);
> ?>
>
> <!--<h3>Files in <?php print $path; ?>:</h3>-->
> <hr width="100%" noshade size="1" color="#6699FF">
> <table>
>
> <?php
> $d = dir($path);
> $icon = '';
>
> while (false !== ($entry = $d->read())) {
> if ( substr($entry, 0, 1)=='.' ) continue;
> // get size
> $size = filesize($path.'/'.$entry);
> $humansize = humansize($size);
>
> // find filename extension
> $dotpos = strrpos($entry, '.');
>
> if ($dotpos) {
> $ext = substr($entry, $dotpos+1);
> if ($ext === 'jpg' || $ext === 'gif' || $ext === 'png') {
> $icon = "<img src='$entry' style='width: 48px; height:
> auto; vertical-align: text-top;' alt='icon' title='$entry' />";
> }
> }
>
> if ($entry !== 'index.php'){
> print "<tr><td valign=top><ul><li><a href='$entry'>$entry</a></
> li></ul></td><td>($humansize) $icon</td></tr>\n";
> }
> $icon= '';
> }
> $d->close();
> ?>
> </table>
> <hr width="100%" noshade size="1" color="#6699FF">
> _______________________________________________
> FlashCodersNY mailing list
> FlashCodersNY at flashcodersny.org
> http://mail.flashcodersny.org/mailman/listinfo/flashcodersny_flashcodersny.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.flashcodersny.org/pipermail/flashcodersny_flashcodersny.org/attachments/20080904/7f0601c5/attachment.html
More information about the FlashCodersNY
mailing list