-1

I'm having an issue with my ajax function returning "Cannot modify header information - headers already sent"

Here is my code:

    $result = array();

    function listFolderFiles($dir){

        $ffs = scandir($dir);

        foreach ( $ffs as $ff ){

            if ( $ff != '.' && $ff != '..'){
                if ( strlen($ff)>=5 ) {
                    if ( substr($ff, -4) == '.jpg' ||  substr($ff, -4) == '.png' ||  substr($ff, -4) == '.jpeg' ) {
                        $timestamp = explode('/',$dir);
                        $obj['name'] = ''.$timestamp[8].'/'.$ff;
                        $obj['size'] = filesize($dir.'/'.$ff);
                        $result[] = $obj;
                    }
                }
                if( is_dir($dir.'/'.$ff) )
                    listFolderFiles($dir.'/'.$ff);
            }

        }
        if(!empty($result)) {
            header('Content-type: text/json');
            header('Content-type: application/json');
            echo json_encode($result);
        } else {
           die;
        }
    }
    $files = array();
    $files = listFolderFiles($fullUploadDir);

Does anybody have any ideas as to how I may solve this issue?

Thanks, Codarz360

Codarz360
  • 424
  • 1
  • 6
  • 16
  • the error means you have output before calling `header(` - which makes setting the headers impossible - look around this has been asked about a million times. – birdspider Nov 19 '14 at 17:22
  • Protip: Output before `header()` calls can also be caused by error messages! – Cobra_Fast Nov 19 '14 at 17:23

1 Answers1

1

You are calling this function recursively, and in each instance you have the possibility to send the headers and the content more than once, hence, the headers are already sent.

Flosculus
  • 6,880
  • 3
  • 18
  • 42