コード例 #1
0
ファイル: import.php プロジェクト: andy-profi/bxApiDocs
 protected static function downloadFile($fileName, $storeAs, $skip404 = false)
 {
     $storeTo = $_SERVER['DOCUMENT_ROOT'] . self::LOCAL_PATH;
     if (file_exists($storeTo)) {
         if (!is_writable($storeTo)) {
             throw new Main\SystemException('Temporal directory is not writable by the current user');
         }
     } else {
         $dir = new IO\Directory($_SERVER['DOCUMENT_ROOT']);
         $dir->createSubdirectory(self::LOCAL_PATH);
     }
     $storeTo .= $storeAs;
     if (file_exists($storeTo)) {
         if (!is_writable($storeTo)) {
             throw new Main\SystemException('Cannot remove previous ' . $storeAs . ' file');
         }
         unlink($storeTo);
     }
     $query = 'http://' . self::DISTRIBUTOR_HOST . ':' . self::DISTRIBUTOR_PORT . self::REMOTE_PATH . $fileName;
     $client = new HttpClient();
     if (!$client->download($query, $storeTo)) {
         $eFormatted = array();
         foreach ($client->getError() as $code => $desc) {
             $eFormatted[] = trim($desc . ' (' . $code . ')');
         }
         throw new Main\SystemException('File download failed: ' . implode(', ', $eFormatted) . ' (' . $query . ')');
     }
     $status = intval($client->getStatus());
     if ($status != 200 && file_exists($storeTo)) {
         unlink($storeTo);
     }
     $okay = $status == 200 || $status == 404 && $skip404;
     // honestly we should check for all 2xx codes, but for now this is enough
     if (!$okay) {
         throw new Main\SystemException('File download failed: http error ' . $status . ' (' . $query . ')');
     }
     // charset conversion here?
     return filesize($storeTo);
 }