Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

wtf!?

Name: Anonymous 2011-03-06 0:51

er... why doesnt sort() do what I am expecting here?

<?
Header("content-type: application/x-javascript");
$pathstring=pathinfo($_SERVER['PHP_SELF']);
$locationstring="http://" . $_SERVER['HTTP_HOST'].$pathstring['dirname'] . "/";

function returnimages($dirname=".") {
     $pattern="(\.jpg$)|(\.png$)|(\.jpeg$)|(\.gif$)";
   $files = array();
     $curimage=0;
   if($handle = opendir($dirname)) {
       while(false !== ($file = readdir($handle))){
               if(eregi($pattern, $file)){
                     echo 'picsarray[' . $curimage .']="' . $file . '";';
                     $curimage++;
               }
       }
       closedir($handle);
   }
   $files = sort($files);
   return($files);
}

echo 'var locationstring="' . $locationstring . '";';
echo 'var picsarray=new Array();';
returnimages()
?>

Name: Anonymous 2011-03-06 6:55

ok, how about this then:
why does the first codeblock work fine
and the second codeblock just renders a blank page in firefox 3.6.14 ?


<?php
    $images = array();
    $pattern="/(\.jpg$)|(\.png$)|(\.jpeg$)|(\.gif$)/";
    if($handle = opendir(".")) {
        while(false !== ($file = readdir($handle))) {
            if(preg_match($pattern, $file)) {
                array_push($images, $file);
            }
        }
        closedir($handle);
    }
    if (!function_exists('json_encode'))
    {
      function json_encode($a=false)
      {
        if (is_null($a)) return 'null';
        if ($a === false) return 'false';
        if ($a === true) return 'true';
        if (is_scalar($a))
        {
          if (is_float($a))
          {
            // Always use "." for floats.
            return floatval(str_replace(",", ".", strval($a)));
          }
          if (is_string($a))
          {
            static $jsonReplaces = array(array("\\", "/", "\n", "\t", "\r", "\b", "\f", '"'), array('\\\\', '\\/', '\\n', '\\t', '\\r', '\\b', '\\f', '\"'));
            return '"' . str_replace($jsonReplaces[0], $jsonReplaces[1], $a) . '"';
          }
          else
            return $a;
        }
        $isList = true;
        for ($i = 0, reset($a); $i < count($a); $i++, next($a))
        {
          if (key($a) !== $i)
          {
            $isList = false;
            break;
          }
        }
        $result = array();
        if ($isList)
        {
          foreach ($a as $v) $result[] = json_encode($v);
          return '[' . join(',', $result) . ']';
        }
        else
        {
          foreach ($a as $k => $v) $result[] = json_encode($k).':'.json_encode($v);
          return '{' . join(',', $result) . '}';
        }
      }
    }
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="application/xml; charset=UTF-8" />
        <title>/photos</title>
        <style>
            *
            {
                margin: 0px;
                padding: 0px;
                font-family: "Georgia", serif;
            }
            html, body
            {
                width: 100%;
                height: 100%;
            }
        </style>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js">
        </script>
        <script>
            $(function() {
                var images = <?php print(json_encode($images)); ?>;
                images.sort();
                var images_lol_at_once = 10;
                var image = 0;
                var func = function() {
                    console.log(image);
                    if($("body").scrollTop() > $("#images").height() - $(window).height() - 100) {
                        var end = image + images_lol_at_once;
                        for( ; image < end; image++) {
                            if(image >= images.length) {
                                clearInterval(interval);
                                return;
                            }
                            $("#images").append('<alt="'+images[image]+'"/ img src="'+images[image]+'"/>');
                        }
                    }
                };
                func();
                var interval = setInterval(func, 1000);
            });
        </script>
    </head>
    <body>
        <div id="images"></div>
    </body>
</html>




<?php
    $images = array();
    $pattern="/(\.jpg$)|(\.png$)|(\.jpeg$)|(\.gif$)/";
    if($handle = opendir(".")) {
        while(false !== ($file = readdir($handle))) {
            if(preg_match($pattern, $file)) {
                array_push($images, $file);
            }
        }
        closedir($handle);
    }
    if (!function_exists('json_encode'))
    {
      function json_encode($a=false)
      {
        if (is_null($a)) return 'null';
        if ($a === false) return 'false';
        if ($a === true) return 'true';
        if (is_scalar($a))
        {
          if (is_float($a))
          {
            // Always use "." for floats.
            return floatval(str_replace(",", ".", strval($a)));
          }
          if (is_string($a))
          {
            static $jsonReplaces = array(array("\\", "/", "\n", "\t", "\r", "\b", "\f", '"'), array('\\\\', '\\/', '\\n', '\\t', '\\r', '\\b', '\\f', '\"'));
            return '"' . str_replace($jsonReplaces[0], $jsonReplaces[1], $a) . '"';
          }
          else
            return $a;
        }
        $isList = true;
        for ($i = 0, reset($a); $i < count($a); $i++, next($a))
        {
          if (key($a) !== $i)
          {
            $isList = false;
            break;
          }
        }
        $result = array();
        if ($isList)
        {
          foreach ($a as $v) $result[] = json_encode($v);
          return '[' . join(',', $result) . ']';
        }
        else
        {
          foreach ($a as $k => $v) $result[] = json_encode($k).':'.json_encode($v);
          return '{' . join(',', $result) . '}';
        }
      }
    }
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="application/xml; charset=UTF-8" />
        <title>/photos</title>
        <style>
            *
            {
                margin: 0px;
                padding: 0px;
                font-family: "Georgia", serif;
            }
            html, body
            {
                width: 100%;
                height: 100%;
            }
        </style>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js">
        </script>
        <script>
            $(function() {
                var images = <?php print(json_encode($images)); ?>;
                images.sort();
                for(var i in images) {
                    $("#images").append('<img src="'+images[i]+'"/>');
                }
            });
        </script>
    </head>
    <body>
        <div id="images"></div>
    </body>
</html>

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List