Php obtiene un archivo jpg de una carpeta como matriz

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;';

preguntado el 26 de agosto de 12 a las 15:08

Sooo... What's the question? Do you have what you've tried that's inadequate? -

I dont know how to get files from a folder in array -

Manual is your friend - find function written by williamcomartin at gmail dot com -

glob: echo '<style>body{background:url('.array_rand(glob("images/*.jpg")).')no-repeat;'; -

@AaronW. - I'm guessing that would probably make a good answer, although there are other ways too. -

2 Respuestas

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

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

Is the path correct? Try debugging to make sure it's populating correctly - Aaron W.

array_rand() devuelve un azar clave solo. - Salathe

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.