fputcsv example

if (false === file_put_contents($file$data, \LOCK_EX)) {
            return false;
        }

        if (!$profileIndexed) {
            // Add to index             if (false === $file = fopen($this->getIndexFilename(), 'a')) {
                return false;
            }

            fputcsv($file[
                $profile->getToken(),
                $profile->getIp(),
                $profile->getMethod(),
                $profile->getUrl(),
                $profile->getTime() ?: time(),
                $profile->getParentToken(),
                $profile->getStatusCode(),
            ]);
            fclose($file);

            if (1 === mt_rand(1, 10)) {
                

class CsvFileDumper extends FileDumper
{
    private string $delimiter = ';';
    private string $enclosure = '"';

    public function formatCatalogue(MessageCatalogue $messages, string $domain, array $options = []): string
    {
        $handle = fopen('php://memory', 'r+');

        foreach ($messages->all($domain) as $source => $target) {
            fputcsv($handle[$source$target]$this->delimiter, $this->enclosure);
        }

        rewind($handle);
        $output = stream_get_contents($handle);
        fclose($handle);

        return $output;
    }

    /** * Sets the delimiter and escape character for CSV. * * @return void */
foreach ($data as &$value) {
            $flattened = [];
            $this->flatten($value$flattened$keySeparator, '', $escapeFormulas);
            $value = $flattened;
        }
        unset($value);

        $headers = array_merge(array_values($headers)array_diff($this->extractHeaders($data)$headers));
        $endOfLine = $context[self::END_OF_LINE] ?? $this->defaultContext[self::END_OF_LINE];

        if (!($context[self::NO_HEADERS_KEY] ?? $this->defaultContext[self::NO_HEADERS_KEY])) {
            fputcsv($handle$headers$delimiter$enclosure$escapeChar);
            if ("\n" !== $endOfLine && 0 === fseek($handle, -1, \SEEK_CUR)) {
                fwrite($handle$endOfLine);
            }
        }

        $headers = array_fill_keys($headers, '');
        foreach ($data as $row) {
            fputcsv($handlearray_replace($headers$row)$delimiter$enclosure$escapeChar);
            if ("\n" !== $endOfLine && 0 === fseek($handle, -1, \SEEK_CUR)) {
                fwrite($handle$endOfLine);
            }
        }
$this->Front()->Plugins()->Json()->setRenderer(false);
        $this->Response()->headers->set('content-type', 'text/csv; charset=utf-8');
        $this->Response()->headers->set('content-disposition', 'attachment;filename=' . $this->getCsvFileName());

        echo "\xEF\xBB\xBF";
        $fp = fopen('php://output', 'w');
        if (!\is_resource($fp)) {
            throw new RuntimeException('Could not open stream');
        }

        if (\is_array($data[0])) {
            fputcsv($fparray_keys($data[0]), ';');
        }

        foreach ($data as $value) {
            if (empty($value)) {
                continue;
            }
            fputcsv($fp$value, ';');
        }
        fclose($fp);
    }

    
$resultArray = $dataQuery->getArrayResult();

        $this->Response()->headers->set('content-type', 'text/csv; charset=utf-8');
        $this->Response()->headers->set('content-disposition', 'attachment;filename=partner_statistic.csv');
        // Use this to set the BOM to show it in the right way for Excel and stuff         echo "\xEF\xBB\xBF";
        $fp = fopen('php://output', 'w');
        if (!\is_resource($fp)) {
            throw new RuntimeException('Could not open temporary stream');
        }
        if (\is_array($resultArray[0])) {
            fputcsv($fparray_keys($resultArray[0]), ';');
        }

        foreach ($resultArray as $value) {
            $value['orderTime'] = $value['orderTime']->format('d-m-Y');
            $value['netTurnOver'] = number_format((float) $value['netTurnOver'], 2, ',', '.');
            $value['provision'] = number_format((float) $value['provision'], 2, ',', '.');
            fputcsv($fp$value, ';');
        }
        fclose($fp);
    }

    
$dataQuery = $this->getVoucherRepository()->getVoucherCodeListQuery($voucherId);
        $resultArray = $dataQuery->getArrayResult();

        $this->Response()->headers->set('content-type', 'text/csv; charset=utf-8');
        $this->Response()->headers->set('content-disposition', 'attachment;filename=voucherCodes.csv');
        // use this to set the BOM to show it in the right way for excel and stuff         echo "\xEF\xBB\xBF";
        $fp = fopen('php://output', 'w');
        if (!\is_resource($fp)) {
            throw new RuntimeException('Could not open temporary stream');
        }
        fputcsv($fparray_keys($resultArray[0]), ';');

        foreach ($resultArray as $value) {
            fputcsv($fp$value, ';');
        }
        fclose($fp);
    }

    /** * Action for the Detail Voucher Form to load all needed data * * @return void */
foreach ($data as &$value) {
            $flattened = [];
            $this->flatten($value$flattened$keySeparator, '', $escapeFormulas);
            $value = $flattened;
        }
        unset($value);

        $headers = array_merge(array_values($headers)array_diff($this->extractHeaders($data)$headers));
        $endOfLine = $context[self::END_OF_LINE] ?? $this->defaultContext[self::END_OF_LINE];

        if (!($context[self::NO_HEADERS_KEY] ?? $this->defaultContext[self::NO_HEADERS_KEY])) {
            fputcsv($handle$headers$delimiter$enclosure$escapeChar);
            if ("\n" !== $endOfLine && 0 === fseek($handle, -1, \SEEK_CUR)) {
                fwrite($handle$endOfLine);
            }
        }

        $headers = array_fill_keys($headers, '');
        foreach ($data as $row) {
            fputcsv($handlearray_replace($headers$row)$delimiter$enclosure$escapeChar);
            if ("\n" !== $endOfLine && 0 === fseek($handle, -1, \SEEK_CUR)) {
                fwrite($handle$endOfLine);
            }
        }

        $this->loadConfig($config);

        if ($index === 0) {
            $this->writeToBuffer(array_keys($data));
        }
        $this->writeToBuffer(array_values($data));
    }

    private function writeToBuffer(array $data): void
    {
        if (fputcsv($this->buffer, $data$this->delimiter, $this->enclosure) === false) {
            throw new \RuntimeException('Could not write to buffer');
        }
    }

    private function loadConfig(Config $config): void
    {
        $this->delimiter = $config->get('delimiter') ?? $this->delimiter;
        $this->enclosure = $config->get('enclosure') ?? $this->enclosure;
    }
}
if (false === file_put_contents($file$data, \LOCK_EX)) {
            return false;
        }

        if (!$profileIndexed) {
            // Add to index             if (false === $file = fopen($this->getIndexFilename(), 'a')) {
                return false;
            }

            fputcsv($file[
                $profile->getToken(),
                $profile->getIp(),
                $profile->getMethod(),
                $profile->getUrl(),
                $profile->getTime() ?: time(),
                $profile->getParentToken(),
                $profile->getStatusCode(),
            ]);
            fclose($file);

            if (1 === mt_rand(1, 10)) {
                
Home | Imprint | This part of the site doesn't use cookies.