Eso lo puedes hacer mucho más sencillo con JSON:
   
Código PHP:
Ver original- <?php 
- $directory = 'images/slider' . $row['imageName']; 
- try {     
-     // Styling for images 
-     foreach ( new DirectoryIterator($directory) as $item ) {             
-         if ($item->isFile()) { 
-             $path =  $directory . "/" . $item; 
-             $photos[] = array('image' => $path); 
-         } 
-     } 
- } 
- catch(Exception $e) { 
- // aqui deberías hacer algo... 
- } 
- ?> 
- var photos = <?php echo $jsonPhotos; ?>; 
Saludos.