Search code examples
phpglob

PHP glob() returns an empty array


I have this code:

<?php
    session_start();

    if( isset( $_SESSION[ 'user' ] ) )
    {
        $pictures = glob( "http://localhost/wandelverhalen/cms/newPictures/*.*" );
        echo count( $pictures );
        echo "\n";
        echo( $_SESSION[ 'user' ] );
     }else{
        echo('You are not logged on.');
     }
?>

I have 50 files in the directory http://localhost/wandelverhalen/cms/newPictures. But the code echoes: 0 Johannes

This is the javascript that calls the php-code:

$.ajax({
    method: 'post',
    url: url,
    dataType: 'json',
    success: function( response ){
        var data = response;
        console.log( data );
    },
    error: function( err ){
        console.log( err.responseText );
    }
});

I found various answers to similar questions, but none of them solve my problem.

Help is very much appreciated.


Solution

  • You should provide the absolute path to glob function

    __DIR__."/wandelverhalen/cms/newPictures/*.*"
    

    Use for all type images

    glob(__DIR__."/wandelverhalen/cms/newPictures/*.{jpg,png,gif}", GLOB_BRACE)