title: PHP Warning: sort() expects parameter 1 to be array, null given
body: <p>I'm struggling with my php script, i used this as my reference: <a href="http://www.dynamicdrive.com/forums/showthread.php?60691-Listing-file-in-a-directory-problem" rel="nofollow">http://www.dynamicdrive.com/forums/showthread.php?60691-Listing-file-in-a-directory-problem</a>.</p>
  
  <p>I have adapted it to suit me a bit better, however i can't seem to work out how to fix this PHP Error: </p>
  
  <blockquote>
    <p>PHP Warning:  sort() expects parameter 1 to be array, null given</p>
  </blockquote>
  
  <p>I understand the error is on line 83 </p>
  
  <blockquote>
    <p>sort($dirArray);</p>
  </blockquote>
  
  <p>However i'm not sure how to fix the error, because i want that forbidden     files function to stay where it is, if i alter this:</p>
  
  <pre><code>  // Gets each entry
    while($entryName = readdir($myDirectory))
    {
      if (is_file($entryName))
      {
        $extensions = explode(".", $entryName);
          if(!in_array($extensions[1],$forbiddenExtensions))
          {
                $dirArray[] = $entryName;
          }
      }
    } 
  </code></pre>
  
  <p>To this:</p>
  
  <pre><code>while($entryName = readdir($myDirectory)) 
  {
      $dirArray[] = $entryName;
  }
  </code></pre>
  
  <p>I know it will be fine. I'm 100% positive theirs a simple solution, my brains just that fried I can't think anymore after 156 trials of re-editing, re-uploading, I'm in need of help. Can someone please help me find a workaround with my $forbiddenExtensions?</p>
  
  <pre><code>&lt;?php
    // Open this directory
    // (NOTE: To open same folder as this file use($directory = "./";)
    $directory = "./files/";
  
    // Selects $directory to open
    $myDirectory = opendir($directory);
  
    // Sets forbidden extensions
    $forbiddenExtensions = array("php", "html", "log");
  
    // Gets each entry
    while($entryName = readdir($myDirectory))
    {
      if (is_file($entryName))
      {
        $extensions = explode(".", $entryName);
          if(!in_array($extensions[1],$forbiddenExtensions))
          {
            $dirArray[] = $entryName;
          }
      }
    }
  
    // This function separates the extension from the rest of the file name and returns it
    function findExtensions ($filename)
    {
      $filename = strtolower($filename) ;
      $extensions = explode(".", $filename) ; //$extensions = split("[/\\.]", $filename) ;
      $n = count($extensions)-1;
      $extensions = $extensions[$n];
      return $extensions;
    }
  
    // Closes $directory
    closedir($myDirectory);
  
    // Counts elements in array
    $indexCount = count($dirArray);
    Print ("$indexCount files&lt;br&gt;\n"); //Display how many files were counted
  
    // Sorts the Array of files
    sort($dirArray);
  
    // Prints the Array of files
    print("&lt;table width='100%' cellspacing='10'&gt;
    &lt;tr&gt;
      &lt;td class='database_head'&gt;Filename&lt;/td&gt;
      &lt;td class='database_head'&gt;Filetype&lt;/td&gt;
      &lt;td class='database_head'&gt;Filesize&lt;/td&gt;
    &lt;/tr&gt;\n");
  
    // Loops through the array of files and display them all
    for($index=0; $index &lt; $indexCount; $index++) 
    {
      // Don't list hidden files
      if (substr($dirArray[$index], 0, 1) != ".")
      {
        print("&lt;tr&gt;&lt;td&gt;&lt;a href=\"$dirArray[$index]\"&gt;$dirArray[$index]&lt;/a&gt;&lt;/td&gt;");
        print("&lt;td&gt;");
        print(findExtensions($dirArray[$index]));
        print("&lt;/td&gt;");
        print("&lt;td&gt;");
        print(filesize($directory.$dirArray[$index]));
        print("&lt;/td&gt;");
        print("&lt;/tr&gt;\n");
      }
    }
    print("&lt;/table&gt;\n");
  ?&gt;
  </code></pre>