this is what I am trying to do I am trying to get all the pictures in a directory and feed them into a php array then I want to take that array and feed it to a javascript array so I can use them with javascript
any body now how I can do this or if this can be done
like i want to use them for an image gallery that I am trying make. its going to use OnmouseOver and OnmouseOut events I need a way to get all the files in the directory thats where php comes in then I want to use javascript to reference them on the page.
getting the values of a php array into a javascript array.
Moderator: General Moderators
Hi
Here is a simple example, (function)
printf
Here is a simple example, (function)
Code: Select all
<?
function get_list ( $path, $ok )
{
$files = array ();
if ( $dir = opendir ( $path ) )
{
while ( ( $file = readdir ( $dir ) ) !== false )
{
if ( $file != '.' && $file != '..' && is_file ( $path . '/' . $file ) && in_array ( substr ( $file, ( strrpos ( $file, '.' ) + 1 ) ), $ok ) )
{
$filesї] = $file;
}
}
closedir ( $dir );
}
return ( $files );
}
// the array of file types to return
$ext = array ( 'gif', 'jpg', 'jpeg', 'bmp', 'png', 'tif' );
// the directory to read, no trailing -> '/'
$dir = '/path_to_directory/read';
// usage
$out = get_list ( $dir, $ext );
if ( empty ( $out ) )
{
echo 'No files match allowed types found';
exit ();
}
else
{
$javasript_array = "'" . implode ( "', '", $out ) . "'";
}
// then on your output page header, inside of PHP (example)
echo "var img = new Array(" . $javasript_array . ");";
?>printf