连接到php中的url

I want through my project to create a cron script to connect to another server that has a remote url: http: // test / Folder /. and in the Folder ago csv file then I would get them and put them in my local application.

$url = 'http://test/Folder/';
    $url=str_replace(" ", "%20", $url);
        $infos = pathinfo($url);
        //if (preg_match('/\b(https?|http?):\/\/*/', $url) !== 1)
        /*{
            echo "<h1>Téléchargement impossible !</h1><br />
                Le fichier demandé n'est pas disponible (<b>".$infos['basename']."</b>).";
            die;
        }*/

        $username = 'test';
        $password = 'test';
        $context = stream_context_create(array(
            'http' => array(
                'header'  => "Authorization: Basic " . base64_encode("$username:$password")
            )
        ));

        $data = file_get_contents($url, true, $context);

       /* $headers = get_headers($url, 1);
        header("Cache-Control: no-cache");
        header("Expires: -1");
        header("Content-Type: ".$headers['Content-Type'].";");
        header("Content-Disposition: attachment; filename=\"" . $infos['basename'] . "\";");
        header("Content-Transfer-Encoding: binary");
        header("Content-Length: " . $headers['Content-Length']);*/

       /* if(!is_dir($data)){
            $message =  'Erreur: Lors de la connexion!';
            die($message);
        }*/

        //ouvre le contenu du dossier courant
        $fichierSimpac = array(); // on déclare le tableau contenant le nom des fichiers
        $fichierDate = array();
        $simpacPlusRecent = '';
        // TODO mieux que ça
        if($dir = opendir($url)){
            while($element = readdir($dir)){
                if($element != '.' && $element != '..' && strrchr($element, '.') == '.csv'){
                    if (!is_dir($url.'/'.$element)) {
                        $fichierSimpac[] = $element;
                        $date            = explode('_', $element);
                        $fichierDate[]   = $date[3].$date[4].$date[5];
                    }
                }
            }
            closedir($dir);

            if(!empty($fichierSimpac) && !empty($fichierDate)){
                arsort($fichierDate, SORT_REGULAR); // Tries du plus grand au plus petit
                reset($fichierDate); // On place le pointeur au début
                $simpacPlusRecent = $fichierSimpac[key($fichierDate)];
            }
            return $url.$simpacPlusRecent;
        }

I want through my project to create a cron script to connect to another server that has a remote url: http: // test / Folder /. and in the Folder ago csv file then I would get them and put them in my local application. like this :

$url = 'http://test/Folder/';
$url=str_replace(" ", "%20", $url);
    $infos = pathinfo($url);
    //if (preg_match('/\b(https?|http?):\/\/*/', $url) !== 1)
    /*{
        echo "<h1>Téléchargement impossible !</h1><br />
            Le fichier demandé n'est pas disponible (<b>".$infos['basename']."</b>).";
        die;
    }*/

    $username = 'test';
    $password = 'test';
    $context = stream_context_create(array(
        'http' => array(
            'header'  => "Authorization: Basic " . base64_encode("$username:$password")
        )
    ));

    $data = file_get_contents($url, true, $context);

   /* $headers = get_headers($url, 1);
    header("Cache-Control: no-cache");
    header("Expires: -1");
    header("Content-Type: ".$headers['Content-Type'].";");
    header("Content-Disposition: attachment; filename=\"" . $infos['basename'] . "\";");
    header("Content-Transfer-Encoding: binary");
    header("Content-Length: " . $headers['Content-Length']);*/

   /* if(!is_dir($data)){
        $message =  'Erreur: Lors de la connexion!';
        die($message);
    }*/

    //ouvre le contenu du dossier courant
    $fichierSimpac = array(); // on déclare le tableau contenant le nom des fichiers
    $fichierDate = array();
    $simpacPlusRecent = '';
    // TODO mieux que ça
    if($dir = opendir($url)){
        while($element = readdir($dir)){
            if($element != '.' && $element != '..' && strrchr($element, '.') == '.csv'){
                if (!is_dir($url.'/'.$element)) {
                    $fichierSimpac[] = $element;
                    $date            = explode('_', $element);
                    $fichierDate[]   = $date[3].$date[4].$date[5];
                }
            }
        }
        closedir($dir);

        if(!empty($fichierSimpac) && !empty($fichierDate)){
            arsort($fichierDate, SORT_REGULAR); // Tries du plus grand au plus petit
            reset($fichierDate); // On place le pointeur au début
            $simpacPlusRecent = $fichierSimpac[key($fichierDate)];
        }
        return $url.$simpacPlusRecent;
    }

    return $simpacPlusRecent;