Php obtiene un archivo jpg de una carpeta como matriz
Frecuentes
Visto 2,437 veces
3
help me to get files from a folder in array.
I am trying to get all jpg file name in array
de una carpeta images
.
and after that use rand
to change css background Randomly.
JPG files from a folder in arry;
$images=array("image1.jpg", "image2.jpg");
luego usa el rand
to load images randomly
echo '<style>body{background:url('.$images[array_rand($images)].')no-repeat;';
2 Respuestas
4
Pass a directory to escandir to get an array of all files in that directory. Maybe use matriz_filtro then to filter out any non-images by file extension.
$files = scandir( '/image/path' );
function images_only( $file )
{
return preg_match( '/\.(gif|jpg|png)$/i', $file );
}
$files = array_filter( $files, 'images_only' );
$ archivos should now contain only the images from the image path.
Respondido 26 ago 12, 23:08
1
glob it
editado with array_rand fix
$images = glob("images/*.jpg");
// may want to verify that the $images array has elements before echoing
echo '<style>body{background:url('.$images[array_rand($images)].') no-repeat;';
Respondido 26 ago 12, 22:08
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas php random or haz tu propia pregunta.
Sooo... What's the question? Do you have what you've tried that's inadequate? - Jared Farrish
I dont know how to get files from a folder in array - user1289939
Manual is your friend - find function written by williamcomartin at gmail dot com - Peter
glob:
echo '<style>body{background:url('.array_rand(glob("images/*.jpg")).')no-repeat;';
- Aaron W.@AaronW. - I'm guessing that would probably make a good answer, although there are other ways too. - Jared Farrish